From 89023fb74b0bc5173a769e4bb11759284ffd3a28 Mon Sep 17 00:00:00 2001 From: Hyeong gyu choi Date: Sun, 9 Jun 2024 22:47:29 +0900 Subject: [PATCH 1/9] add input name of cars logic --- README.md | 10 ++++++++++ src/main/java/Main.java | 8 ++++++++ src/main/java/view/InputView.java | 11 +++++++++++ 3 files changed, 29 insertions(+) create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/view/InputView.java diff --git a/README.md b/README.md index c550c4c2a09..9b028ec4eba 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,15 @@ * 코드 리뷰 피드백에 대한 개선 작업을 하고 다시 PUSH한다. * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. + +## 기능 +* 1. n대 자동차 이름 입력 +* 2. 입력값 체크(5자 넘어가면 에러 메세지 출력) +* 3. 쉼표 기준으로 스플릿하여 이름 저장 +* 4. 몇 번 시도할지 입력(0 또는 양수가 아닌 경우 에러 메세지 출력): m번 +* 5. f함수: 0~9사이 생성 랜덤 함수 +* 6. f함수 m번 실행 후 값 저장 +* 7. 각 함수 실행 결과 출력 +* 8. 우승자 선출 ## 온라인 코드 리뷰 과정 * [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview) \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000000..c6ceaf1d348 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,8 @@ +import view.InputView; + +public class Main { + public static void main(String[] args){ + InputView inputView = new InputView(); + } + +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000000..ac38c4adb40 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,11 @@ +package view; + +import java.util.Scanner; + +public class InputView { + public static String getCarsName() { + Scanner scanner = new Scanner(System.in); + return scanner.next(); + } + +} From 4c0cadb9f991081ecd5705bc2c76baf05fb115d6 Mon Sep 17 00:00:00 2001 From: thank Date: Sun, 9 Jun 2024 23:29:12 +0900 Subject: [PATCH 2/9] add input name of cars logic --- src/main/java/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c6ceaf1d348..b76a5fcefbf 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,6 +3,7 @@ public class Main { public static void main(String[] args){ InputView inputView = new InputView(); + inputView.getCarsName(); } } From aee17b7ef355dc79ad612747336d41fc381b1caf Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 01:45:41 +0900 Subject: [PATCH 3/9] add car name validation --- README.md | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- src/main/java/Main.java | 9 ---- src/main/java/{ => racingcar}/.gitkeep | 0 .../java/racingcar/RacingCarApplication.java | 16 +++++++ .../racingcar/controller/RacingCarGame.java | 16 +++++++ src/main/java/racingcar/domain/Car.java | 18 +++++++ .../java/racingcar/service/CarsService.java | 5 ++ .../service/impl/CarsServiceImpl.java | 48 +++++++++++++++++++ .../java/racingcar/util/StringParser.java | 22 +++++++++ .../java/{ => racingcar}/view/InputView.java | 5 +- 11 files changed, 131 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/Main.java rename src/main/java/{ => racingcar}/.gitkeep (100%) create mode 100644 src/main/java/racingcar/RacingCarApplication.java create mode 100644 src/main/java/racingcar/controller/RacingCarGame.java create mode 100644 src/main/java/racingcar/domain/Car.java create mode 100644 src/main/java/racingcar/service/CarsService.java create mode 100644 src/main/java/racingcar/service/impl/CarsServiceImpl.java create mode 100644 src/main/java/racingcar/util/StringParser.java rename src/main/java/{ => racingcar}/view/InputView.java (70%) diff --git a/README.md b/README.md index 9b028ec4eba..9af77111b34 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ ## 기능 -* 1. n대 자동차 이름 입력 +* 1. n대 자동차 이름 입력 * 2. 입력값 체크(5자 넘어가면 에러 메세지 출력) * 3. 쉼표 기준으로 스플릿하여 이름 저장 * 4. 몇 번 시도할지 입력(0 또는 양수가 아닌 경우 에러 메세지 출력): m번 -* 5. f함수: 0~9사이 생성 랜덤 함수 +* 5. f함수: 0~9사이 생성 랜덤 함수 * 6. f함수 m번 실행 후 값 저장 * 7. 각 함수 실행 결과 출력 * 8. 우승자 선출 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 28ff446a215..ffed3a254e9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/Main.java b/src/main/java/Main.java deleted file mode 100644 index b76a5fcefbf..00000000000 --- a/src/main/java/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -import view.InputView; - -public class Main { - public static void main(String[] args){ - InputView inputView = new InputView(); - inputView.getCarsName(); - } - -} diff --git a/src/main/java/.gitkeep b/src/main/java/racingcar/.gitkeep similarity index 100% rename from src/main/java/.gitkeep rename to src/main/java/racingcar/.gitkeep diff --git a/src/main/java/racingcar/RacingCarApplication.java b/src/main/java/racingcar/RacingCarApplication.java new file mode 100644 index 00000000000..a1323d564b8 --- /dev/null +++ b/src/main/java/racingcar/RacingCarApplication.java @@ -0,0 +1,16 @@ +package racingcar; + +import racingcar.controller.RacingCarGame; +import racingcar.service.CarsService; +import racingcar.service.impl.CarsServiceImpl; +import racingcar.view.InputView; + +public class RacingCarApplication { + public static void main(String[] args){ + CarsService carsService = new CarsServiceImpl(new InputView()); + RacingCarGame racingCarGame = new RacingCarGame(carsService); + racingCarGame.run(); + + } + +} diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java new file mode 100644 index 00000000000..bb16ef3a194 --- /dev/null +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -0,0 +1,16 @@ +package racingcar.controller; + +import racingcar.service.CarsService; +import racingcar.view.InputView; + +public class RacingCarGame { + + private final CarsService carsService; + public RacingCarGame(CarsService carsService) { + this.carsService = carsService; + } + public void run() { + + carsService.getCarsName(); + } +} diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java new file mode 100644 index 00000000000..774c841f4b3 --- /dev/null +++ b/src/main/java/racingcar/domain/Car.java @@ -0,0 +1,18 @@ +package racingcar.domain; + +public class Car { + private final int position; + + public int getPosition() { return position; } + + public String getName() { return name; } + + private final String name; + public Car(String name) { + this(0, name); + } + public Car(int position, String name) { + this.position = position; + this.name = name; + } +} diff --git a/src/main/java/racingcar/service/CarsService.java b/src/main/java/racingcar/service/CarsService.java new file mode 100644 index 00000000000..5356b7ea4b1 --- /dev/null +++ b/src/main/java/racingcar/service/CarsService.java @@ -0,0 +1,5 @@ +package racingcar.service; + +public interface CarsService { + public String getCarsName(); +} diff --git a/src/main/java/racingcar/service/impl/CarsServiceImpl.java b/src/main/java/racingcar/service/impl/CarsServiceImpl.java new file mode 100644 index 00000000000..52bae568fed --- /dev/null +++ b/src/main/java/racingcar/service/impl/CarsServiceImpl.java @@ -0,0 +1,48 @@ +package racingcar.service.impl; + +import racingcar.service.CarsService; +import racingcar.view.InputView; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class CarsServiceImpl implements CarsService { + private final InputView inputView; + private static final int LIMIT_NAME_LENGTH = 5; + + public CarsServiceImpl(InputView inputView) { + this.inputView = inputView; + } + + public String getCarsName(){ + Scanner scanner = new Scanner(System.in); + String carsName; + while(true){ + try{ + carsName = inputView.inputString(); + List carsNameList = splitCarsNmae(carsName); + checkCarsName(carsNameList); + break; + }catch (IllegalArgumentException e){ + System.out.println("[ERROR] 이름은 5자를 넘길 수 없습니다."); + } + } + return carsName; + } + + private List splitCarsNmae(String carsName){ + if(carsName.isEmpty()) throw new IllegalArgumentException("이름을 입력하세요"); + + return Arrays.stream(carsName.split(",")) + .collect(Collectors.toList()); + } + private void checkCarsName(List carsNameList){ + if (carsNameList.stream().anyMatch(name -> name.length() > LIMIT_NAME_LENGTH)) { + throw new IllegalArgumentException("이름은 5자를 넘길 수 없습니다."); + } + } + + +} diff --git a/src/main/java/racingcar/util/StringParser.java b/src/main/java/racingcar/util/StringParser.java new file mode 100644 index 00000000000..9bd3c1bdfce --- /dev/null +++ b/src/main/java/racingcar/util/StringParser.java @@ -0,0 +1,22 @@ +package racingcar.util; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class StringParser { + private static final String SPLIT_PATTERN = ","; + + private StringParser() {} + + public static List parse(String text) { + return parse(text, SPLIT_PATTERN); + } + + public static List parse(String text, String pattern) { + Objects.requireNonNull(pattern, SPLIT_PATTERN); + return Stream.of(text.split(pattern)) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/racingcar/view/InputView.java similarity index 70% rename from src/main/java/view/InputView.java rename to src/main/java/racingcar/view/InputView.java index ac38c4adb40..282f966894b 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -1,11 +1,12 @@ -package view; +package racingcar.view; import java.util.Scanner; public class InputView { - public static String getCarsName() { + public String inputString() { Scanner scanner = new Scanner(System.in); return scanner.next(); } + } From c7d01cbfbf96294161820c62a3eded8af56391b5 Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 01:55:35 +0900 Subject: [PATCH 4/9] modify cars name validation --- src/main/java/racingcar/RacingCarApplication.java | 4 ++-- .../java/racingcar/controller/RacingCarGame.java | 7 ++++--- src/main/java/racingcar/service/CarsService.java | 5 ----- .../java/racingcar/service/cars/CarsService.java | 7 +++++++ .../service/{ => cars}/impl/CarsServiceImpl.java | 14 +++++++------- src/main/java/racingcar/view/InputView.java | 4 ++++ 6 files changed, 24 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/racingcar/service/CarsService.java create mode 100644 src/main/java/racingcar/service/cars/CarsService.java rename src/main/java/racingcar/service/{ => cars}/impl/CarsServiceImpl.java (80%) diff --git a/src/main/java/racingcar/RacingCarApplication.java b/src/main/java/racingcar/RacingCarApplication.java index a1323d564b8..e001e29f986 100644 --- a/src/main/java/racingcar/RacingCarApplication.java +++ b/src/main/java/racingcar/RacingCarApplication.java @@ -1,8 +1,8 @@ package racingcar; import racingcar.controller.RacingCarGame; -import racingcar.service.CarsService; -import racingcar.service.impl.CarsServiceImpl; +import racingcar.service.cars.CarsService; +import racingcar.service.cars.impl.CarsServiceImpl; import racingcar.view.InputView; public class RacingCarApplication { diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java index bb16ef3a194..2fe9e03a286 100644 --- a/src/main/java/racingcar/controller/RacingCarGame.java +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -1,7 +1,8 @@ package racingcar.controller; -import racingcar.service.CarsService; -import racingcar.view.InputView; +import racingcar.service.cars.CarsService; + +import java.util.List; public class RacingCarGame { @@ -11,6 +12,6 @@ public RacingCarGame(CarsService carsService) { } public void run() { - carsService.getCarsName(); + List carsName = carsService.getCarsName(); } } diff --git a/src/main/java/racingcar/service/CarsService.java b/src/main/java/racingcar/service/CarsService.java deleted file mode 100644 index 5356b7ea4b1..00000000000 --- a/src/main/java/racingcar/service/CarsService.java +++ /dev/null @@ -1,5 +0,0 @@ -package racingcar.service; - -public interface CarsService { - public String getCarsName(); -} diff --git a/src/main/java/racingcar/service/cars/CarsService.java b/src/main/java/racingcar/service/cars/CarsService.java new file mode 100644 index 00000000000..23a8451f69e --- /dev/null +++ b/src/main/java/racingcar/service/cars/CarsService.java @@ -0,0 +1,7 @@ +package racingcar.service.cars; + +import java.util.List; + +public interface CarsService { + public List getCarsName(); +} diff --git a/src/main/java/racingcar/service/impl/CarsServiceImpl.java b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java similarity index 80% rename from src/main/java/racingcar/service/impl/CarsServiceImpl.java rename to src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java index 52bae568fed..53eb60b927b 100644 --- a/src/main/java/racingcar/service/impl/CarsServiceImpl.java +++ b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java @@ -1,6 +1,6 @@ -package racingcar.service.impl; +package racingcar.service.cars.impl; -import racingcar.service.CarsService; +import racingcar.service.cars.CarsService; import racingcar.view.InputView; import java.util.Arrays; @@ -16,20 +16,20 @@ public CarsServiceImpl(InputView inputView) { this.inputView = inputView; } - public String getCarsName(){ + public List getCarsName(){ Scanner scanner = new Scanner(System.in); - String carsName; + List carsNameList; while(true){ try{ - carsName = inputView.inputString(); - List carsNameList = splitCarsNmae(carsName); + String carsName = inputView.inputString(); + carsNameList = splitCarsNmae(carsName); checkCarsName(carsNameList); break; }catch (IllegalArgumentException e){ System.out.println("[ERROR] 이름은 5자를 넘길 수 없습니다."); } } - return carsName; + return carsNameList; } private List splitCarsNmae(String carsName){ diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 282f966894b..f026804cc5e 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -7,6 +7,10 @@ public String inputString() { Scanner scanner = new Scanner(System.in); return scanner.next(); } + public int inputInt() { + Scanner scanner = new Scanner(System.in); + return scanner.nextInt(); + } } From 63b43a1b220e06a61c8b22d71d20d40d964b0d17 Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 02:27:10 +0900 Subject: [PATCH 5/9] add input race try count logic --- .../java/racingcar/RacingCarApplication.java | 5 +++- .../racingcar/controller/RacingCarGame.java | 13 +++++++++- src/main/java/racingcar/domain/Car.java | 8 ++++++ .../service/cars/impl/CarsServiceImpl.java | 3 +-- .../racingcar/service/race/RaceService.java | 5 ++++ .../service/race/impl/RaceServiceImpl.java | 25 +++++++++++++++++++ 6 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/java/racingcar/service/race/RaceService.java create mode 100644 src/main/java/racingcar/service/race/impl/RaceServiceImpl.java diff --git a/src/main/java/racingcar/RacingCarApplication.java b/src/main/java/racingcar/RacingCarApplication.java index e001e29f986..016ec06809c 100644 --- a/src/main/java/racingcar/RacingCarApplication.java +++ b/src/main/java/racingcar/RacingCarApplication.java @@ -3,12 +3,15 @@ import racingcar.controller.RacingCarGame; import racingcar.service.cars.CarsService; import racingcar.service.cars.impl.CarsServiceImpl; +import racingcar.service.race.RaceService; +import racingcar.service.race.impl.RaceServiceImpl; import racingcar.view.InputView; public class RacingCarApplication { public static void main(String[] args){ CarsService carsService = new CarsServiceImpl(new InputView()); - RacingCarGame racingCarGame = new RacingCarGame(carsService); + RaceService raceService = new RaceServiceImpl(new InputView()); + RacingCarGame racingCarGame = new RacingCarGame(carsService, raceService); racingCarGame.run(); } diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java index 2fe9e03a286..e36c565ae26 100644 --- a/src/main/java/racingcar/controller/RacingCarGame.java +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -1,17 +1,28 @@ package racingcar.controller; +import racingcar.domain.Car; import racingcar.service.cars.CarsService; +import racingcar.service.race.RaceService; import java.util.List; +import java.util.stream.Collectors; public class RacingCarGame { private final CarsService carsService; - public RacingCarGame(CarsService carsService) { + private final RaceService raceService; + + public RacingCarGame(CarsService carsService, RaceService raceService) { this.carsService = carsService; + this.raceService = raceService; } + public void run() { List carsName = carsService.getCarsName(); + List cars = carsName.stream() + .map(Car::new) + .collect(Collectors.toList()); + int raceTryCount = raceService.getRaceTryCount(); } } diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 774c841f4b3..80914cf9ed1 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -15,4 +15,12 @@ public Car(int position, String name) { this.position = position; this.name = name; } + + @Override + public String toString() { + return "Car{" + + "position=" + position + + ", name='" + name + '\'' + + '}'; + } } diff --git a/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java index 53eb60b927b..4525b96fcdd 100644 --- a/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java +++ b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java @@ -17,7 +17,6 @@ public CarsServiceImpl(InputView inputView) { } public List getCarsName(){ - Scanner scanner = new Scanner(System.in); List carsNameList; while(true){ try{ @@ -26,7 +25,7 @@ public List getCarsName(){ checkCarsName(carsNameList); break; }catch (IllegalArgumentException e){ - System.out.println("[ERROR] 이름은 5자를 넘길 수 없습니다."); + System.out.println("[ERROR]" + e); } } return carsNameList; diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java new file mode 100644 index 00000000000..af9abce31ed --- /dev/null +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -0,0 +1,5 @@ +package racingcar.service.race; + +public interface RaceService { + public int getRaceTryCount(); +} diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java new file mode 100644 index 00000000000..8b1d5c1f65d --- /dev/null +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -0,0 +1,25 @@ +package racingcar.service.race.impl; + +import racingcar.service.race.RaceService; +import racingcar.view.InputView; + +public class RaceServiceImpl implements RaceService { + private final InputView inputView; + + public RaceServiceImpl(InputView inputView) { + this.inputView = inputView; + } + public int getRaceTryCount(){ + int tryCount; + while(true){ + try{ + tryCount = inputView.inputInt(); + if(tryCount <= 0) throw new IllegalArgumentException("비정상적인 숫자입니다."); + return tryCount; + }catch (IllegalArgumentException e){ + System.out.println("[ERROR]" + e); + } + } + } + +} From e822b2af4ae46d52bb5c27b5241dfb2d91b3594c Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 02:40:11 +0900 Subject: [PATCH 6/9] add Random Moving Number Logic --- src/main/java/racingcar/service/race/RaceService.java | 1 + .../racingcar/service/race/impl/RaceServiceImpl.java | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java index af9abce31ed..f20e6ac4a45 100644 --- a/src/main/java/racingcar/service/race/RaceService.java +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -2,4 +2,5 @@ public interface RaceService { public int getRaceTryCount(); + public int randomValue(); } diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java index 8b1d5c1f65d..283290e2d36 100644 --- a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -3,9 +3,11 @@ import racingcar.service.race.RaceService; import racingcar.view.InputView; +import java.util.Random; + public class RaceServiceImpl implements RaceService { private final InputView inputView; - + private static final int RANDOM_BOUNDARY = 10; public RaceServiceImpl(InputView inputView) { this.inputView = inputView; } @@ -21,5 +23,10 @@ public int getRaceTryCount(){ } } } + public int randomValue(){ + Random random = new Random(); + return random.nextInt(RANDOM_BOUNDARY); + } + } From 0d8c00912f1c05eee626fb360d6424e6c068d66b Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 03:02:16 +0900 Subject: [PATCH 7/9] add Racing Logic --- .../java/racingcar/controller/RacingCarGame.java | 1 + src/main/java/racingcar/domain/Car.java | 6 +++++- .../java/racingcar/service/race/RaceService.java | 5 +++++ .../service/race/impl/RaceServiceImpl.java | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java index e36c565ae26..8be51a5a639 100644 --- a/src/main/java/racingcar/controller/RacingCarGame.java +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -24,5 +24,6 @@ public void run() { .map(Car::new) .collect(Collectors.toList()); int raceTryCount = raceService.getRaceTryCount(); + raceService.raceStart(cars, raceTryCount); } } diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 80914cf9ed1..d5872098b66 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -1,7 +1,7 @@ package racingcar.domain; public class Car { - private final int position; + private int position; public int getPosition() { return position; } @@ -15,6 +15,10 @@ public Car(int position, String name) { this.position = position; this.name = name; } + public void move(int moveValue){ + position += moveValue; + } + @Override public String toString() { diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java index f20e6ac4a45..6acb2a9afec 100644 --- a/src/main/java/racingcar/service/race/RaceService.java +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -1,6 +1,11 @@ package racingcar.service.race; +import racingcar.domain.Car; + +import java.util.List; + public interface RaceService { public int getRaceTryCount(); public int randomValue(); + public void raceStart(List cars, int tryCount); } diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java index 283290e2d36..bd62a7704a6 100644 --- a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -1,13 +1,18 @@ package racingcar.service.race.impl; +import racingcar.domain.Car; import racingcar.service.race.RaceService; import racingcar.view.InputView; +import java.util.List; import java.util.Random; public class RaceServiceImpl implements RaceService { private final InputView inputView; private static final int RANDOM_BOUNDARY = 10; + private static final int MOVE_CONDITION = 4; + private static final int GO = 4; + private static final int STOP = 4; public RaceServiceImpl(InputView inputView) { this.inputView = inputView; } @@ -27,6 +32,17 @@ public int randomValue(){ Random random = new Random(); return random.nextInt(RANDOM_BOUNDARY); } + public int moveNumber(int randomValue){ + if(randomValue >= MOVE_CONDITION ) return GO; + return STOP; + } + public void raceStart(List cars, int tryCount){ + for(int i = 0; i < tryCount; ++i){ + for(Car car: cars){ + car.move(moveNumber(this.randomValue())); + } + } + } } From f47c4377834e83a9b3f6aebe16200e86eb1de7ff Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 03:39:14 +0900 Subject: [PATCH 8/9] add Print Race Result Logic --- .../racingcar/controller/RacingCarGame.java | 1 + .../racingcar/service/race/RaceService.java | 2 +- .../service/race/impl/RaceServiceImpl.java | 17 ++++++++++++----- src/main/java/racingcar/view/InputView.java | 2 ++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java index 8be51a5a639..2a24b4d0a68 100644 --- a/src/main/java/racingcar/controller/RacingCarGame.java +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -25,5 +25,6 @@ public void run() { .collect(Collectors.toList()); int raceTryCount = raceService.getRaceTryCount(); raceService.raceStart(cars, raceTryCount); + } } diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java index 6acb2a9afec..bef0c6920de 100644 --- a/src/main/java/racingcar/service/race/RaceService.java +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -6,6 +6,6 @@ public interface RaceService { public int getRaceTryCount(); - public int randomValue(); public void raceStart(List cars, int tryCount); + // public void printResult(List cars); } diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java index bd62a7704a6..97d75890b9c 100644 --- a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -11,8 +11,8 @@ public class RaceServiceImpl implements RaceService { private final InputView inputView; private static final int RANDOM_BOUNDARY = 10; private static final int MOVE_CONDITION = 4; - private static final int GO = 4; - private static final int STOP = 4; + private static final int GO = 1; + private static final int STOP = 0; public RaceServiceImpl(InputView inputView) { this.inputView = inputView; } @@ -28,21 +28,28 @@ public int getRaceTryCount(){ } } } - public int randomValue(){ + private int randomValue(){ Random random = new Random(); return random.nextInt(RANDOM_BOUNDARY); } - public int moveNumber(int randomValue){ + private int moveNumber(int randomValue){ if(randomValue >= MOVE_CONDITION ) return GO; return STOP; } public void raceStart(List cars, int tryCount){ for(int i = 0; i < tryCount; ++i){ for(Car car: cars){ - car.move(moveNumber(this.randomValue())); + int randomValue = this.randomValue(); + car.move(moveNumber(randomValue)); + printCar(car); } + System.out.println(); } } + public void printCar(Car car){ + System.out.println(car.getName() + " : " + car.getPosition()); + } + } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index f026804cc5e..ff05abbb2f8 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -4,10 +4,12 @@ public class InputView { public String inputString() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); Scanner scanner = new Scanner(System.in); return scanner.next(); } public int inputInt() { + System.out.println("시도할 회수는 몇회인가요?"); Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } From f8a67a847a5410bc4472f500102fbc13613d98a4 Mon Sep 17 00:00:00 2001 From: thank Date: Mon, 10 Jun 2024 03:57:31 +0900 Subject: [PATCH 9/9] add Determin Winners Logic --- .../racingcar/controller/RacingCarGame.java | 1 + .../racingcar/service/race/RaceService.java | 2 +- .../service/race/impl/RaceServiceImpl.java | 37 ++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java index 2a24b4d0a68..550c631a81d 100644 --- a/src/main/java/racingcar/controller/RacingCarGame.java +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -25,6 +25,7 @@ public void run() { .collect(Collectors.toList()); int raceTryCount = raceService.getRaceTryCount(); raceService.raceStart(cars, raceTryCount); + raceService.determineWinner(cars); } } diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java index bef0c6920de..52d6444eac6 100644 --- a/src/main/java/racingcar/service/race/RaceService.java +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -7,5 +7,5 @@ public interface RaceService { public int getRaceTryCount(); public void raceStart(List cars, int tryCount); - // public void printResult(List cars); + public void determineWinner(List cars); } diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java index 97d75890b9c..89850aee5a6 100644 --- a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Random; +import java.util.stream.Collectors; public class RaceServiceImpl implements RaceService { private final InputView inputView; @@ -16,6 +17,17 @@ public class RaceServiceImpl implements RaceService { public RaceServiceImpl(InputView inputView) { this.inputView = inputView; } + private int randomValue(){ + Random random = new Random(); + return random.nextInt(RANDOM_BOUNDARY); + } + private int moveNumber(int randomValue){ + if(randomValue >= MOVE_CONDITION ) return GO; + return STOP; + } + public void printCar(Car car){ + System.out.println(car.getName() + " : " + car.getPosition()); + } public int getRaceTryCount(){ int tryCount; while(true){ @@ -28,14 +40,6 @@ public int getRaceTryCount(){ } } } - private int randomValue(){ - Random random = new Random(); - return random.nextInt(RANDOM_BOUNDARY); - } - private int moveNumber(int randomValue){ - if(randomValue >= MOVE_CONDITION ) return GO; - return STOP; - } public void raceStart(List cars, int tryCount){ for(int i = 0; i < tryCount; ++i){ for(Car car: cars){ @@ -46,10 +50,23 @@ public void raceStart(List cars, int tryCount){ System.out.println(); } } - public void printCar(Car car){ - System.out.println(car.getName() + " : " + car.getPosition()); + + public void determineWinner(List cars){ + + int maxPosition = cars.stream() + .mapToInt(Car::getPosition) + .max() + .orElse(Integer.MIN_VALUE); + + String winners = cars.stream() + .filter(car -> car.getPosition() == maxPosition) + .map(Car::getName) + .collect(Collectors.joining(", ")); + + System.out.println("최종 우승자 : " + winners); } + }