diff --git a/build.gradle b/build.gradle index a89989b..15f3352 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ dependencies { java { toolchain { - languageVersion = JavaLanguageVersion.of(21) + languageVersion = JavaLanguageVersion.of(17) } } diff --git a/docs/README.md b/docs/README.md index e69de29..8b3cd32 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,5 @@ +자동차를 객체로 생성해서 사용한다 +자동차 객체는 이름과 점수를 가진다 + +이름은 한줄로 받은 후에 ","을 기준으로 분리해서 ArrayList에 입력한다. +입력 받은 후 for문을 활용하여 객체를 생성한다 diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..e47a984 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,96 @@ package racingcar; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + ArrayList cars = new ArrayList<>(); + ArrayList winners = new ArrayList<>(); + + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String input = Console.readLine(); + String[] names = input.split(","); + + for (String name : names) { + try{ + RacingCar RC = new RacingCar(name); + cars.add(RC); + } catch (IllegalArgumentException e){ + System.out.println("오류 발생 : " + e.getMessage()); + System.out.println("\"" +name +"\"" + "은 사용하실 수 없습니다."); + System.exit(0); + } + } + + System.out.println("시도할 회수는 몇회인가요?"); + int attempt = Integer.parseInt(Console.readLine()); + System.out.println("실행 결과\n"); + + for (int i = 0; i < attempt; i++) { + for (RacingCar car : cars) { + car.updateScoreOnMove(); + System.out.print(car.name + " : "); + car.printScore(); + } + System.out.println(); + } + int maxScore = 0; + for (RacingCar car : cars) { + if (car.getScore() > maxScore) { + maxScore = car.getScore(); + } + } + + for (RacingCar car : cars) { + if (car.getScore() == maxScore) { + winners.add(car); + } + } + + System.out.print("최종 우승자 :"); + for (RacingCar car : winners) { + System.out.print(car.getName() + " "); + } + } + + private static class RacingCar { + private final String name; + private int score = 0; + + public RacingCar(String name) { + checkValidLength(name); + this.name = name; + } + + public void checkValidLength(String name) { + if (name.length() > 5) { // 이름이 5자 초과일 때 + throw new IllegalArgumentException("이름은 5자 이하만 가능합니다."); + } + } + + public void updateScoreOnMove() { + if(Randoms.pickNumberInRange(0,9)>=4) { + score++; + } + } + + public void printScore() { + for (int i = 0; i < score; i++){ + System.out.print("-"); + } + System.out.println(); + } + + public String getName() { + return name; + } + + public int getScore() { + return score; + } } }