diff --git a/CarRaceController.java b/CarRaceController.java new file mode 100644 index 00000000..f1b1af1d --- /dev/null +++ b/CarRaceController.java @@ -0,0 +1,19 @@ +public class CarRaceController { + private CarRaceModel model; + private CarRaceView view; + private int raceNum; // 사용자 입력값 / 레이스 횟수 + + public CarRaceController(CarRaceModel model, CarRaceView view) { + this.model = model; + this.view = view; + } + + public void startRace(int raceNum) { + this.raceNum = raceNum; + for (int nowRace = 0; nowRace < raceNum; nowRace++) { + model.race(); + view.printRaceInfo(model.getCarList(), model.getCarRaceInfo()); + } + view.printResult(model.getCarList(), model.getCarRaceInfo(), model.getWinnerLength()); + } +} diff --git a/CarRaceModel.java b/CarRaceModel.java new file mode 100644 index 00000000..f8c7ba96 --- /dev/null +++ b/CarRaceModel.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class CarRaceModel { + private List carList; // 사용자 입력값 / 자동차 이름 + private List carRaceInfo; // 자동차마다의 레이스 상황값 + private Random random; + private int winnerLen = 0; // 레이스에서의 최대 길이 + + public CarRaceModel(List carList) { + this.carList = carList; + this.carRaceInfo = new ArrayList<>(carList.size()); + for (int i = 0; i < carList.size(); i++) { + this.carRaceInfo.add(""); + } + this.random = new Random(); + } + + public void race() { + for (int i = 0; i < carList.size(); i++) { + raceStart(i); + } + } + + private void raceStart(int i) { + int randomNumber = random.nextInt(10); + if (randomNumber <= 4) { + carRaceInfo.set(i, carRaceInfo.get(i) + "-"); + winnerLen = Math.max(winnerLen, carRaceInfo.get(i).length()); + } + } + + public List getCarList() { + return carList; + } + + public List getCarRaceInfo() { + return carRaceInfo; + } + + public int getWinnerLength() { + return winnerLen; + } +} diff --git a/CarRaceView.java b/CarRaceView.java new file mode 100644 index 00000000..9b604d46 --- /dev/null +++ b/CarRaceView.java @@ -0,0 +1,26 @@ +import java.util.List; + +public class CarRaceView { + public void printRaceInfo(List carList, List carRaceInfo) { + for (int i = 0; i < carList.size(); i++) { + System.out.println(carList.get(i) + " : " + carRaceInfo.get(i)); + } + System.out.println(); + } + + public void printResult(List carList, List carRaceInfo, int winnerLen) { + boolean firstWinner = true; + System.out.print("최종 우승자 : "); + + for (int i = 0; i < carList.size(); i++) { + if (carRaceInfo.get(i).length() == winnerLen) { + if (firstWinner) { + firstWinner = false; + System.out.print(carList.get(i)); + continue; + } + System.out.print(", " + carList.get(i)); + } + } + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 00000000..6b92dc62 --- /dev/null +++ b/Main.java @@ -0,0 +1,86 @@ +import java.util.List; +import java.util.Scanner; +import java.util.ArrayList; + +public class Main { + + static Scanner scanner ; + static List carList ; + + public static void main(String[] args) { + scanner = new Scanner(System.in); + + System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분"); + getCarList(); + + System.out.println("시도할 회수는 몇회인가요?"); + int raceNum = getRaceNum(); + + // int raceNum = Integer.parseInt(scanner.nextLine()); + System.out.println(); + + CarRaceModel model = new CarRaceModel(carList); + CarRaceView view = new CarRaceView(); + CarRaceController controller = new CarRaceController(model, view); + controller.startRace(raceNum); + } + + public static List getCarList(){ + carList = new ArrayList<>(); + + while (true) { + String input = scanner.nextLine().trim(); + + try { + validateInput(input); + break; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + } + } + return carList ; + } + + public static List checkCarName(String input) { // 입력받은 차 이름 반환 + List checkedNames = new ArrayList<>(); + String[] names = input.split(","); + + for (String name : names) { + if (name.length() <= 5) { + checkedNames.add(name); + } + } + return checkedNames; + } + + private static void validateInput(String input) throws IllegalArgumentException { + if (input.isEmpty()) { + throw new IllegalArgumentException("입력이 비어있습니다."); + } + + String[] names = input.split(","); + for (String name : names) { + if (name.trim().isEmpty()) { + throw new IllegalArgumentException("빈 자동차 이름이 있습니다."); + } + if (name.trim().length() > 5) { + throw new IllegalArgumentException("자동차 이름은 5자 이하여야 합니다."); + } + carList.add(name); + } + } + + public static int getRaceNum(){ + int raceNum ; + while (true) { + try { + raceNum = scanner.nextInt(); + break; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + scanner.nextLine(); + } + } + return raceNum; + } +} diff --git a/README.md b/README.md index 491aece1..7f2e7279 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# java-racingcar-precourse \ No newline at end of file +# java-racingcar-precourse + +1. 안내문구 프린트하기 +2. 사용자 문자 입력받기 +3. 사용자 입력 쉼표로 분리하기 +4. 사용자 숫자 입력받기 +5. 입력된 숫자만큼 무작위로 자동차 주행하기 + 1. else, switch/case 사용 불가 +6. 우승자 선정 \ No newline at end of file