From 34feffd9ecfeca2c1b58d48b60a5104807b5ea50 Mon Sep 17 00:00:00 2001 From: prolkh Date: Fri, 5 May 2023 16:50:54 +0900 Subject: [PATCH 01/15] =?UTF-8?q?feat=20:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 6 +++++- src/main/java/racingcar/InputView.java | 14 +++++++------- src/main/java/racingcar/Racing.java | 11 ++++++----- src/test/java/racingcar/RacingTest.java | 4 ++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 400fab8adb0..b8c79e1fff8 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -11,9 +11,12 @@ public class Car { private static final int INIT_POSITION = 1; private final Random random = new Random(); + + private final String name; private final List positions; - Car(int trialCount) { + Car(String name, int trialCount) { + this.name = name; positions = new ArrayList<>(); int position = INIT_POSITION; for(int i=0; i< trialCount; i++) { @@ -23,6 +26,7 @@ public class Car { } Car(List positions) { + this.name = ""; this.positions = new ArrayList<>(positions); } diff --git a/src/main/java/racingcar/InputView.java b/src/main/java/racingcar/InputView.java index e3ba6916d4a..0f2aacbdbb2 100644 --- a/src/main/java/racingcar/InputView.java +++ b/src/main/java/racingcar/InputView.java @@ -7,18 +7,18 @@ public class InputView { public Racing init() { Scanner scanner = new Scanner(System.in); - Integer carCount = getCarCount(scanner); + String carNames = getCarNames(scanner); Integer trialCount = getTrialCount(scanner); - System.out.println("carCount = " + carCount); - System.out.println("trialCount = " + trialCount); + System.out.println("Car Names : " + carNames); + System.out.println("Trial Count : " + trialCount); - return new Racing(carCount, trialCount); + return new Racing(carNames, trialCount); } - private Integer getCarCount(Scanner scanner) { - System.out.println("자동차 대수는 몇 대 인가요?"); - return Integer.parseInt(scanner.nextLine()); + private String getCarNames(Scanner scanner) { + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); + return scanner.nextLine(); } private static Integer getTrialCount(Scanner scanner) { diff --git a/src/main/java/racingcar/Racing.java b/src/main/java/racingcar/Racing.java index 2a202b2f8b5..66a4f890f2f 100644 --- a/src/main/java/racingcar/Racing.java +++ b/src/main/java/racingcar/Racing.java @@ -6,14 +6,15 @@ public class Racing { private final List cars; - Racing(int carCount, int trialCount) { - this.cars = createCars(carCount, trialCount); + Racing(String carNames, int trialCount) { + this.cars = createCars(carNames, trialCount); } - private static List createCars(int carCount, int trialCount) { + private static List createCars(String carNames, int trialCount) { + String[] names = carNames.split(","); List cars = new ArrayList<>(); - for(int i=0; i Date: Fri, 5 May 2023 16:55:34 +0900 Subject: [PATCH 02/15] =?UTF-8?q?feat=20:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 8 ++++++-- src/main/java/racingcar/ResultView.java | 1 + src/test/java/racingcar/RacingTest.java | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index b8c79e1fff8..4e6b5864961 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -25,8 +25,8 @@ public class Car { } } - Car(List positions) { - this.name = ""; + Car(String name, List positions) { + this.name = name; this.positions = new ArrayList<>(positions); } @@ -46,4 +46,8 @@ public int proceedOrStop() { int randomValue = random.nextInt(RANDOM_BOUND); return randomValue >= PROCEED_THRESHOLD ? 1 : 0; } + + public String getName() { + return name; + } } diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index 73bbb6fb80e..4235956df27 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -15,6 +15,7 @@ public void displayResults(List cars) { private void displayColumn(List cars, int columnIndex) { for (Car car : cars) { + System.out.printf("%s : ", car.getName()); System.out.println(getPositionLine(car, columnIndex)); } } diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 05232790914..96fc94e937e 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -16,7 +16,7 @@ public class RacingTest { @Test void 전부_합친_테스트() { // Given - String testInput = "3\n5\n"; + String testInput = "pobi,crong,honux\n5\n"; ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); System.setIn(in); @@ -52,9 +52,9 @@ public class RacingTest { // Given List cars = Arrays.asList( - new Car(Arrays.asList(1, 2, 3, 4, 4)), - new Car(Arrays.asList(1, 1, 2, 3, 4)), - new Car(Arrays.asList(1, 2, 3, 4, 5)) + new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), + new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), + new Car("honux", Arrays.asList(1, 2, 3, 4, 5)) ); // Then From 688af00726b0af4f93b0a6bd18da67a56544758a Mon Sep 17 00:00:00 2001 From: prolkh Date: Sun, 7 May 2023 21:01:09 +0900 Subject: [PATCH 03/15] =?UTF-8?q?test=20:=20Car=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 4 ++++ src/test/java/racingcar/RacingTest.java | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 4e6b5864961..c6d7d1aea4a 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -50,4 +50,8 @@ public int proceedOrStop() { public String getName() { return name; } + + public List getPositions() { + return positions; + } } diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 96fc94e937e..ec2687f78ce 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -6,6 +6,8 @@ import java.util.Arrays; import java.util.List; +import static org.assertj.core.api.Assertions.*; + public class RacingTest { public InputView inputView = new InputView(); @@ -14,7 +16,7 @@ public class RacingTest { @Test - void 전부_합친_테스트() { + void 레이싱_전체_테스트() { // Given String testInput = "pobi,crong,honux\n5\n"; ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); @@ -60,4 +62,14 @@ public class RacingTest { // Then resultView.displayResults(cars); } + + @Test + void Car_객체_테스트() { + // Given + Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 5)); + + // Then + assertThat(car.getName()).isEqualTo("pobi"); + assertThat(car.getPositions()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); + } } From 4f1e6b3c6bf90370a213900a19a53b0c87952095 Mon Sep 17 00:00:00 2001 From: prolkh Date: Tue, 9 May 2023 03:46:30 +0900 Subject: [PATCH 04/15] =?UTF-8?q?feat=20:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 7 +++ src/main/java/racingcar/ResultView.java | 60 +++++++++++++++++++++++++ src/test/java/racingcar/RacingTest.java | 13 ++++++ 3 files changed, 80 insertions(+) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index c6d7d1aea4a..f21c76b7ef9 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -38,6 +38,13 @@ public int getPosition(int index) { return positions.get(index); } + public int getLastPosition() { + if(positions.isEmpty()) + return -1; + + return positions.get(positions.size() - 1); + } + private void addPosition(int position) { positions.add(position); } diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index 4235956df27..ec499e8053b 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -1,5 +1,6 @@ package racingcar; +import java.util.ArrayList; import java.util.List; public class ResultView { @@ -20,7 +21,66 @@ private void displayColumn(List cars, int columnIndex) { } } + public String getResults(List cars) { + StringBuilder sb = new StringBuilder(); + int trialCount = cars.get(0).getTrialCount(); + + for (int i = 0; i < trialCount; i++) { + sb.append(getColumn(cars, i)); + sb.append(System.lineSeparator()); + } + + return sb.toString(); + } + + private String getColumn(List cars, int columnIndex) { + StringBuilder sb = new StringBuilder(); + + for (Car car : cars) { + sb.append(car.getName()) + .append(" : ") + .append(getPositionLine(car, columnIndex)) + .append(System.lineSeparator()); + } + + return sb.toString(); + } + + private String getPositionLine(Car car, int columnIndex) { return "-".repeat(car.getPosition(columnIndex)); } + + public String getWinnerMessage(List cars) { + String winner = getWinner(cars, getBiggestLastPosition(cars)); + + return winner + "가 최종 우승했습니다."; + } + + private int getBiggestLastPosition(List cars) { + int max = -1; + + for (Car car : cars) { + int lastPosition = car.getLastPosition(); + + if(max < lastPosition) { + max = lastPosition; + } + } + return max; + } + + private String getWinner(List cars, int biggestLastPosition) { + ArrayList winnerList = new ArrayList<>(); + + for (Car car : cars) { + int lastPosition = car.getLastPosition(); + + if(lastPosition == biggestLastPosition) + winnerList.add(car.getName()); + } + + return String.join(", ", winnerList); + } + } diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index ec2687f78ce..77183831207 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -72,4 +72,17 @@ public class RacingTest { assertThat(car.getName()).isEqualTo("pobi"); assertThat(car.getPositions()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); } + + @Test + void 우승자_출력하기() { + // Given + List cars = Arrays.asList( + new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), + new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), + new Car("honux", Arrays.asList(1, 2, 3, 3, 3)) + ); + + // Then + assertThat(resultView.getWinnerMessage(cars)).isEqualTo("pobi, crong가 최종 우승했습니다."); + } } From 910383ca88a36fac1f9a06edc9ab6fe23f26219e Mon Sep 17 00:00:00 2001 From: prolkh Date: Tue, 9 May 2023 04:23:50 +0900 Subject: [PATCH 05/15] =?UTF-8?q?test=20:=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EB=8C=80=EC=8B=A0=20assertions=20=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B2=80=EC=A6=9D=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/InputView.java | 17 +++++++++---- src/main/java/racingcar/ResultView.java | 14 +---------- src/test/java/racingcar/RacingTest.java | 32 ++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/main/java/racingcar/InputView.java b/src/main/java/racingcar/InputView.java index 0f2aacbdbb2..66c7f092e23 100644 --- a/src/main/java/racingcar/InputView.java +++ b/src/main/java/racingcar/InputView.java @@ -3,15 +3,15 @@ import java.util.Scanner; public class InputView { + private String carNames; + private int trialCount; + public Racing init() { Scanner scanner = new Scanner(System.in); - String carNames = getCarNames(scanner); - Integer trialCount = getTrialCount(scanner); - - System.out.println("Car Names : " + carNames); - System.out.println("Trial Count : " + trialCount); + carNames = getCarNames(scanner); + trialCount = getTrialCount(scanner); return new Racing(carNames, trialCount); } @@ -26,4 +26,11 @@ private static Integer getTrialCount(Scanner scanner) { return Integer.parseInt(scanner.nextLine()); } + public String getCarNames() { + return carNames; + } + + public int getTrialCount() { + return trialCount; + } } diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index ec499e8053b..9f47aeb0b76 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -6,19 +6,7 @@ public class ResultView { public void displayResults(List cars) { - int trialCount = cars.get(0).getTrialCount(); - - for (int i = 0; i < trialCount; i++) { - displayColumn(cars, i); - System.out.println(); - } - } - - private void displayColumn(List cars, int columnIndex) { - for (Car car : cars) { - System.out.printf("%s : ", car.getName()); - System.out.println(getPositionLine(car, columnIndex)); - } + System.out.println(getResults(cars)); } public String getResults(List cars) { diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 77183831207..aae2b27f37e 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -34,8 +34,12 @@ public class RacingTest { ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); System.setIn(in); - // Then + // When inputView.init(); + + // Then + assertThat(inputView.getCarNames()).isEqualTo("pobi,crong,honux"); + assertThat(inputView.getTrialCount()).isEqualTo(5); } @Test @@ -52,15 +56,37 @@ public class RacingTest { @Test void 자동차_출력하기() { // Given - List cars = Arrays.asList( new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), new Car("honux", Arrays.asList(1, 2, 3, 4, 5)) ); + // When + String results = resultView.getResults(cars); + String lineSeparator = System.lineSeparator(); // Then - resultView.displayResults(cars); + assertThat(results).isEqualTo( + "pobi : -" + lineSeparator + + "crong : -" + lineSeparator + + "honux : -" + lineSeparator + + "" + lineSeparator + + "pobi : --" + lineSeparator + + "crong : -" + lineSeparator + + "honux : --" + lineSeparator + + "" + lineSeparator + + "pobi : ---" + lineSeparator + + "crong : --" + lineSeparator + + "honux : ---" + lineSeparator + + "" + lineSeparator + + "pobi : ----" + lineSeparator + + "crong : ---" + lineSeparator + + "honux : ----" + lineSeparator + + "" + lineSeparator + + "pobi : ----" + lineSeparator + + "crong : ----" + lineSeparator + + "honux : -----" + lineSeparator + + "" + lineSeparator); } @Test From 65218a3744f8dac9f22f656923dc32f623880c87 Mon Sep 17 00:00:00 2001 From: prolkh Date: Tue, 9 May 2023 12:48:00 +0900 Subject: [PATCH 06/15] =?UTF-8?q?feat=20:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=205=EC=9E=90=20=EC=B4=88=EA=B3=BC=EC=8B=9C?= =?UTF-8?q?=20=EA=B2=80=EC=A6=9D=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=98?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 7 +++++++ src/test/java/racingcar/RacingTest.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index f21c76b7ef9..28cbabb72eb 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -26,10 +26,17 @@ public class Car { } Car(String name, List positions) { + validateCarName(name); this.name = name; this.positions = new ArrayList<>(positions); } + private void validateCarName(String name) { + if(name.length() > 5) { + throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없다."); + } + } + public int getTrialCount() { return positions.size(); } diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index aae2b27f37e..828b4cc49e5 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -99,6 +99,13 @@ public class RacingTest { assertThat(car.getPositions()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); } + @Test + void 자동차_이름은_5자를_초과할_수_없다() { + assertThatIllegalArgumentException().isThrownBy(() -> { + Car car = new Car("pobibi", Arrays.asList(1, 2, 3, 4, 5)); + }).withMessage("자동차 이름은 5자를 초과할 수 없다."); + } + @Test void 우승자_출력하기() { // Given From 91dc43b1f1b37dee7009b693e9f19e59fdb225bf Mon Sep 17 00:00:00 2001 From: prolkh Date: Tue, 9 May 2023 22:26:14 +0900 Subject: [PATCH 07/15] =?UTF-8?q?test=20:=20proceedOrStop()=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=98?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/RacingTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 828b4cc49e5..c7e7ff2bf8d 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -89,6 +89,19 @@ public class RacingTest { "" + lineSeparator); } + @Test + void proceedOrStop함수는_0_또는_1을_반환한다() { + // Given + Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)); + + // Then + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + } + @Test void Car_객체_테스트() { // Given From 412eba05b498f401afe85aba878529f41bc0567a Mon Sep 17 00:00:00 2001 From: prolkh Date: Sun, 14 May 2023 19:40:24 +0900 Subject: [PATCH 08/15] =?UTF-8?q?refactor=20:=20car=EC=9D=98=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=20=EC=BD=9C=EB=A0=89=EC=85=98=EC=9D=84=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarCollection.java | 32 ++++++++++++++++++++++ src/main/java/racingcar/Racing.java | 21 +++++++------- src/main/java/racingcar/ResultView.java | 16 +++++------ src/test/java/racingcar/RacingTest.java | 8 ++++-- 4 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 src/main/java/racingcar/CarCollection.java diff --git a/src/main/java/racingcar/CarCollection.java b/src/main/java/racingcar/CarCollection.java new file mode 100644 index 00000000000..62646fe20b1 --- /dev/null +++ b/src/main/java/racingcar/CarCollection.java @@ -0,0 +1,32 @@ +package racingcar; + +import java.util.ArrayList; +import java.util.List; + +public class CarCollection { + private final List cars; + + public CarCollection() { + this.cars = new ArrayList<>(); + } + + public CarCollection(List cars) { + this.cars = cars; + } + + public void addCar(Car car) { + cars.add(car); + } + + public void removeCar(Car car) { + cars.remove(car); + } + + public int getCarCount() { + return cars.size(); + } + + public List getCars() { + return new ArrayList<>(cars); + } +} diff --git a/src/main/java/racingcar/Racing.java b/src/main/java/racingcar/Racing.java index 66a4f890f2f..f2e86e16a8e 100644 --- a/src/main/java/racingcar/Racing.java +++ b/src/main/java/racingcar/Racing.java @@ -1,25 +1,24 @@ package racingcar; -import java.util.ArrayList; -import java.util.List; - public class Racing { - private final List cars; +// private final List cars; + private final CarCollection carCollection; Racing(String carNames, int trialCount) { - this.cars = createCars(carNames, trialCount); + carCollection = buildCarCollection(carNames, trialCount); } - private static List createCars(String carNames, int trialCount) { + private static CarCollection buildCarCollection(String carNames, int trialCount) { String[] names = carNames.split(","); - List cars = new ArrayList<>(); + CarCollection carCollection = new CarCollection(); for(String name : names) { - cars.add(new Car(name, trialCount)); + Car car = new Car(name, trialCount); + carCollection.addCar(car); } - return cars; + return carCollection; } - public List getCars() { - return new ArrayList<>(cars); + public CarCollection getCarCollection() { + return carCollection; } } diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index 9f47aeb0b76..45118322669 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -5,29 +5,29 @@ public class ResultView { - public void displayResults(List cars) { - System.out.println(getResults(cars)); + public void displayResults(CarCollection carCollection) { + System.out.println(getResults(carCollection)); } - public String getResults(List cars) { + public String getResults(CarCollection carCollection) { StringBuilder sb = new StringBuilder(); - int trialCount = cars.get(0).getTrialCount(); + int trialCount = carCollection.getCars().get(0).getTrialCount(); for (int i = 0; i < trialCount; i++) { - sb.append(getColumn(cars, i)); + sb.append(getColumn(carCollection, i)); sb.append(System.lineSeparator()); } return sb.toString(); } - private String getColumn(List cars, int columnIndex) { + private String getColumn(CarCollection carCollection, int index) { StringBuilder sb = new StringBuilder(); - for (Car car : cars) { + for (Car car : carCollection.getCars()) { sb.append(car.getName()) .append(" : ") - .append(getPositionLine(car, columnIndex)) + .append(getPositionLine(car, index)) .append(System.lineSeparator()); } diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index c7e7ff2bf8d..4b0485d5d2a 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -24,7 +24,7 @@ public class RacingTest { // Then Racing race = inputView.init(); - resultView.displayResults(race.getCars()); + resultView.displayResults(race.getCarCollection()); } @Test @@ -50,7 +50,7 @@ public class RacingTest { // When // Then - resultView.displayResults(race.getCars()); + resultView.displayResults(race.getCarCollection()); } @Test @@ -61,8 +61,10 @@ public class RacingTest { new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), new Car("honux", Arrays.asList(1, 2, 3, 4, 5)) ); + // When - String results = resultView.getResults(cars); + CarCollection carCollection = new CarCollection(cars); + String results = resultView.getResults(carCollection); String lineSeparator = System.lineSeparator(); // Then From 5f3e106499d60e24bd7c3bae142b30735e4c6cde Mon Sep 17 00:00:00 2001 From: prolkh Date: Sun, 14 May 2023 19:43:46 +0900 Subject: [PATCH 09/15] =?UTF-8?q?refactor=20:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=ED=8F=AC=EB=A7=B7=ED=8C=85=EC=9D=84=20=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 7 ++++--- src/main/java/racingcar/Racing.java | 4 ++-- src/main/java/racingcar/ResultView.java | 5 +++-- src/test/java/racingcar/RacingTest.java | 1 - 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 28cbabb72eb..786cb07fc1b 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -19,7 +19,7 @@ public class Car { this.name = name; positions = new ArrayList<>(); int position = INIT_POSITION; - for(int i=0; i< trialCount; i++) { + for (int i = 0; i < trialCount; i++) { addPosition(position); position += proceedOrStop(); } @@ -32,7 +32,7 @@ public class Car { } private void validateCarName(String name) { - if(name.length() > 5) { + if (name.length() > 5) { throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없다."); } } @@ -46,8 +46,9 @@ public int getPosition(int index) { } public int getLastPosition() { - if(positions.isEmpty()) + if (positions.isEmpty()) { return -1; + } return positions.get(positions.size() - 1); } diff --git a/src/main/java/racingcar/Racing.java b/src/main/java/racingcar/Racing.java index f2e86e16a8e..ca52075a104 100644 --- a/src/main/java/racingcar/Racing.java +++ b/src/main/java/racingcar/Racing.java @@ -1,7 +1,7 @@ package racingcar; public class Racing { -// private final List cars; + // private final List cars; private final CarCollection carCollection; Racing(String carNames, int trialCount) { @@ -11,7 +11,7 @@ public class Racing { private static CarCollection buildCarCollection(String carNames, int trialCount) { String[] names = carNames.split(","); CarCollection carCollection = new CarCollection(); - for(String name : names) { + for (String name : names) { Car car = new Car(name, trialCount); carCollection.addCar(car); } diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index 45118322669..a7f0f36049d 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -51,7 +51,7 @@ private int getBiggestLastPosition(List cars) { for (Car car : cars) { int lastPosition = car.getLastPosition(); - if(max < lastPosition) { + if (max < lastPosition) { max = lastPosition; } } @@ -64,8 +64,9 @@ private String getWinner(List cars, int biggestLastPosition) { for (Car car : cars) { int lastPosition = car.getLastPosition(); - if(lastPosition == biggestLastPosition) + if (lastPosition == biggestLastPosition) { winnerList.add(car.getName()); + } } return String.join(", ", winnerList); diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 4b0485d5d2a..ac854ea0bf5 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -14,7 +14,6 @@ public class RacingTest { public ResultView resultView = new ResultView(); - @Test void 레이싱_전체_테스트() { // Given From 51d24ebe0bff94efa56c8663fdf6f8148569018d Mon Sep 17 00:00:00 2001 From: prolkh Date: Sun, 14 May 2023 20:00:58 +0900 Subject: [PATCH 10/15] =?UTF-8?q?refactor=20:=20inputView=20Test,=20result?= =?UTF-8?q?View=20Test=EB=A5=BC=20=EB=94=B0=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/InputViewTest.java | 28 +++++++++++ src/test/java/racingcar/RacingTest.java | 52 --------------------- src/test/java/racingcar/ResultViewTest.java | 52 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 52 deletions(-) create mode 100644 src/test/java/racingcar/InputViewTest.java create mode 100644 src/test/java/racingcar/ResultViewTest.java diff --git a/src/test/java/racingcar/InputViewTest.java b/src/test/java/racingcar/InputViewTest.java new file mode 100644 index 00000000000..1a067be6662 --- /dev/null +++ b/src/test/java/racingcar/InputViewTest.java @@ -0,0 +1,28 @@ +package racingcar; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class InputViewTest { + + public InputView inputView = new InputView(); + + @Test + void 문자_입력_받기() { + // Given + String testInput = "pobi,crong,honux\n5\n"; + ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); + System.setIn(in); + + // When + inputView.init(); + + // Then + assertThat(inputView.getCarNames()).isEqualTo("pobi,crong,honux"); + assertThat(inputView.getTrialCount()).isEqualTo(5); + } +} \ No newline at end of file diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index ac854ea0bf5..cada8904c33 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -26,20 +26,6 @@ public class RacingTest { resultView.displayResults(race.getCarCollection()); } - @Test - void 문자_입력_받기() { - // Given - String testInput = "pobi,crong,honux\n5\n"; - ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); - System.setIn(in); - - // When - inputView.init(); - - // Then - assertThat(inputView.getCarNames()).isEqualTo("pobi,crong,honux"); - assertThat(inputView.getTrialCount()).isEqualTo(5); - } @Test void 랜덤하게_전진() { @@ -52,44 +38,6 @@ public class RacingTest { resultView.displayResults(race.getCarCollection()); } - @Test - void 자동차_출력하기() { - // Given - List cars = Arrays.asList( - new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), - new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), - new Car("honux", Arrays.asList(1, 2, 3, 4, 5)) - ); - - // When - CarCollection carCollection = new CarCollection(cars); - String results = resultView.getResults(carCollection); - String lineSeparator = System.lineSeparator(); - - // Then - assertThat(results).isEqualTo( - "pobi : -" + lineSeparator + - "crong : -" + lineSeparator + - "honux : -" + lineSeparator + - "" + lineSeparator + - "pobi : --" + lineSeparator + - "crong : -" + lineSeparator + - "honux : --" + lineSeparator + - "" + lineSeparator + - "pobi : ---" + lineSeparator + - "crong : --" + lineSeparator + - "honux : ---" + lineSeparator + - "" + lineSeparator + - "pobi : ----" + lineSeparator + - "crong : ---" + lineSeparator + - "honux : ----" + lineSeparator + - "" + lineSeparator + - "pobi : ----" + lineSeparator + - "crong : ----" + lineSeparator + - "honux : -----" + lineSeparator + - "" + lineSeparator); - } - @Test void proceedOrStop함수는_0_또는_1을_반환한다() { // Given diff --git a/src/test/java/racingcar/ResultViewTest.java b/src/test/java/racingcar/ResultViewTest.java new file mode 100644 index 00000000000..fc8c7fef67d --- /dev/null +++ b/src/test/java/racingcar/ResultViewTest.java @@ -0,0 +1,52 @@ +package racingcar; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class ResultViewTest { + + public ResultView resultView = new ResultView(); + + @Test + void 자동차_출력하기() { + // Given + List cars = Arrays.asList( + new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), + new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), + new Car("honux", Arrays.asList(1, 2, 3, 4, 5)) + ); + + // When + CarCollection carCollection = new CarCollection(cars); + String results = resultView.getResults(carCollection); + String lineSeparator = System.lineSeparator(); + + // Then + assertThat(results).isEqualTo( + "pobi : -" + lineSeparator + + "crong : -" + lineSeparator + + "honux : -" + lineSeparator + + "" + lineSeparator + + "pobi : --" + lineSeparator + + "crong : -" + lineSeparator + + "honux : --" + lineSeparator + + "" + lineSeparator + + "pobi : ---" + lineSeparator + + "crong : --" + lineSeparator + + "honux : ---" + lineSeparator + + "" + lineSeparator + + "pobi : ----" + lineSeparator + + "crong : ---" + lineSeparator + + "honux : ----" + lineSeparator + + "" + lineSeparator + + "pobi : ----" + lineSeparator + + "crong : ----" + lineSeparator + + "honux : -----" + lineSeparator + + "" + lineSeparator); + } +} \ No newline at end of file From 529d809f8f6d0a36690c01ba92cdf677e5fa0679 Mon Sep 17 00:00:00 2001 From: prolkh Date: Sun, 14 May 2023 20:20:13 +0900 Subject: [PATCH 11/15] =?UTF-8?q?refactor=20:=20=EB=A7=A4=EC=A7=81?= =?UTF-8?q?=EB=84=98=EB=B2=84=EB=A5=BC=20=EB=B3=80=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EC=84=A0=EC=96=B8=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 786cb07fc1b..1a575513ca6 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -9,6 +9,7 @@ public class Car { private static final int RANDOM_BOUND = 10; private static final int PROCEED_THRESHOLD = 4; private static final int INIT_POSITION = 1; + public static final int MAX_NAME_LENGTH = 5; private final Random random = new Random(); @@ -32,7 +33,7 @@ public class Car { } private void validateCarName(String name) { - if (name.length() > 5) { + if (name.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없다."); } } From 245d7c16f148e75c1036e39a7e1a89807edd0fac Mon Sep 17 00:00:00 2001 From: prolkh Date: Mon, 15 May 2023 12:02:47 +0900 Subject: [PATCH 12/15] =?UTF-8?q?refactor=20:=20car=EC=9D=98=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=20=EC=BD=9C=EB=A0=89=EC=85=98=EC=9D=84=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EB=8B=A4.=20(2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/ResultView.java | 12 ++++++------ src/test/java/racingcar/RacingTest.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index a7f0f36049d..d6b290d9e20 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -39,16 +39,16 @@ private String getPositionLine(Car car, int columnIndex) { return "-".repeat(car.getPosition(columnIndex)); } - public String getWinnerMessage(List cars) { - String winner = getWinner(cars, getBiggestLastPosition(cars)); + public String getWinnerMessage(CarCollection carCollection) { + String winner = getWinner(carCollection, getBiggestLastPosition(carCollection)); return winner + "가 최종 우승했습니다."; } - private int getBiggestLastPosition(List cars) { + private int getBiggestLastPosition(CarCollection carCollection) { int max = -1; - for (Car car : cars) { + for (Car car : carCollection.getCars()) { int lastPosition = car.getLastPosition(); if (max < lastPosition) { @@ -58,10 +58,10 @@ private int getBiggestLastPosition(List cars) { return max; } - private String getWinner(List cars, int biggestLastPosition) { + private String getWinner(CarCollection carCollection, int biggestLastPosition) { ArrayList winnerList = new ArrayList<>(); - for (Car car : cars) { + for (Car car : carCollection.getCars()) { int lastPosition = car.getLastPosition(); if (lastPosition == biggestLastPosition) { diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index cada8904c33..095dbbbc8ec 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -78,6 +78,7 @@ public class RacingTest { ); // Then - assertThat(resultView.getWinnerMessage(cars)).isEqualTo("pobi, crong가 최종 우승했습니다."); + CarCollection carCollection = new CarCollection(cars); + assertThat(resultView.getWinnerMessage(carCollection)).isEqualTo("pobi, crong가 최종 우승했습니다."); } } From 7d59770582ebfbaedf65679dd82995288924d397 Mon Sep 17 00:00:00 2001 From: prolkh Date: Mon, 15 May 2023 12:55:48 +0900 Subject: [PATCH 13/15] =?UTF-8?q?feat=20:=20main=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=EB=A1=9C=20=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8=EC=9D=B4=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/InputView.java | 17 +++--- src/main/java/racingcar/InputViewDto.java | 24 +++++++++ src/main/java/racingcar/Racing.java | 18 ++++--- src/main/java/racingcar/ResultView.java | 4 ++ src/test/java/racingcar/RacingTest.java | 63 +---------------------- 5 files changed, 50 insertions(+), 76 deletions(-) create mode 100644 src/main/java/racingcar/InputViewDto.java diff --git a/src/main/java/racingcar/InputView.java b/src/main/java/racingcar/InputView.java index 66c7f092e23..a8a4c2d191e 100644 --- a/src/main/java/racingcar/InputView.java +++ b/src/main/java/racingcar/InputView.java @@ -3,17 +3,16 @@ import java.util.Scanner; public class InputView { - private String carNames; - private int trialCount; + private InputViewDto inputViewDto; - public Racing init() { - Scanner scanner = new Scanner(System.in); - carNames = getCarNames(scanner); - trialCount = getTrialCount(scanner); + InputView() { + Scanner scanner = new Scanner(System.in); - return new Racing(carNames, trialCount); + inputViewDto = new InputViewDto(); + inputViewDto.setCarNames(getCarNames(scanner)); + inputViewDto.setTrialCount(getTrialCount(scanner)); } private String getCarNames(Scanner scanner) { @@ -27,10 +26,10 @@ private static Integer getTrialCount(Scanner scanner) { } public String getCarNames() { - return carNames; + return inputViewDto.getCarNames(); } public int getTrialCount() { - return trialCount; + return inputViewDto.getTrialCount(); } } diff --git a/src/main/java/racingcar/InputViewDto.java b/src/main/java/racingcar/InputViewDto.java new file mode 100644 index 00000000000..c49c12ddded --- /dev/null +++ b/src/main/java/racingcar/InputViewDto.java @@ -0,0 +1,24 @@ +package racingcar; + +import java.util.Scanner; + +public class InputViewDto { + private String carNames; + private int trialCount; + + public String getCarNames() { + return carNames; + } + + public void setCarNames(String carNames) { + this.carNames = carNames; + } + + public int getTrialCount() { + return trialCount; + } + + public void setTrialCount(int trialCount) { + this.trialCount = trialCount; + } +} diff --git a/src/main/java/racingcar/Racing.java b/src/main/java/racingcar/Racing.java index ca52075a104..07cd769e66c 100644 --- a/src/main/java/racingcar/Racing.java +++ b/src/main/java/racingcar/Racing.java @@ -2,20 +2,26 @@ public class Racing { // private final List cars; - private final CarCollection carCollection; + private CarCollection carCollection; - Racing(String carNames, int trialCount) { - carCollection = buildCarCollection(carNames, trialCount); + public void main() { + InputView inputView = new InputView(); + ResultView resultView = new ResultView(); + + buildCarCollection(inputView.getCarNames(), inputView.getTrialCount()); + resultView.displayResults(carCollection); + resultView.displayWinnerMessage(carCollection); } - private static CarCollection buildCarCollection(String carNames, int trialCount) { + private void buildCarCollection(String carNames, int trialCount) { + carCollection = new CarCollection(); + String[] names = carNames.split(","); - CarCollection carCollection = new CarCollection(); + for (String name : names) { Car car = new Car(name, trialCount); carCollection.addCar(car); } - return carCollection; } public CarCollection getCarCollection() { diff --git a/src/main/java/racingcar/ResultView.java b/src/main/java/racingcar/ResultView.java index d6b290d9e20..0e7225fe79e 100644 --- a/src/main/java/racingcar/ResultView.java +++ b/src/main/java/racingcar/ResultView.java @@ -39,6 +39,10 @@ private String getPositionLine(Car car, int columnIndex) { return "-".repeat(car.getPosition(columnIndex)); } + public void displayWinnerMessage(CarCollection carCollection) { + System.out.println(getWinnerMessage(carCollection)); + } + public String getWinnerMessage(CarCollection carCollection) { String winner = getWinner(carCollection, getBiggestLastPosition(carCollection)); diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index 095dbbbc8ec..dce30ed1b79 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -10,10 +10,6 @@ public class RacingTest { - public InputView inputView = new InputView(); - public ResultView resultView = new ResultView(); - - @Test void 레이싱_전체_테스트() { // Given @@ -21,64 +17,9 @@ public class RacingTest { ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes()); System.setIn(in); - // Then - Racing race = inputView.init(); - resultView.displayResults(race.getCarCollection()); - } - - - @Test - void 랜덤하게_전진() { - // Given - Racing race = new Racing("pobi,crong,honux", 5); - // When - - // Then - resultView.displayResults(race.getCarCollection()); - } - - @Test - void proceedOrStop함수는_0_또는_1을_반환한다() { - // Given - Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)); - - // Then - assertThat(car.proceedOrStop()).isIn(0, 1); - assertThat(car.proceedOrStop()).isIn(0, 1); - assertThat(car.proceedOrStop()).isIn(0, 1); - assertThat(car.proceedOrStop()).isIn(0, 1); - assertThat(car.proceedOrStop()).isIn(0, 1); - } - - @Test - void Car_객체_테스트() { - // Given - Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 5)); - - // Then - assertThat(car.getName()).isEqualTo("pobi"); - assertThat(car.getPositions()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); - } - - @Test - void 자동차_이름은_5자를_초과할_수_없다() { - assertThatIllegalArgumentException().isThrownBy(() -> { - Car car = new Car("pobibi", Arrays.asList(1, 2, 3, 4, 5)); - }).withMessage("자동차 이름은 5자를 초과할 수 없다."); - } - - @Test - void 우승자_출력하기() { - // Given - List cars = Arrays.asList( - new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), - new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), - new Car("honux", Arrays.asList(1, 2, 3, 3, 3)) - ); - // Then - CarCollection carCollection = new CarCollection(cars); - assertThat(resultView.getWinnerMessage(carCollection)).isEqualTo("pobi, crong가 최종 우승했습니다."); + Racing racing = new Racing(); + racing.main(); } } From d74cf35f8b36c3690a27cf62324ae536fed8b343 Mon Sep 17 00:00:00 2001 From: prolkh Date: Mon, 15 May 2023 12:56:40 +0900 Subject: [PATCH 14/15] =?UTF-8?q?refactor=20:=20test=EB=A5=BC=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/CarTest.java | 41 +++++++++++++++++++++ src/test/java/racingcar/InputViewTest.java | 5 +-- src/test/java/racingcar/ResultViewTest.java | 15 ++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/test/java/racingcar/CarTest.java diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java new file mode 100644 index 00000000000..2ed7a68b7e2 --- /dev/null +++ b/src/test/java/racingcar/CarTest.java @@ -0,0 +1,41 @@ +package racingcar; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.jupiter.api.Assertions.*; + +class CarTest { + @Test + void proceedOrStop함수는_0_또는_1을_반환한다() { + // Given + Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)); + + // Then + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + assertThat(car.proceedOrStop()).isIn(0, 1); + } + + @Test + void Car_객체_테스트() { + // Given + Car car = new Car("pobi", Arrays.asList(1, 2, 3, 4, 5)); + + // Then + assertThat(car.getName()).isEqualTo("pobi"); + assertThat(car.getPositions()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); + } + + @Test + void 자동차_이름은_5자를_초과할_수_없다() { + assertThatIllegalArgumentException().isThrownBy(() -> { + Car car = new Car("pobibi", Arrays.asList(1, 2, 3, 4, 5)); + }).withMessage("자동차 이름은 5자를 초과할 수 없다."); + } +} \ No newline at end of file diff --git a/src/test/java/racingcar/InputViewTest.java b/src/test/java/racingcar/InputViewTest.java index 1a067be6662..c7cb6d89301 100644 --- a/src/test/java/racingcar/InputViewTest.java +++ b/src/test/java/racingcar/InputViewTest.java @@ -5,11 +5,10 @@ import java.io.ByteArrayInputStream; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; class InputViewTest { - public InputView inputView = new InputView(); + public InputView inputView; @Test void 문자_입력_받기() { @@ -19,7 +18,7 @@ class InputViewTest { System.setIn(in); // When - inputView.init(); + inputView = new InputView(); // Then assertThat(inputView.getCarNames()).isEqualTo("pobi,crong,honux"); diff --git a/src/test/java/racingcar/ResultViewTest.java b/src/test/java/racingcar/ResultViewTest.java index fc8c7fef67d..bc296ee928b 100644 --- a/src/test/java/racingcar/ResultViewTest.java +++ b/src/test/java/racingcar/ResultViewTest.java @@ -49,4 +49,19 @@ class ResultViewTest { "honux : -----" + lineSeparator + "" + lineSeparator); } + + + @Test + void 우승자_출력하기() { + // Given + List cars = Arrays.asList( + new Car("pobi", Arrays.asList(1, 2, 3, 4, 4)), + new Car("crong", Arrays.asList(1, 1, 2, 3, 4)), + new Car("honux", Arrays.asList(1, 2, 3, 3, 3)) + ); + + // Then + CarCollection carCollection = new CarCollection(cars); + assertThat(resultView.getWinnerMessage(carCollection)).isEqualTo("pobi, crong가 최종 우승했습니다."); + } } \ No newline at end of file From 2cef32f867d64e986edf54d6589050c36b042150 Mon Sep 17 00:00:00 2001 From: prolkh Date: Mon, 15 May 2023 12:59:17 +0900 Subject: [PATCH 15/15] =?UTF-8?q?refactor=20:=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=9C=84=EC=B9=98=EB=A5=BC=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=98=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/RacingTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/racingcar/RacingTest.java b/src/test/java/racingcar/RacingTest.java index dce30ed1b79..d29c44a375c 100644 --- a/src/test/java/racingcar/RacingTest.java +++ b/src/test/java/racingcar/RacingTest.java @@ -10,6 +10,8 @@ public class RacingTest { + private Racing racing = new Racing(); + @Test void 레이싱_전체_테스트() { // Given @@ -19,7 +21,6 @@ public class RacingTest { // When // Then - Racing racing = new Racing(); racing.main(); } }