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..ba55ee9 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,16 @@ package racingcar; public class Application { + public static void main(String[] args) { // TODO: 프로그램 구현 + RaceSetup setup = new RaceSetup(); + setup.raceSetup(); + + Race race = new Race(setup.getCarsList(), setup.getRound()); + race.progressRounds(); + + RaceResult result = new RaceResult(setup.getCarsList()); + result.printWinner(); } } diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java new file mode 100644 index 0000000..ce4b34d --- /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; + } +} diff --git a/src/main/java/racingcar/Race.java b/src/main/java/racingcar/Race.java new file mode 100644 index 0000000..45a4311 --- /dev/null +++ b/src/main/java/racingcar/Race.java @@ -0,0 +1,45 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; + + +public class Race { + private ArrayList cars; + private int round; + + Race(ArrayList cars, int round) { + this.cars = cars; + this.round = round; + } + + public void progressRounds() { + System.out.println("실행 결과"); + for (int i = 0; i < this.round; i++) { + moveCars(); + printScore(); + System.out.println(" "); + } + } + + private void moveCars() { + int random; + + for (int i = 0; i < this.cars.size(); i++) { + random = Randoms.pickNumberInRange(0, 9); + if (random >= 4) { + this.cars.get(i).move(); + } + } + } + + private void printScore() { + for (int i = 0; i < this.cars.size(); i++) { + System.out.print(this.cars.get(i).getName() + " : "); + for (int j = 0; j < this.cars.get(i).getScore(); j++) { + System.out.print("-"); + } + System.out.println(" "); + } + } +} diff --git a/src/main/java/racingcar/RaceResult.java b/src/main/java/racingcar/RaceResult.java new file mode 100644 index 0000000..1df4f4c --- /dev/null +++ b/src/main/java/racingcar/RaceResult.java @@ -0,0 +1,44 @@ +package racingcar; + +import java.util.ArrayList; + +public class RaceResult { + private ArrayList cars; + RaceResult(ArrayList cars) { + this.cars = cars; + } + + public void printWinner() { + boolean flag = true; + + this.findWinner(); + System.out.print("최종 우승자 : "); + + for (int i = 0; i < this.cars.size(); i++) { + if (this.cars.get(i).isWinner() && !flag) { + System.out.print(", "); + System.out.print(this.cars.get(i).getName()); + } + if (this.cars.get(i).isWinner() && flag) { + System.out.print(this.cars.get(i).getName()); + flag = false; + } + } + } + + private void findWinner() { + int maxScore = 0; + + for (int i = 0; i < this.cars.size(); i++) { + if (this.cars.get(i).getScore() >= maxScore) { + maxScore = this.cars.get(i).getScore(); + } + } + + for (int i = 0; i < cars.size(); i++) { + if (cars.get(i).getScore() == maxScore) { + cars.get(i).win(); + } + } + } +} diff --git a/src/main/java/racingcar/RaceSetup.java b/src/main/java/racingcar/RaceSetup.java new file mode 100644 index 0000000..0fd94f4 --- /dev/null +++ b/src/main/java/racingcar/RaceSetup.java @@ -0,0 +1,69 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +public class RaceSetup { + private ArrayList cars; + private int round; + + RaceSetup(){} + + public void raceSetup() { + cars = this.setupCars(); + this.inputRound(); + } + + public ArrayList getCarsList() { + return cars; + } + + public int getRound() { + return round; + } + + private boolean isDuplicateName(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 ArrayList setupCars() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String input = Console.readLine(); + String[] names = input.split(","); + ArrayList cars = new ArrayList<>(); + + for (int i = 0; i < names.length; i++) { + if (names[i].length() > 5 || names[i].contains(" ") || names[i].isEmpty()) { + throw new IllegalArgumentException(); + } + cars.add(new Car(names[i])); + } + if (isDuplicateName(cars)) { + throw new IllegalArgumentException(); + } + return cars; + } + + private void inputRound() { + System.out.println("시도할 회수는 몇회인가요?"); + try { + this.round = Integer.parseInt(Console.readLine().trim()); + } catch(NumberFormatException e) { + throw new IllegalArgumentException(); + } + if (this.round <= 0) { + throw new IllegalArgumentException(); + } + System.out.println(" "); + } +}