diff --git a/README.md b/README.md index 491aece1..16bdf4ef 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -# java-racingcar-precourse \ No newline at end of file +# java-racingcar-precourse + +자동차 생성 시 validation (5자 이하) +게임 시작 시 시도 회수 입력 값에 대한 validation(IllegalArgumentException) diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..c6e7ef0c --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,25 @@ +import java.util.Random; + +public class Car { + private final String name; + private int distance; + private final Random rand; + + public Car(String name,Random random) { + this.name = name; + this.rand = random; + } + public void run(){ + if(rand.nextInt(9)>=4){ + distance++; + } + } + public void printStatus(){ + System.out.print(name+" : "); + for(int i=0; i 5) { + System.out.println("[ERROR] 자동차명은 5자를 초과할 수 없습니다"); + throw new IllegalArgumentException(); + } + } + } + + public void getTryOutNumber() { + System.out.println("시도할 회수는 몇회인가요?"); + try { + tryCnt = sc.nextInt(); + sc.nextLine(); + } catch (NoSuchElementException e) { + System.out.println("[ERROR] 숫자를 입력해주세요"); + sc = new Scanner(System.in); + getTryOutNumber(); + } + if (tryCnt < 0) { + System.out.println("[ERROR] 시도 회수는 양수여야 합니다"); + getTryOutNumber(); + } + } + + public void createCars() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String[] carNames = getListOfCars(); + cars = new Car[carNames.length]; + for (int i = 0; i < carNames.length; i++) { + cars[i] = new Car(carNames[i].trim(), rand); + } + } +}