-
Notifications
You must be signed in to change notification settings - Fork 29
[장지훈_BackEnd] 1주차 과제 제출합니다. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bb90cf8
4d4980a
0fdd0c4
f5cc4a7
9287cb9
91902f8
0a232c0
bc89b37
ec06bea
53803f4
1072cf3
71aa530
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| 구현해야할 목록 | ||
|
|
||
| 자동차 이름과 시도횟수 입력 | ||
|
|
||
| 레이싱 진행 출력 | ||
|
|
||
| 우승자 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,124 @@ | ||
| package racingcar; | ||
| import java.util.*; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| ArrayList<Car> Cars = inputCars(); | ||
| int round = inputRound(); | ||
| Cars = rounding(round, Cars); | ||
| Cars = findWinner(Cars); | ||
| award(Cars); | ||
| } | ||
|
|
||
| private static boolean nameAgain(ArrayList<Car> Cars){ | ||
| Set<String> nameSet = new HashSet<>(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 순서가 없어 중복을 허용하지 않습니다. |
||
| for(int i = 0; i < Cars.size() ; i++) { | ||
| if (!nameSet.add(Cars.get(i).getName())) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static ArrayList<Car> inputCars(){ | ||
| ArrayList<Car> Cars = new ArrayList<>(); | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input = Console.readLine(); | ||
| String[] names = input.split(","); | ||
|
|
||
| for(int i = 0; i < names.length; i++){ | ||
| if(names[i].length() > 5 || names[i].contains(" ") || names[i].isEmpty()){ | ||
| throw new IllegalArgumentException(); | ||
| } else { | ||
| Cars.add(new Car(names[i])); | ||
| } | ||
|
Comment on lines
+36
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별 차이는 없지만, if 문에서 조건이 걸린다면 어차피 예외를 발생해서 프로그램을 종료하므로 else 문은 명시하지 않아도 될 것 같아요!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 더욱 깔끔하게 되네요. 참고하겠습니다. |
||
| } | ||
| if(nameAgain(Cars)){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| return Cars; | ||
| } | ||
|
|
||
| private static int inputRound(){ | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| int round = 0; | ||
| try { | ||
| round = Integer.parseInt(Console.readLine().trim()); | ||
| } catch(NumberFormatException e){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| if(round <= 0){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| System.out.println(" "); | ||
| return round; | ||
| } | ||
|
|
||
| private static ArrayList<Car> racing(ArrayList<Car> Cars){ | ||
| int random = 0; | ||
| for (int i = 0; i < Cars.size(); i++){ | ||
| random = Randoms.pickNumberInRange(0, 9); | ||
| if(random >= 4){ | ||
| Cars.get(i).move(); | ||
| } | ||
| } | ||
| return Cars; | ||
| } | ||
|
|
||
| private static ArrayList<Car> rounding(int round, ArrayList<Car> Cars){ | ||
| System.out.println("실행 결과"); | ||
| for (int i = 0; i < round; i++){ | ||
| racing(Cars); | ||
| chart(Cars); | ||
| System.out.println(" "); | ||
| } | ||
| return Cars; | ||
| } | ||
|
|
||
| private static void chart(ArrayList<Car> Cars){ | ||
| for(int i = 0; i < Cars.size(); i++){ | ||
| System.out.print(Cars.get(i).getName() + " : "); | ||
| for(int j = 0; j < Cars.get(i).getScore(); j++){ | ||
| System.out.print("-"); | ||
| } | ||
| System.out.println(" "); | ||
| } | ||
| } | ||
|
|
||
| private static ArrayList<Car> findWinner(ArrayList<Car> Cars){ | ||
| int maxScore = 0; | ||
|
|
||
| for(int i = 0; i < Cars.size(); i++){ | ||
| if(Cars.get(i).getScore() >= maxScore){ | ||
| maxScore = Cars.get(i).getScore(); | ||
| } | ||
| } | ||
|
|
||
| for(int i = 0; i < Cars.size(); i++){ | ||
| if(Cars.get(i).getScore() == maxScore){ | ||
| Cars.get(i).win(); | ||
| } | ||
| } | ||
|
|
||
| return Cars; | ||
| } | ||
|
|
||
| private static void award(ArrayList<Car> Cars) { | ||
| boolean flag = true; | ||
| System.out.print("최종 우승자 : "); | ||
| for (int i = 0; i < Cars.size(); i++){ | ||
| if(Cars.get(i).isWinner() && !flag){ | ||
| System.out.print(", "); | ||
| System.out.print(Cars.get(i).getName()); | ||
| } | ||
| if(Cars.get(i).isWinner() && flag) { | ||
| System.out.print(Cars.get(i).getName()); | ||
| flag = false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package racingcar; | ||
|
|
||
| public class Car { | ||
| private String name; | ||
| private int score = 0; | ||
| private boolean winner = false; | ||
|
|
||
| Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName(){ | ||
| return this.name; | ||
| } | ||
|
|
||
| public int getScore(){ | ||
| return this.score; | ||
| } | ||
|
|
||
| public boolean isWinner(){ | ||
| if(this.winner) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public void move() { | ||
| this.score++; | ||
| } | ||
|
|
||
| public void win(){ | ||
| this.winner = true; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private static메소드는 무슨 특징을 지닐까요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 클래스 안에서만 호출이 가능하고 객체 없이 호출이 가능한 특징이 있습니다. 그리고 여기 클래스 안에서만 사용하고 따로 객체 없이 기능을 하는 메소드라는 특징이 있습니다. 따로 이유는 없이 유틸 메소드를 만들려고 static으로 지정했습니다.