diff --git a/docs/README.md b/docs/README.md index e69de29..317744b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,7 @@ +구현해야할 목록 + +자동차 이름과 시도횟수 입력 + +레이싱 진행 출력 + +우승자 출력 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..98f2656 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -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 Cars = inputCars(); + int round = inputRound(); + Cars = rounding(round, Cars); + Cars = findWinner(Cars); + award(Cars); + } + + private static boolean nameAgain(ArrayList Cars){ + Set nameSet = new HashSet<>(); + for(int i = 0; i < Cars.size() ; i++) { + if (!nameSet.add(Cars.get(i).getName())) { + return true; + } + } + return false; + } + + private static ArrayList inputCars(){ + ArrayList 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])); + } + } + 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 racing(ArrayList 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 rounding(int round, ArrayList 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 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 findWinner(ArrayList 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 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; + } + } } } diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java new file mode 100644 index 0000000..1b16759 --- /dev/null +++ b/src/main/java/racingcar/Car.java @@ -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; + } +}