From c46483ab52b96c249c81326013d21f9aa35dd054 Mon Sep 17 00:00:00 2001 From: CHAHANS Date: Mon, 20 Nov 2023 12:56:34 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat(docs):=20=EC=A4=80=EB=B9=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2d..96b4de2beb6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1 @@ +# 최종 코테 연습용 From 620bcd12843bef1854d9d93615280e5ec1bf0063 Mon Sep 17 00:00:00 2001 From: CHAHANS Date: Mon, 20 Nov 2023 16:46:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat(racingcar):=20=EC=A0=9C=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 24 +++++++ src/main/java/racingcar/Application.java | 1 + src/main/java/racingcar/domain/Car.java | 36 ++++++++++ src/main/java/racingcar/domain/CarRule.java | 18 +++++ src/main/java/racingcar/domain/Judge.java | 31 ++++++++ .../java/racingcar/domain/RacingCars.java | 31 ++++++++ .../java/racingcar/message/ErrorMessage.java | 16 +++++ .../java/racingcar/message/GuideMessage.java | 16 +++++ .../java/racingcar/message/OutputFormat.java | 17 +++++ .../java/racingcar/racingCarController.java | 52 ++++++++++++++ .../java/racingcar/util/NumberGenerator.java | 18 +++++ .../java/racingcar/view/InputValidator.java | 21 ++++++ src/main/java/racingcar/view/InputView.java | 40 +++++++++++ src/main/java/racingcar/view/OutputView.java | 43 +++++++++++ src/test/java/racingcar/domain/CarTest.java | 27 +++++++ src/test/java/racingcar/domain/JudgeTest.java | 48 +++++++++++++ .../java/racingcar/domain/RacingCarsTest.java | 33 +++++++++ .../racingcar/util/NumberGeneratorTest.java | 15 ++++ .../racingcar/view/InputValidatorTest.java | 43 +++++++++++ .../java/racingcar/view/InputViewTest.java | 24 +++++++ .../java/racingcar/view/OutputViewTest.java | 71 +++++++++++++++++++ 21 files changed, 625 insertions(+) create mode 100644 src/main/java/racingcar/domain/Car.java create mode 100644 src/main/java/racingcar/domain/CarRule.java create mode 100644 src/main/java/racingcar/domain/Judge.java create mode 100644 src/main/java/racingcar/domain/RacingCars.java create mode 100644 src/main/java/racingcar/message/ErrorMessage.java create mode 100644 src/main/java/racingcar/message/GuideMessage.java create mode 100644 src/main/java/racingcar/message/OutputFormat.java create mode 100644 src/main/java/racingcar/racingCarController.java create mode 100644 src/main/java/racingcar/util/NumberGenerator.java create mode 100644 src/main/java/racingcar/view/InputValidator.java create mode 100644 src/main/java/racingcar/view/InputView.java create mode 100644 src/main/java/racingcar/view/OutputView.java create mode 100644 src/test/java/racingcar/domain/CarTest.java create mode 100644 src/test/java/racingcar/domain/JudgeTest.java create mode 100644 src/test/java/racingcar/domain/RacingCarsTest.java create mode 100644 src/test/java/racingcar/util/NumberGeneratorTest.java create mode 100644 src/test/java/racingcar/view/InputValidatorTest.java create mode 100644 src/test/java/racingcar/view/InputViewTest.java create mode 100644 src/test/java/racingcar/view/OutputViewTest.java diff --git a/docs/README.md b/docs/README.md index 96b4de2beb6..25b2268c86c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1 +1,25 @@ # 최종 코테 연습용 + +## 기능 +- [x] 가이드 메세지 노출 +- [x] 각 자동차에 이름을 부여할 수 있다. +- [x] 자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다. +- [x] 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. +- [x] 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다. + - [x] 입력값 없는 경우(전체 ,split ",") + - [x] 숫자로 변경할 수 없는 경우 +- [x] 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. +- [x] 전진하는 조건은 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상일 경우이다. +- [x] 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다. +- [x] 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다. +- [x] 우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다. + +## 추가 요구사항 +- [x] JDK에서 제공하는 Random 및 Scanner API 대신 camp.nextstep.edu.missionutils에서 제공하는 Randoms 및 Console API를 사용하여 구현해야 한다. + - [x] Random 값 추출은 camp.nextstep.edu.missionutils.Randoms의 pickNumberInRange()를 활용한다. + - [x] 사용자가 입력하는 값은 camp.nextstep.edu.missionutils.Console의 readLine()을 활용한다. +- [x] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다. + 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다. + 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다. +- [x] 3항 연산자를 쓰지 않는다. +- [ ] 함수(또는 메서드)가 한 가지 일만 하도록 최대한 작게 만들어라. diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e7242..a81f097e996 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -3,5 +3,6 @@ public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + new racingCarController().mainRun(); } } diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java new file mode 100644 index 00000000000..36685688dd8 --- /dev/null +++ b/src/main/java/racingcar/domain/Car.java @@ -0,0 +1,36 @@ +package racingcar.domain; + + +import static racingcar.domain.CarRule.*; +import static racingcar.message.ErrorMessage.CAR_NAME_ERROR; + +public class Car { + private final String name; + private int location; + + public Car(String name) { + validateName(name); + this.name = name; + this.location = CAR_LOCATION_INIT.getNumber(); + } + + public void move(int number) { + if (number >= CAR_MOVE_MINIMUM.getNumber()) { + this.location += CAR_LOCATION_STEP.getNumber(); + } + } + + private void validateName(String newCarName) { + if (!(newCarName.length() <= CAR_NAME_MAN_SIZE.getNumber())) { + throw new IllegalArgumentException(CAR_NAME_ERROR.getMessage()); + } + } + + public String getName() { + return name; + } + + public int getLocation() { + return location; + } +} diff --git a/src/main/java/racingcar/domain/CarRule.java b/src/main/java/racingcar/domain/CarRule.java new file mode 100644 index 00000000000..c4a886c8b25 --- /dev/null +++ b/src/main/java/racingcar/domain/CarRule.java @@ -0,0 +1,18 @@ +package racingcar.domain; + +public enum CarRule { + CAR_NAME_MAN_SIZE(5), + CAR_LOCATION_INIT(0), + CAR_LOCATION_STEP(1), + CAR_MOVE_MINIMUM(4); + + private final int number; + + CarRule(int number) { + this.number = number; + } + + public int getNumber() { + return number; + } +} diff --git a/src/main/java/racingcar/domain/Judge.java b/src/main/java/racingcar/domain/Judge.java new file mode 100644 index 00000000000..2d1a7d88174 --- /dev/null +++ b/src/main/java/racingcar/domain/Judge.java @@ -0,0 +1,31 @@ +package racingcar.domain; + +import racingcar.util.NumberGenerator; + +import java.util.ArrayList; +import java.util.List; + +import static racingcar.domain.CarRule.CAR_LOCATION_INIT; + +public class Judge { + + public List getWinners(List racingCars) { + List winners = new ArrayList<>(); + int maxLocation = findMaxLocation(racingCars); + for (Car thisCar : racingCars) { + if (thisCar.getLocation() == maxLocation) { + winners.add(thisCar); + } + } + return winners; + } + + private int findMaxLocation(List racingCars) { + int maxLocation = CAR_LOCATION_INIT.getNumber(); + NumberGenerator ng = new NumberGenerator(); + for (Car thisCar : racingCars) { + maxLocation = ng.findMaxNumber(maxLocation, thisCar.getLocation()); + } + return maxLocation; + } +} diff --git a/src/main/java/racingcar/domain/RacingCars.java b/src/main/java/racingcar/domain/RacingCars.java new file mode 100644 index 00000000000..fec93a4f7a3 --- /dev/null +++ b/src/main/java/racingcar/domain/RacingCars.java @@ -0,0 +1,31 @@ +package racingcar.domain; + +import racingcar.util.NumberGenerator; + +import java.util.ArrayList; +import java.util.List; + +public class RacingCars { + private List cars; + public RacingCars(List newCars) { + this.cars = new ArrayList<>(); + setRacingCars(newCars); + } + + private void setRacingCars(List newCars) { + for (String carName : newCars) { + this.cars.add(new Car(carName)); + } + } + + public void racingStepOne() { + NumberGenerator ng = new NumberGenerator(); + for (Car thisCar : this.cars) { + thisCar.move(ng.getRandomNumber()); + } + } + + public List getCars() { + return this.cars; + } +} diff --git a/src/main/java/racingcar/message/ErrorMessage.java b/src/main/java/racingcar/message/ErrorMessage.java new file mode 100644 index 00000000000..09a5e196dee --- /dev/null +++ b/src/main/java/racingcar/message/ErrorMessage.java @@ -0,0 +1,16 @@ +package racingcar.message; + +public enum ErrorMessage { + CAR_NAME_ERROR("[ERROR] 잘못된 이름입니다."), + INCLUDE_NULL_ERROR("[ERROR] 입력에 공백이 있습니다."), + NOT_NUMBERS_ERROR("[ERROR] 잘못된 숫자입니다."); + private final String message; + + ErrorMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/racingcar/message/GuideMessage.java b/src/main/java/racingcar/message/GuideMessage.java new file mode 100644 index 00000000000..a455ff31009 --- /dev/null +++ b/src/main/java/racingcar/message/GuideMessage.java @@ -0,0 +1,16 @@ +package racingcar.message; + +public enum GuideMessage { + INPUT_NAME_GUIDE("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), + INPUT_RACE_NUM_GUIDE("시도할 회수는 몇회인가요?"); + + private final String message; + + GuideMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/racingcar/message/OutputFormat.java b/src/main/java/racingcar/message/OutputFormat.java new file mode 100644 index 00000000000..bfe9d259e57 --- /dev/null +++ b/src/main/java/racingcar/message/OutputFormat.java @@ -0,0 +1,17 @@ +package racingcar.message; + +public enum OutputFormat { + RACING_NOW_FORMAT("%s : %s%n"), + RACING_WINNER_FORMAT("최종 우승자 : %s%n"), + CAR_NAME_STEP_FORMAT(", "), + LOCATION_STEP_FORMAT("-"); + private final String format; + + OutputFormat(String format) { + this.format = format; + } + + public String getFormat() { + return format; + } +} diff --git a/src/main/java/racingcar/racingCarController.java b/src/main/java/racingcar/racingCarController.java new file mode 100644 index 00000000000..5b738e2974b --- /dev/null +++ b/src/main/java/racingcar/racingCarController.java @@ -0,0 +1,52 @@ +package racingcar; + +import racingcar.domain.Judge; +import racingcar.domain.RacingCars; +import racingcar.view.InputView; +import racingcar.view.OutputView; + + +public class racingCarController { + private final InputView inputView; + private final OutputView outputView; + private RacingCars racingCars; + + public racingCarController() { + this.inputView = new InputView(); + this.outputView = new OutputView(); + } + + public void mainRun() { + //차량이름 입력받아 준비 + createCar(); + + //레이싱 반복 횟수 입력받아 반복실행 + runRacing(); + + //결과 출력 + result(); + + } + + private void createCar() { + this.racingCars = new RacingCars(inputView.inputCarName()); + } + + private void runRacing() { + int racingNum = inputView.inputRacingNum(); + racingLoop(racingNum); + } + + private void racingLoop(int number) { + for (int i = 0; i < number; i++) { + racingCars.racingStepOne(); + outputView.racingNow(racingCars); + } + } + + private void result() { + Judge judge = new Judge(); + outputView.printWinners(judge.getWinners(racingCars.getCars())); + } + +} diff --git a/src/main/java/racingcar/util/NumberGenerator.java b/src/main/java/racingcar/util/NumberGenerator.java new file mode 100644 index 00000000000..5e395da6a80 --- /dev/null +++ b/src/main/java/racingcar/util/NumberGenerator.java @@ -0,0 +1,18 @@ +package racingcar.util; + +import camp.nextstep.edu.missionutils.Randoms; + +public class NumberGenerator { + public static final int MIN_NUMBER = 1; + public static final int MAX_NUMBER = 9; + public int getRandomNumber () { + return Randoms.pickNumberInRange(MIN_NUMBER,MAX_NUMBER); + } + + public int findMaxNumber (int number1, int number2) { + if(number1>number2) { + return number1; + } + return number2; + } +} diff --git a/src/main/java/racingcar/view/InputValidator.java b/src/main/java/racingcar/view/InputValidator.java new file mode 100644 index 00000000000..f14f7fcda67 --- /dev/null +++ b/src/main/java/racingcar/view/InputValidator.java @@ -0,0 +1,21 @@ +package racingcar.view; + +import java.util.List; + +public class InputValidator { + + public boolean nameSplitValid(List names) { + //전체가 빈 경우 + if (names.isEmpty()) { + return false; + } + //split 된 이름이 빈경우 + for (String name : names) { + if (name.isEmpty()) { + return false; + } + } + return true; + } + +} diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java new file mode 100644 index 00000000000..42bcb5d75fe --- /dev/null +++ b/src/main/java/racingcar/view/InputView.java @@ -0,0 +1,40 @@ +package racingcar.view; + +import camp.nextstep.edu.missionutils.Console; +import racingcar.message.OutputFormat; + +import java.util.Arrays; +import java.util.List; + +import static racingcar.message.ErrorMessage.NOT_NUMBERS_ERROR; +import static racingcar.message.ErrorMessage.INCLUDE_NULL_ERROR; +import static racingcar.message.GuideMessage.INPUT_NAME_GUIDE; +import static racingcar.message.GuideMessage.INPUT_RACE_NUM_GUIDE; + +public class InputView { + private final InputValidator inputValidator; + + public InputView() { + this.inputValidator = new InputValidator(); + } + + public List inputCarName() { + System.out.println(INPUT_NAME_GUIDE.getMessage()); + List cars = Arrays.asList(Console.readLine().trim().split(OutputFormat.CAR_NAME_STEP_FORMAT.getFormat())); + if (!inputValidator.nameSplitValid(cars)) { + System.out.println(INCLUDE_NULL_ERROR.getMessage()); + throw new IllegalArgumentException(); + } + return cars; + } + + public int inputRacingNum() { + System.out.println(INPUT_RACE_NUM_GUIDE.getMessage()); + try { + return Integer.parseInt(Console.readLine().trim()); + } catch (NumberFormatException e) { + System.out.println(NOT_NUMBERS_ERROR.getMessage()); + throw new IllegalArgumentException(); + } + } +} diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java new file mode 100644 index 00000000000..d9c33919845 --- /dev/null +++ b/src/main/java/racingcar/view/OutputView.java @@ -0,0 +1,43 @@ +package racingcar.view; + +import racingcar.domain.Car; +import racingcar.domain.RacingCars; + +import java.util.Iterator; +import java.util.List; + +import static racingcar.message.OutputFormat.*; + +public class OutputView { + public void racingNow(RacingCars racingCars) { + for (Car thisCar : racingCars.getCars()) { + System.out.printf(RACING_NOW_FORMAT.getFormat(), thisCar.getName(), makeLocationPrinter(thisCar.getLocation())); + } + System.out.println(); + } + + public void printWinners(List cars) { + System.out.printf(RACING_WINNER_FORMAT.getFormat(), makeWinnerPrinter(cars)); + } + + private String makeLocationPrinter(int location) { + StringBuilder locationPrint = new StringBuilder(); + for (int i = 0; i < location; i++) { + locationPrint.append(LOCATION_STEP_FORMAT.getFormat()); + } + return locationPrint.toString(); + } + + private String makeWinnerPrinter(List cars) { + StringBuilder winnerPrinter = new StringBuilder(); + Iterator Cars = cars.iterator(); + while (Cars.hasNext()) { + winnerPrinter.append(Cars.next().getName()); + if (Cars.hasNext()) { + winnerPrinter.append(CAR_NAME_STEP_FORMAT.getFormat()); + } + } + return winnerPrinter.toString(); + } + +} diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/CarTest.java new file mode 100644 index 00000000000..aafe715e35e --- /dev/null +++ b/src/test/java/racingcar/domain/CarTest.java @@ -0,0 +1,27 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; + +class CarTest { + + @DisplayName("이름이 6자인 경우 예외 발생") + @Test + void nameLenTest() { + assertThatThrownBy(() -> new Car("qwertt")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("4이상인 경우 지정된 step만큼 증가") + @Test + void moveTureTest() { + Car thisCar = new Car("qwer"); + thisCar.move(4); + assertThat(thisCar.getLocation()).isEqualTo(CarRule.CAR_LOCATION_STEP.getNumber()); + } + + +} \ No newline at end of file diff --git a/src/test/java/racingcar/domain/JudgeTest.java b/src/test/java/racingcar/domain/JudgeTest.java new file mode 100644 index 00000000000..d6962a41c33 --- /dev/null +++ b/src/test/java/racingcar/domain/JudgeTest.java @@ -0,0 +1,48 @@ +package racingcar.domain; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class JudgeTest { + private Judge judge; + + @BeforeEach + void setUp() { + this.judge = new Judge(); + } + + @DisplayName("첫번째 차량만 이동시켜 우승자 확인") + @Test + void checkWinnerTest() { + RacingCars racingCars = new RacingCars(List.of("asdf", "qwer")); + racingCars.getCars().get(0).move(5); + + List expect = new ArrayList<>(); + expect.add(racingCars.getCars().get(0)); + + assertThat(judge.getWinners(racingCars.getCars())) + .isEqualTo(expect); + } + + @DisplayName("차량 모두 이동시켜 중복 우승자 확인") + @Test + void checkWinnersTest() { + RacingCars racingCars = new RacingCars(List.of("asdf", "qwer")); + racingCars.getCars().get(0).move(5); + racingCars.getCars().get(1).move(5); + + List expect = new ArrayList<>(); + expect.add(racingCars.getCars().get(0)); + expect.add(racingCars.getCars().get(1)); + + assertThat(judge.getWinners(racingCars.getCars())) + .isEqualTo(expect); + } + +} \ No newline at end of file diff --git a/src/test/java/racingcar/domain/RacingCarsTest.java b/src/test/java/racingcar/domain/RacingCarsTest.java new file mode 100644 index 00000000000..f005390f856 --- /dev/null +++ b/src/test/java/racingcar/domain/RacingCarsTest.java @@ -0,0 +1,33 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; + +class RacingCarsTest { + @DisplayName("경주용 자동차가 저장되는지 확인") + @Test + void createCarsTest() { + List cars = Arrays.asList("abcd", "qwer"); + RacingCars racingCars = new RacingCars(cars); + Iterator nowCars = racingCars.getCars().iterator(); + assertThat(nowCars.next().getName()).isEqualTo("abcd"); + assertThat(nowCars.next().getName()).isEqualTo("qwer"); + } + + @DisplayName("경주용 자동차 이름 예외발생") + @Test + void createCarsFalseTest() { + List cars = Arrays.asList("abcd", "qwertq"); + assertThatThrownBy(() -> new RacingCars(cars)) + .isInstanceOf(IllegalArgumentException.class); + } + + +} \ No newline at end of file diff --git a/src/test/java/racingcar/util/NumberGeneratorTest.java b/src/test/java/racingcar/util/NumberGeneratorTest.java new file mode 100644 index 00000000000..6f2312959a3 --- /dev/null +++ b/src/test/java/racingcar/util/NumberGeneratorTest.java @@ -0,0 +1,15 @@ +package racingcar.util; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class NumberGeneratorTest { + @DisplayName("숫자 비교 기능 테스트") + @Test + void compareNumberTest() { + NumberGenerator numberGenerator = new NumberGenerator(); + assertThat(numberGenerator.findMaxNumber(1,2)).isEqualTo(2); + } +} \ No newline at end of file diff --git a/src/test/java/racingcar/view/InputValidatorTest.java b/src/test/java/racingcar/view/InputValidatorTest.java new file mode 100644 index 00000000000..fece8e70d32 --- /dev/null +++ b/src/test/java/racingcar/view/InputValidatorTest.java @@ -0,0 +1,43 @@ +package racingcar.view; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +class InputValidatorTest { + + private InputValidator inputValidator; + + @BeforeEach + void setup() { + this.inputValidator = new InputValidator(); + } + + @DisplayName("이름에 빈 공간이 있는 경우 예외발생") + @Test + void inputNameValidFalseTest() { + List names = List.of("nana", "", "sol"); + assertThat(inputValidator.nameSplitValid(names)).isFalse(); + + } + + @DisplayName("이름에 빈 공간이 없는 경우 true") + @Test + void inputNameValidTrueTest() { + List names = List.of("nana", "sol"); + assertThat(inputValidator.nameSplitValid(names)).isTrue(); + } + + @DisplayName("이름 없는 경우") + @Test + void inputNameValidNullTest() { + List names = List.of(""); + assertThat(inputValidator.nameSplitValid(names)).isFalse(); + } + + +} \ No newline at end of file diff --git a/src/test/java/racingcar/view/InputViewTest.java b/src/test/java/racingcar/view/InputViewTest.java new file mode 100644 index 00000000000..3e2b7d75e8a --- /dev/null +++ b/src/test/java/racingcar/view/InputViewTest.java @@ -0,0 +1,24 @@ +package racingcar.view; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class InputViewTest { + + @DisplayName("숫자로 변경할 수 없는 값 입력시 애러") + @Test + void notNumberTest() { + String input = "aa"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + + InputView inputView = new InputView(); + + assertThatThrownBy(() -> inputView.inputRacingNum()) + .isInstanceOf(IllegalArgumentException.class); + } + +} \ No newline at end of file diff --git a/src/test/java/racingcar/view/OutputViewTest.java b/src/test/java/racingcar/view/OutputViewTest.java new file mode 100644 index 00000000000..73a98a70c9d --- /dev/null +++ b/src/test/java/racingcar/view/OutputViewTest.java @@ -0,0 +1,71 @@ +package racingcar.view; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import racingcar.domain.Judge; +import racingcar.domain.RacingCars; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class OutputViewTest { + private OutputView outputView; + private OutputStream outMessage; + private Judge judge; + + @BeforeEach + void setUp() { + this.outputView = new OutputView(); + this.outMessage = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outMessage)); + this.judge = new Judge(); + } + + @DisplayName("전진 stop 메세지 테스트") + @Test + void racingNow() { + RacingCars racingCars = new RacingCars(List.of("asdf", "qwer")); + racingCars.getCars().get(0).move(5); + + outputView.racingNow(racingCars); + assertThat(outMessage.toString()) + .contains("asdf : -") + .contains("qwer :"); + + } + + @DisplayName("단일 우승자 출력 테스트") + @Test + void printWinner() { + RacingCars racingCars = new RacingCars(List.of("asdf", "qwer")); + racingCars.getCars().get(0).move(5); + + outputView.printWinners(judge.getWinners(racingCars.getCars())); + assertThat(outMessage.toString()) + .contains("최종 우승자") + .contains("asdf") + .doesNotContain("qwer"); + } + + @DisplayName("중복 우승자 출력 테스트") + @Test + void printWinners() { + RacingCars racingCars = new RacingCars(List.of("asdf", "qwer", "zxcv","uiop")); + racingCars.getCars().get(0).move(5); + racingCars.getCars().get(1).move(5); + racingCars.getCars().get(2).move(5); + + outputView.printWinners(judge.getWinners(racingCars.getCars())); + assertThat(outMessage.toString()) + .contains("최종 우승자") + .contains("asdf") + .contains("qwer") + .contains("zxcv") + .doesNotContain("uiop"); + } +} \ No newline at end of file From aafee0bcb04beb5fec395d3adbbd2028f75cdc79 Mon Sep 17 00:00:00 2001 From: CHAHANS Date: Mon, 20 Nov 2023 16:54:38 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor(view):=20=EC=9E=98=EB=AA=BB?= =?UTF-8?q?=EB=90=9C=20=EC=83=81=EC=88=98=20=EC=82=AC=EC=9A=A9=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Car.java | 2 +- src/main/java/racingcar/domain/CarRule.java | 2 +- src/main/java/racingcar/view/InputView.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 36685688dd8..c935c504d81 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -21,7 +21,7 @@ public void move(int number) { } private void validateName(String newCarName) { - if (!(newCarName.length() <= CAR_NAME_MAN_SIZE.getNumber())) { + if (!(newCarName.length() <= CAR_NAME_MIN_SIZE.getNumber())) { throw new IllegalArgumentException(CAR_NAME_ERROR.getMessage()); } } diff --git a/src/main/java/racingcar/domain/CarRule.java b/src/main/java/racingcar/domain/CarRule.java index c4a886c8b25..07b294565ff 100644 --- a/src/main/java/racingcar/domain/CarRule.java +++ b/src/main/java/racingcar/domain/CarRule.java @@ -1,7 +1,7 @@ package racingcar.domain; public enum CarRule { - CAR_NAME_MAN_SIZE(5), + CAR_NAME_MIN_SIZE(5), CAR_LOCATION_INIT(0), CAR_LOCATION_STEP(1), CAR_MOVE_MINIMUM(4); diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 42bcb5d75fe..63f9c2f30ce 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -13,6 +13,7 @@ public class InputView { private final InputValidator inputValidator; + private static final String NAME_SPLIT_FORMAT = ","; public InputView() { this.inputValidator = new InputValidator(); @@ -20,7 +21,7 @@ public InputView() { public List inputCarName() { System.out.println(INPUT_NAME_GUIDE.getMessage()); - List cars = Arrays.asList(Console.readLine().trim().split(OutputFormat.CAR_NAME_STEP_FORMAT.getFormat())); + List cars = Arrays.asList(Console.readLine().trim().split(NAME_SPLIT_FORMAT)); if (!inputValidator.nameSplitValid(cars)) { System.out.println(INCLUDE_NULL_ERROR.getMessage()); throw new IllegalArgumentException();