diff --git a/docs/README.md b/docs/README.md index e69de29..2ecc432 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,22 @@ + +1. Car: 자동차의 생성, 전진/정지를 결정, 결과를 저장. +-> 변수: moveCount = 전진 횟수, carName = 차량 이름(String) +-> 메소드 +* 생성자/init(생성자가 쓰이지 않을 케이스를 염두해두고 만듦. 내용은 생성자와 동일.): 이름을 파라미터로 받아서 저장, moveCount 초기화. +* moveForward: moveCount ++ +* isCarMove: 랜덤 숫자의 크기에 따라 차가 전진/정지 결정하는 함수. +* update: isCarMove에 따라 moveForward를 호출/ 호출X를 결정. + +2. InputOutput: 입력 값 받고, 결과 값 출력 +-> 변수: carCount = 차량 개수, carNames = 차량 이름들을 저장하는 String 배열 +-> 메소드: +* readCarNames: 사용자의 입력 -> 이름들 분리하여 저장 + 차량 개수 저장. +* readDriveCount: 사용자의 입력 -> round 개수 저장. +* printRoundResult: 각 라운드의 결과 값을 출력. 라운드 종료 시점의 car의 moveCount를 출력함. +* printWinner: 모든 라운드 종료 후, winner를 출력 + +3. Application: 실제 프로그램의 플로우가 진행될 곳. +-> 변수: inputOutput = InputOutput class를 멤버로 가지고 있음. cars = Car class를 배열로 가지고 있음. + driveCount = round 개수, carCount = 차량 개수 +-> FLOW: 차량 이름 입력 -> round 개수 입력 -> 차량 개수만큼 차량 생성 -> 첫 번째 round 실행() + -> 결과 값 출력 -> (round 개수만큼 반복) -> 우승자 출력 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..bc0b414 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -2,6 +2,68 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + InputOutput inputOut = new InputOutput(); + + String[] carNames = inputOut.readCarNames(); + int driveCount = inputOut.readDriveCount(); + int carCount = carNames.length; + + Car[] cars = new Car[carCount]; // car 생성 + for(int i =0; i < carCount; i++){ + cars[i] = new Car(carNames[i]); // 초기화 + } + // 여기에 그대로 쓰면 너무 지저분해진다. 따로 빼자.... + + for(int i =0; i = ) + } + }*/ + + private static String[] getWinner(Car[] cars, int carCount, int driveCount){ + String[] winner = new String[carCount]; // max index = 전체 + + int maxMove = driveCount; // 최댓값은 차피 모든 거리를 주행한 것. + int winnerIndex = 0; + + for(int i = maxMove; i >=0; i--){ + for(int j =0; j < carCount; j++){ + if(cars[j].getMoveCount() == i){ + winner[winnerIndex] = cars[j].getCarName(); // car에 넣어줌. + winnerIndex++; + } + } + if(winnerIndex > 0) break; + } + + return winner; // winner에 값이 없으면 지워야 함... + } + } diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java new file mode 100644 index 0000000..c66a556 --- /dev/null +++ b/src/main/java/racingcar/Car.java @@ -0,0 +1,56 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Randoms; + +public class Car { + + private String carName; + private int moveCount; + + Car(String name){ + carName = name; + moveCount = 0; + } + + void init(String name){ + carName = name; + moveCount = 0; + } + + private void moveForward(){ + moveCount++; + } + + private boolean isCarMove(){ + int decisionNumber = Randoms.pickNumberInRange(0,9); + + if( decisionNumber >=4 ) return true; + else return false; + } + + /* + 초기 구상이었으나, 출력값이 횟수 별로 기록되어야 하기 때문에 이 방법은 그리 좋지 않음. + void update(int count) { + for (int i = 0; i < count; i++) { + if (isCarMove()) moveForward(); + } + }*/ + + void update(){ + if(isCarMove()) moveForward(); + } + + // getter/ setter + void setCarName(String name){ + carName = name; + } + + String getCarName(){ + return carName; + } + + int getMoveCount(){ + return moveCount; + } + +} diff --git a/src/main/java/racingcar/InputOutput.java b/src/main/java/racingcar/InputOutput.java new file mode 100644 index 0000000..30f7bea --- /dev/null +++ b/src/main/java/racingcar/InputOutput.java @@ -0,0 +1,58 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Console; + +public class InputOutput { + private int carCount = 0; + private String[] carNames; + + public String[] readCarNames(){ + System.out.print("차량들의 이름을 입력하세요: ( ,로 구별해주세요.)"); + String names = Console.readLine(); + carNames = names.split(", "); + + carCount = carNames.length; + + if(carCount <=1 ) throw new IllegalArgumentException("차량의 이름을 2개 이상 입력해주세요."); + + return carNames; + } + + public int readDriveCount(){ // 횟수가 1 이상의 정수여야 함. done + System.out.print("횟수를 입력하세요: "); + String input = Console.readLine(); + + try{ + carCount = Integer.parseInt(input); + } catch(NumberFormatException e){ + throw new IllegalArgumentException("type error: 1 이상의 정수를 입력해주세요."); + } + + if(carCount <= 0) throw new IllegalArgumentException("0보다 큰 정수여야 합니다."); + + return carCount; + } + + public void printRoundResult(int[] moveCount){ + for(int i =0; i < carCount; i++){ + System.out.print(carNames[i] + " : " ); + for(int j =0; j < moveCount[i]; j++){ + System.out.print("-"); + } + System.out.println(); + } + } + + public void printWinner(String[] winnerList){ + if(winnerList.length == 1) System.out.println("최종 우승자: " + winnerList[0]); + else { + System.out.print("최종 우승자: " + winnerList[0]); + for(int i = 1; i < winnerList.length; i++) System.out.print(", " + winnerList[i]); + } + } + + public int getCarCount() { + return carCount; + } + +}