From a5fe8cee1d8fc36b43436ac5c9a38f428f010c36 Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Thu, 3 Oct 2024 16:18:58 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat:=201,=202=20=EB=8B=A8=EA=B3=84=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test: 관련 테스트 코드 작성 --- src/main/java/.gitkeep | 0 src/main/java/Car.java | 39 +++++++++++++++++++++++++++++++++++++++ src/test/java/.gitkeep | 0 3 files changed, 39 insertions(+) delete mode 100644 src/main/java/.gitkeep create mode 100644 src/main/java/Car.java delete mode 100644 src/test/java/.gitkeep diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..8ba6cf9e --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,39 @@ +import java.util.Comparator; +import java.util.Random; +import javax.management.RuntimeErrorException; + +public class Car{ + + public int pos=0; + public int carId; + public int rank; + private int RAND_MAX = 9; + + + public Car(int Id) { + this.pos = 0; + this.carId = Id; + } + + public void move() { + int rand = getRandNum(); + go(rand); + } + + public void go(int num) { + if (num < 3) { + pos +=1; + return; + } + if (num < 9) { + return; + } + throw new RuntimeException("예상치 못한 랜덤값 입력"); + } + + public int getRandNum() { + Random random = new Random(); + int randNum = random.nextInt(RAND_MAX); + return randNum; + } +} diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep deleted file mode 100644 index e69de29b..00000000 From 77ecf636512a1fd5bb50fa454991f243eb38b737 Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Thu, 10 Oct 2024 12:11:17 +0900 Subject: [PATCH 2/8] =?UTF-8?q?Docs:=20README=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=ED=8C=8C=EC=9D=BC=EA=B5=AC=EC=A1=B0=20=EC=84=B8?= =?UTF-8?q?=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++++++++++++ src/main/java/Car.java | 39 --------------------------------------- 2 files changed, 20 insertions(+), 39 deletions(-) create mode 100644 README.md delete mode 100644 src/main/java/Car.java diff --git a/README.md b/README.md new file mode 100644 index 00000000..578d5f24 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# 자동차 경주 + +## 1단계 - 움직이는 자동차 + - [ ] 자동차는 이름을 가지고 있다. + - [x] 자동차는 움직일 수 있다. + - [X] 0에서 9 사이에서 random 값을 구한 후 random 값이 4 이상일 경우 전진하고, 3 이하의 값이면 멈춘다. + +## 2단계 - 우승 자동차 구하기 + - [X] n대의 자동차가 참여할 수 있다. + - [X] 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. + - [X] 0에서 9 사이에서 random 값을 구한 후 random 값이 4 이상일 경우 전진하고, 3 이하의 값이면 멈춘다. + - [X] 자동차 경주 게임을 완료한 후 누가 우승했는지를 구할 수 있다. 우승자는 한 명 이상일 수 있다. + +## 3단계 - 게임 실행 + - [ ] 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. + - [ ] 각 자동차에 이름을 부여할 수 있다. 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다. + - [ ] 자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다. + - [ ] 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. + - [ ] 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4 이상일 경우 전진하고, 3 이하의 값이면 멈춘다. + - [ ] 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다. \ No newline at end of file diff --git a/src/main/java/Car.java b/src/main/java/Car.java deleted file mode 100644 index 8ba6cf9e..00000000 --- a/src/main/java/Car.java +++ /dev/null @@ -1,39 +0,0 @@ -import java.util.Comparator; -import java.util.Random; -import javax.management.RuntimeErrorException; - -public class Car{ - - public int pos=0; - public int carId; - public int rank; - private int RAND_MAX = 9; - - - public Car(int Id) { - this.pos = 0; - this.carId = Id; - } - - public void move() { - int rand = getRandNum(); - go(rand); - } - - public void go(int num) { - if (num < 3) { - pos +=1; - return; - } - if (num < 9) { - return; - } - throw new RuntimeException("예상치 못한 랜덤값 입력"); - } - - public int getRandNum() { - Random random = new Random(); - int randNum = random.nextInt(RAND_MAX); - return randNum; - } -} From 4e22499f76135c9423d5d59e3aeaad4fbe95118f Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Sat, 12 Oct 2024 04:50:29 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 +++++++++++++++++- src/main/java/Service/FixedNumGenerator.java | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Service/FixedNumGenerator.java diff --git a/README.md b/README.md index 578d5f24..41b545a0 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,20 @@ - [ ] 자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다. - [ ] 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. - [ ] 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4 이상일 경우 전진하고, 3 이하의 값이면 멈춘다. - - [ ] 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다. \ No newline at end of file + - [ ] 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다. + + + +## 막히는 부분? + - Input시 예외처리.. + InputView에서 하는가? Serive에서 하는가. + 에러 핸들링은 어디서 하는가.. + + - Dto를 제작해야 할까? + 현재 코드에선 car.move가 public임 + 하지만 outView에서는 이 메소드에 접근하지 않으므로 CarList를 넘기는 것보다 RoundResultDto를 넘기는 것이 맞다 판단.. + + - 게임의 N번 실행은 Controller의 역할? RacingGame의 역할? + 뭔가 RacingGame에서 N번 라운드를 실행해야 할 것 같다. + 그러면 OutputView가 RacingGame의 라운드 결과를 출력하는 과정이 매끄럽지 않아진다. + * OutputView가 RacingGame 안에서 동작해야 함 -> contoller의 역할이 애매해진다. \ No newline at end of file diff --git a/src/main/java/Service/FixedNumGenerator.java b/src/main/java/Service/FixedNumGenerator.java new file mode 100644 index 00000000..67464548 --- /dev/null +++ b/src/main/java/Service/FixedNumGenerator.java @@ -0,0 +1,14 @@ +package Service; + +public class FixedNumGenerator implements NumGenerator{ + + private int num; + + public FixedNumGenerator (final int num) { + this.num = num; + } + @Override + public int createNumber() { + return num; + } +} From bd8257e9f811c5235f5e284d4abdd15835951022 Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Sat, 12 Oct 2024 13:09:46 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++++++- .../java/{Service => service}/FixedNumGenerator.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) rename src/main/java/{Service => service}/FixedNumGenerator.java (92%) diff --git a/README.md b/README.md index 41b545a0..0a6a92d3 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ - [ ] 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다. +## 바꾸자 + 선언할때 값을 할당 안해도 final를 넣을 수 있다..! ## 막히는 부분? - Input시 예외처리.. @@ -33,4 +35,9 @@ - 게임의 N번 실행은 Controller의 역할? RacingGame의 역할? 뭔가 RacingGame에서 N번 라운드를 실행해야 할 것 같다. 그러면 OutputView가 RacingGame의 라운드 결과를 출력하는 과정이 매끄럽지 않아진다. - * OutputView가 RacingGame 안에서 동작해야 함 -> contoller의 역할이 애매해진다. \ No newline at end of file + * OutputView가 RacingGame 안에서 동작해야 함 -> contoller의 역할이 애매해진다. + + - Model(Domain)은 메소드를 안 가지는 것이 좋은가? + 뭔가 도메인이 해야하는 일들은 서비스에서 처리하는 것이 더욱 깔끔할 것 같다.. + + - vaildator를 넣으면 특정 객체에 대한 탐색을 두번하게 된다 -> 비효율적인가? diff --git a/src/main/java/Service/FixedNumGenerator.java b/src/main/java/service/FixedNumGenerator.java similarity index 92% rename from src/main/java/Service/FixedNumGenerator.java rename to src/main/java/service/FixedNumGenerator.java index 67464548..2a8a1186 100644 --- a/src/main/java/Service/FixedNumGenerator.java +++ b/src/main/java/service/FixedNumGenerator.java @@ -1,4 +1,4 @@ -package Service; +package service; public class FixedNumGenerator implements NumGenerator{ From 5f81c06690224b8715c3d9942713539508958cff Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Sat, 12 Oct 2024 13:11:03 +0900 Subject: [PATCH 5/8] =?UTF-8?q?fix:=20=EC=BB=A4=EB=B0=8B=20=EB=88=84?= =?UTF-8?q?=EB=9D=BD=20=ED=8C=8C=EC=9D=BC=20=EC=9E=AC=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 11 ++++ src/main/java/Model/Car.java | 45 ++++++++++++++ src/main/java/Model/RacingGame.java | 61 +++++++++++++++++++ src/main/java/controller/GameController.java | 35 +++++++++++ src/main/java/service/NumGenerator.java | 5 ++ src/main/java/service/RandomNumGenerator.java | 18 ++++++ src/main/java/validator/ErrorStatus.java | 17 ++++++ src/main/java/validator/Validator.java | 36 +++++++++++ src/main/java/view/InputView.java | 42 +++++++++++++ src/main/java/view/OutputView.java | 47 ++++++++++++++ 10 files changed, 317 insertions(+) create mode 100644 src/main/java/Application.java create mode 100644 src/main/java/Model/Car.java create mode 100644 src/main/java/Model/RacingGame.java create mode 100644 src/main/java/controller/GameController.java create mode 100644 src/main/java/service/NumGenerator.java create mode 100644 src/main/java/service/RandomNumGenerator.java create mode 100644 src/main/java/validator/ErrorStatus.java create mode 100644 src/main/java/validator/Validator.java create mode 100644 src/main/java/view/InputView.java create mode 100644 src/main/java/view/OutputView.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..c6ce52b6 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,11 @@ +import controller.GameController; + +public class Application { + + + public static void main(String[] args) { + final GameController gameController = new GameController(); + + gameController.start(); + } +} diff --git a/src/main/java/Model/Car.java b/src/main/java/Model/Car.java new file mode 100644 index 00000000..4ba1357d --- /dev/null +++ b/src/main/java/Model/Car.java @@ -0,0 +1,45 @@ +package Model; + +import service.NumGenerator; +import service.RandomNumGenerator; + + +public class Car { + private int pos=0; + private String name; + private NumGenerator numGenerator = new RandomNumGenerator(); + + public int getPos() { + return this.pos; + } + + public void setPos(int pos) { + this.pos = pos; + } + + public String getName() { + return this.name; + } + + public Car (String name) { + numGenerator = new RandomNumGenerator(); + this.name = name; + } + + public Car (String name, NumGenerator numGenerator) { + this.numGenerator = numGenerator; + this.name = name; + } + + public void move() { + int num = numGenerator.createNumber(); + if (num > 9 || num < 0) { + throw new RuntimeException("생성된 숫자가 정해진 범위를 초과합니다."); + } + + else if (num >= 4) { + pos += 1; + } + } + +} diff --git a/src/main/java/Model/RacingGame.java b/src/main/java/Model/RacingGame.java new file mode 100644 index 00000000..dddc2752 --- /dev/null +++ b/src/main/java/Model/RacingGame.java @@ -0,0 +1,61 @@ +package Model; + +import java.util.ArrayList; +import java.util.List; + +public class RacingGame { + + private int maxPos = 0; + + private final List carList = new ArrayList<>(); + + + public void createCarList(List nameList) { + for (String name : nameList) { + carList.add(new Car(name)); + } + } + + private void getMaxPos() { + int maxPos = 0; + for (Car car : carList) { + maxPos = Math.max( maxPos, car.getPos() ); + } + + this.maxPos = maxPos; + } + + private boolean isWinner(Car car) { + if (maxPos == car.getPos()) { + return true; + } + else { + return false; + } + } + + public void run() { + for (Car car : carList) { + car.move(); + } + } + + public List getWinnerList() { + List winnerList = new ArrayList<>(); + + getMaxPos(); + + winnerList = carList.stream() + .filter( car -> isWinner(car) ) + .map(Car::getName) + .toList(); + + return winnerList; + + } + + public List getCarList() { + return carList; + } + +} diff --git a/src/main/java/controller/GameController.java b/src/main/java/controller/GameController.java new file mode 100644 index 00000000..8e36279b --- /dev/null +++ b/src/main/java/controller/GameController.java @@ -0,0 +1,35 @@ +package controller; + +import Model.RacingGame; +import validator.Validator; +import view.InputView; +import view.OutputView; +import java.util.List; + + +public class GameController { + private final RacingGame racingGame= new RacingGame(); + + private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + private final Validator validator = new Validator(); + + public void start() { + + List nameList = inputView.inputNameList(); + validator.validName(nameList); + + racingGame.createCarList(nameList); + + int round = inputView.inputAttempt(); + validator.validRound(round); + + outputView.printRoundResultInfo(); + for (int i=1; i<=round; i++) { + racingGame.run(); + outputView.printRoundResult( racingGame.getCarList() ); + } + + outputView.printWinner( racingGame.getWinnerList() ); + } +} diff --git a/src/main/java/service/NumGenerator.java b/src/main/java/service/NumGenerator.java new file mode 100644 index 00000000..6b9c89de --- /dev/null +++ b/src/main/java/service/NumGenerator.java @@ -0,0 +1,5 @@ +package service; + +public interface NumGenerator { + int createNumber(); +} diff --git a/src/main/java/service/RandomNumGenerator.java b/src/main/java/service/RandomNumGenerator.java new file mode 100644 index 00000000..1fcf5ad4 --- /dev/null +++ b/src/main/java/service/RandomNumGenerator.java @@ -0,0 +1,18 @@ +package service; + +import java.util.Random; + +public class RandomNumGenerator implements NumGenerator { + + private final int RAND_MAX = 10; + private final int RAND_MIN = 0; + + @Override + public int createNumber() { + Random random = new Random(); + return random.nextInt(0, 10); + } + + + +} diff --git a/src/main/java/validator/ErrorStatus.java b/src/main/java/validator/ErrorStatus.java new file mode 100644 index 00000000..a4775a86 --- /dev/null +++ b/src/main/java/validator/ErrorStatus.java @@ -0,0 +1,17 @@ +package validator; + +public enum ErrorStatus { + INVAILD_ROUND_ERROR("시도할 횟수가 유효하지 않습니다."), + EMPTY_NAME_ERROR("이름에 빈값이 포함되어 있습니다."), + DUPLICATE_NAME_ERROR("중복된 이름이 존재합니다."), + NOT_FOUND_NAME_ERROR ("참가자가 존재하지 않습니다."); + private final String reason; + + private ErrorStatus(String reason) { + this.reason = reason; + } + + public String getReason() { + return reason; + } +} diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java new file mode 100644 index 00000000..cf727797 --- /dev/null +++ b/src/main/java/validator/Validator.java @@ -0,0 +1,36 @@ +package validator; + +import java.util.List; +import java.util.Set; + +public class Validator { + + public void validRound(int round) { + if (round <= 0 ) { + throw new RuntimeException(ErrorStatus.INVAILD_ROUND_ERROR.getReason()); + } + } + + public void validName(List nameList) { + for (String name : nameList) { + vaildEmpty(name); + } + + if ( nameList.isEmpty() ) { + throw new RuntimeException(ErrorStatus.NOT_FOUND_NAME_ERROR.getReason()); + } + + Set set = Set.copyOf(nameList); + if ( set.size() != nameList.size() ) { + throw new RuntimeException(ErrorStatus.DUPLICATE_NAME_ERROR.getReason()); + } + + + } + + private void vaildEmpty(String str) { + if ( str.isBlank() ) + throw new RuntimeException(ErrorStatus.EMPTY_NAME_ERROR.getReason()); + } + +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000..8e5f817f --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,42 @@ +package view; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class InputView { + private final String INPUT_NAME_MESSAGE = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; + private final String INPUT_MAX_ATTEMPT_MESSAGE = "시도할 회수는 몇회인가요?"; + + public List inputNameList() { + + // 스캐너 선언 + Scanner scanner = new Scanner(System.in); + + // 자동차 이름 입력받기 + System.out.println(INPUT_NAME_MESSAGE); + String str = scanner.nextLine(); + + // nameList 반환 (공백제거 및 split) + List nameList = new ArrayList<>(); + nameList = Arrays.stream(str.replaceAll(" ", "").split(",")).toList(); + + return nameList; + } + + public int inputAttempt() { + + // 스캐너 선언 + Scanner scanner = new Scanner(System.in); + + // 시도횟수 입력받기 + System.out.println(INPUT_MAX_ATTEMPT_MESSAGE); + int attempt = scanner.nextInt(); + + // 시도횟수 반환 + return attempt; + + } + +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 00000000..af6749e9 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,47 @@ +package view; + +import Model.Car; +import java.util.List; + +public class OutputView { + + private final String ROUND_RESULT_INFO_MESSAGE = "실행결과"; + private final String WINNER_RESULT_MESSAGE = "가 최종 우승했습니다."; + + private void print(String string) { + System.out.println(string); + } + + public void printRoundResultInfo() { + print(ROUND_RESULT_INFO_MESSAGE); + } + public void printRoundResult(List CarList) { + for (Car car : CarList) { + String Result = car.getName() + " : " + posBar( car.getPos() ); + print(Result); + } + print(""); + } + + private String posBar(int pos) { + String str = ""; + for (int i=0; i winnerList) { + String str = ""; + + str += winnerList.get(0); + + for (int i=1; i Date: Sat, 12 Oct 2024 20:28:35 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20Dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/controller/GameController.java | 4 +-- src/main/java/{Model => model}/Car.java | 2 +- .../java/{Model => model}/RacingGame.java | 27 ++++++++++++++++--- src/main/java/view/OutputView.java | 16 +++++++---- 5 files changed, 39 insertions(+), 13 deletions(-) rename src/main/java/{Model => model}/Car.java (98%) rename src/main/java/{Model => model}/RacingGame.java (61%) diff --git a/README.md b/README.md index 0a6a92d3..1766e743 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ - Input시 예외처리.. InputView에서 하는가? Serive에서 하는가. 에러 핸들링은 어디서 하는가.. + 입력을 받자마자 바로 처리하는게 좋은 것인지, 코드상에서 에러가 나기 시작하는 지점에서 예외처리를 하는게 좋은 것인지.. - Dto를 제작해야 할까? 현재 코드에선 car.move가 public임 @@ -37,7 +38,7 @@ 그러면 OutputView가 RacingGame의 라운드 결과를 출력하는 과정이 매끄럽지 않아진다. * OutputView가 RacingGame 안에서 동작해야 함 -> contoller의 역할이 애매해진다. - - Model(Domain)은 메소드를 안 가지는 것이 좋은가? + - model(Domain)은 메소드를 안 가지는 것이 좋은가? 뭔가 도메인이 해야하는 일들은 서비스에서 처리하는 것이 더욱 깔끔할 것 같다.. - vaildator를 넣으면 특정 객체에 대한 탐색을 두번하게 된다 -> 비효율적인가? diff --git a/src/main/java/controller/GameController.java b/src/main/java/controller/GameController.java index 8e36279b..03c824d9 100644 --- a/src/main/java/controller/GameController.java +++ b/src/main/java/controller/GameController.java @@ -1,6 +1,6 @@ package controller; -import Model.RacingGame; +import model.RacingGame; import validator.Validator; import view.InputView; import view.OutputView; @@ -27,7 +27,7 @@ public void start() { outputView.printRoundResultInfo(); for (int i=1; i<=round; i++) { racingGame.run(); - outputView.printRoundResult( racingGame.getCarList() ); + outputView.printRoundResult( racingGame.getRoundResult() ); } outputView.printWinner( racingGame.getWinnerList() ); diff --git a/src/main/java/Model/Car.java b/src/main/java/model/Car.java similarity index 98% rename from src/main/java/Model/Car.java rename to src/main/java/model/Car.java index 4ba1357d..2402fcf8 100644 --- a/src/main/java/Model/Car.java +++ b/src/main/java/model/Car.java @@ -1,4 +1,4 @@ -package Model; +package model; import service.NumGenerator; import service.RandomNumGenerator; diff --git a/src/main/java/Model/RacingGame.java b/src/main/java/model/RacingGame.java similarity index 61% rename from src/main/java/Model/RacingGame.java rename to src/main/java/model/RacingGame.java index dddc2752..fa97b77b 100644 --- a/src/main/java/Model/RacingGame.java +++ b/src/main/java/model/RacingGame.java @@ -1,5 +1,7 @@ -package Model; +package model; +import Dto.GameResultDto; +import Dto.RoundResultDto; import java.util.ArrayList; import java.util.List; @@ -40,7 +42,7 @@ public void run() { } } - public List getWinnerList() { + public GameResultDto getWinnerList() { List winnerList = new ArrayList<>(); getMaxPos(); @@ -50,12 +52,29 @@ public List getWinnerList() { .map(Car::getName) .toList(); - return winnerList; - + return new GameResultDto().builder() + .name(winnerList) + .build(); } public List getCarList() { return carList; } + public List getRoundResult() { + + List roundResultDtoList = new ArrayList<>(); + + for(Car car : carList){ + RoundResultDto roundResultDto = new RoundResultDto().builder() + .name(car.getName()) + .pos(car.getPos()) + .build(); + + roundResultDtoList.add(roundResultDto); + } + + return roundResultDtoList; + } + } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index af6749e9..aa9f6f03 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,6 +1,8 @@ package view; -import Model.Car; +import Dto.GameResultDto; +import Dto.RoundResultDto; +import model.Car; import java.util.List; public class OutputView { @@ -15,14 +17,16 @@ private void print(String string) { public void printRoundResultInfo() { print(ROUND_RESULT_INFO_MESSAGE); } - public void printRoundResult(List CarList) { - for (Car car : CarList) { - String Result = car.getName() + " : " + posBar( car.getPos() ); + public void printRoundResult(List results) { + for (RoundResultDto result : results) { + String Result = result.getName() + " : " + posBar( result.getPos() ); print(Result); } print(""); } + + private String posBar(int pos) { String str = ""; for (int i=0; i winnerList) { + public void printWinner(GameResultDto result) { + + List winnerList = result.getNameList(); String str = ""; str += winnerList.get(0); From ec2c76dc3e621bfbf3b521f379154baf003b9859 Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Sat, 12 Oct 2024 20:28:54 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20Dto=20=EC=B6=94=EA=B0=80(=EC=9E=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Dto/GameResultDto.java | 35 ++++++++++++ src/main/java/Dto/RoundResultDto.java | 49 +++++++++++++++++ src/test/java/CarTest.java | 76 +++++++++++++++++++++++++++ src/test/java/GameTest.java | 57 ++++++++++++++++++++ 4 files changed, 217 insertions(+) create mode 100644 src/main/java/Dto/GameResultDto.java create mode 100644 src/main/java/Dto/RoundResultDto.java create mode 100644 src/test/java/CarTest.java create mode 100644 src/test/java/GameTest.java diff --git a/src/main/java/Dto/GameResultDto.java b/src/main/java/Dto/GameResultDto.java new file mode 100644 index 00000000..a29346a8 --- /dev/null +++ b/src/main/java/Dto/GameResultDto.java @@ -0,0 +1,35 @@ +package Dto; + +import java.util.List; + +public class GameResultDto { + + private List nameList; + + public GameResultDto() { + } + + public GameResultDto(Builder builder) { + this.nameList = builder.nameList; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private List nameList; + public Builder name(List nameList) { + this.nameList = nameList; + return this; + } + + public GameResultDto build() { + return new GameResultDto(this); + } + + } + + public List getNameList() { + return this.nameList; + } diff --git a/src/main/java/Dto/RoundResultDto.java b/src/main/java/Dto/RoundResultDto.java new file mode 100644 index 00000000..b5e0e9f7 --- /dev/null +++ b/src/main/java/Dto/RoundResultDto.java @@ -0,0 +1,49 @@ +package Dto; + +import java.util.List; + +public class RoundResultDto { + + private String name; + private int pos; + + public RoundResultDto() { + } + + public RoundResultDto(Builder builder) { + this.name = builder.name; + this.pos = builder.pos; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String name; + private int pos; + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder pos(int pos) { + this.pos = pos; + return this; + } + + public RoundResultDto build() { + return new RoundResultDto(this); + } + + } + + public String getName() { + return this.name; + } + + public int getPos() { + return this.pos; + } +} diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java new file mode 100644 index 00000000..8ec40227 --- /dev/null +++ b/src/test/java/CarTest.java @@ -0,0 +1,76 @@ +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; + + +import model.Car; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import service.FixedNumGenerator; + +public class CarTest { + + @Nested + @DisplayName("생성테스트") + public class createTest { + + @Test + void 차량은_이름을_가진다() { + final String name = "steve"; + final Car car = new Car(name); + + assertThat(car.getName()).isEqualTo(name); + } + } + + + @Nested + @DisplayName("움직임테스트") + public class moveTest { + + @Test + @DisplayName("전진테스트") + public void 4_이상의_수는_차량을_전진시킨다() { + + final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(3); + + final Car car = new Car("steve", fixedNumGenerator); + + car.move(); + + int expect = 0; + int actual = car.getPos(); + + assertThat(actual).isEqualTo(expect); + } + + @Test + @DisplayName("멈춤테스트") + public void stopTest() { + + final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(3); + final Car car = new Car("steve", fixedNumGenerator); + + car.move(); + + int expect = 0; + int actual = car.getPos(); + + assertThat(actual).isEqualTo(expect); + } + + @Test + @DisplayName("예외테스트") + public void exceptionTest() { + + final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(10); + final Car car = new Car("steve", fixedNumGenerator); + + car.move(); + + assertThatThrownBy(() -> car.move()) + .isInstanceOf(RuntimeException.class); + + } + } +} diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java new file mode 100644 index 00000000..4724d5cf --- /dev/null +++ b/src/test/java/GameTest.java @@ -0,0 +1,57 @@ +import static org.assertj.core.api.Assertions.*; + +import java.util.List; +import model.Car; +import model.RacingGame; +import org.junit.jupiter.api.Test; + +public class GameTest { + + @Test + public void 차량을_원하는_수만큼_생성시킨다() { + final RacingGame racingGame = new RacingGame(); + + List nameList = List.of("Steve", "Alex", "James"); + + racingGame.createCarList(nameList); + List actual = racingGame.getCarList().stream().map(Car::getName).toList(); + + assertThat(actual).containsExactlyInAnyOrderElementsOf( nameList ); + } + + @Test + public void 차량은_N번의_라운드동안_움직일_수_있다() { + + } + + @Test + public void 라운드_결과를_출력한다() { + + } + + @Test + public void 승자를_출력한다() { + + } + + @Test + public void 참가하는_차량이_없다면_예외를_던진다() { + + } + + @Test + public void 차량이름이_비었다면_예외를_던진다() { + + } + + @Test + public void 차량이름이_중복이라면_예외를_던진다() { + + } + + @Test + public void 시도횟수가_음수라면_예외를_던진다() { + + } + +} From 209d1f5c1b05e3f05fc3122cd0b5103ed7796a19 Mon Sep 17 00:00:00 2001 From: LHC0312 Date: Sun, 13 Oct 2024 04:58:08 +0900 Subject: [PATCH 8/8] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++ src/main/java/Dto/GameResultDto.java | 3 ++ src/test/java/CarTest.java | 9 ++-- src/test/java/GameTest.java | 61 ++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1766e743..cc469ea2 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,6 @@ 뭔가 도메인이 해야하는 일들은 서비스에서 처리하는 것이 더욱 깔끔할 것 같다.. - vaildator를 넣으면 특정 객체에 대한 탐색을 두번하게 된다 -> 비효율적인가? + + - 출력되는 메세지는 어떻게 테스트하지? + 애초에 테스트를 해야하나?? \ No newline at end of file diff --git a/src/main/java/Dto/GameResultDto.java b/src/main/java/Dto/GameResultDto.java index a29346a8..12ab9ab4 100644 --- a/src/main/java/Dto/GameResultDto.java +++ b/src/main/java/Dto/GameResultDto.java @@ -18,7 +18,9 @@ public static Builder builder() { } public static class Builder { + private List nameList; + public Builder name(List nameList) { this.nameList = nameList; return this; @@ -33,3 +35,4 @@ public GameResultDto build() { public List getNameList() { return this.nameList; } +} diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java index 8ec40227..4a35fec4 100644 --- a/src/test/java/CarTest.java +++ b/src/test/java/CarTest.java @@ -29,8 +29,7 @@ public class createTest { public class moveTest { @Test - @DisplayName("전진테스트") - public void 4_이상의_수는_차량을_전진시킨다() { + public void 수가_4_이상일_때_전진한다() { final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(3); @@ -45,8 +44,7 @@ public class moveTest { } @Test - @DisplayName("멈춤테스트") - public void stopTest() { + public void 수가_4_미만일_때_멈춘다() { final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(3); final Car car = new Car("steve", fixedNumGenerator); @@ -60,8 +58,7 @@ public void stopTest() { } @Test - @DisplayName("예외테스트") - public void exceptionTest() { + public void 수가_잘못된_범위라면_예외를_던진다() { final FixedNumGenerator fixedNumGenerator = new FixedNumGenerator(10); final Car car = new Car("steve", fixedNumGenerator); diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 4724d5cf..88501496 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -1,9 +1,19 @@ import static org.assertj.core.api.Assertions.*; +import Dto.GameResultDto; +import Dto.RoundResultDto; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.ArrayList; import java.util.List; import model.Car; import model.RacingGame; import org.junit.jupiter.api.Test; +import validator.Validator; +import java.io.ByteArrayInputStream; +import view.InputView; +import view.OutputView; public class GameTest { @@ -20,38 +30,81 @@ public class GameTest { } @Test - public void 차량은_N번의_라운드동안_움직일_수_있다() { + public void 라운드_결과를_출력한다() { + final OutputView outputView = new OutputView(); + final List result = new ArrayList<>(); - } + result.add( + new RoundResultDto().builder() + .name("Steve") + .pos(5) + .build() + ); - @Test - public void 라운드_결과를_출력한다() { + String expected = "Steve : -----\n\n"; + + OutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + + outputView.printRoundResult( result ); + assertThat(out.toString()).isEqualTo(expected); } @Test public void 승자를_출력한다() { + final OutputView outputView = new OutputView(); + final GameResultDto result = new GameResultDto().builder() + .name( List.of("Steve", "Alex") ) + .build(); + String expected = "Steve, Alex가 최종 우승했습니다.\n"; + + OutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + + outputView.printWinner( result ); + + assertThat(out.toString()).isEqualTo(expected); } @Test public void 참가하는_차량이_없다면_예외를_던진다() { + final Validator validator = new Validator(); + List nameList = List.of(); + + assertThatThrownBy( () -> validator.validName(nameList) ) + .isInstanceOf(RuntimeException.class); } @Test public void 차량이름이_비었다면_예외를_던진다() { + final Validator validator = new Validator(); + + List nameList = List.of("Steve", ""); + assertThatThrownBy( () -> validator.validName(nameList) ) + .isInstanceOf(RuntimeException.class); } @Test public void 차량이름이_중복이라면_예외를_던진다() { + final Validator validator = new Validator(); + + List nameList = List.of("Steve", "Alex", "Steve"); + assertThatThrownBy( () -> validator.validName(nameList) ) + .isInstanceOf(RuntimeException.class); } @Test public void 시도횟수가_음수라면_예외를_던진다() { + final Validator validator = new Validator(); + int round = -5; + assertThatThrownBy( () -> validator.validRound(round)) + .isInstanceOf(RuntimeException.class); } }