Skip to content

Commit a1a4e38

Browse files
authored
๐Ÿš€ 4๋‹จ๊ณ„ - ์ž๋™์ฐจ ๊ฒฝ์ฃผ(์šฐ์Šน์ž) (#6036)
* [step2] 2๋‹จ๊ณ„ - ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ * [step4] feat : ์ž๋™์ฐจ ์ด๋ฆ„ ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„ - Car ๊ฐ์ฒด name ์ถ”๊ฐ€ - inputView : ์ž๋™์ฐจ ์ด๋ฆ„ ์ž…๋ ฅ ๋ฐ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ ๋กœ์ง ์ถ”๊ฐ€ - ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€ * [step4] feat : ์ž๋™์ฐจ ์ด๋ฆ„ 5์ž ์ดˆ๊ณผ ์ œํ•œ ๊ตฌํ˜„ - Cars ๊ฐ์ฒด์—์„œ ์ด๋ฆ„ ์ดˆ๊ณผ ์ œํ•œ ๋กœ์ง ๊ตฌํ˜„ - ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€ * [step4] feat : ์ž๋™์ฐจ ์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„ - ResultView : ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ์ž๋™์ฐจ ์ƒํƒœ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ - ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€ * [step4] feat : ์ž๋™์ฐจ ๊ฒฝ์ฃผ ๊ฒŒ์ž„ ์šฐ์Šน์ž ์ถœ๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„ * [step4] fix : FunctionalInterface annotation ์ถ”๊ฐ€ * refactor : winners ์ถœ๋ ฅ ๋กœ์ง ์ˆ˜์ • * refactor : Car name์— ๋Œ€ํ•œ ๊ฒ€์ฆ๋กœ์ง ๋ณ€๊ฒฝ Cars -> Car * fix: ์ค‘๋ณต๋œ ์ด๋ฆ„ ๊ฒ€์ฆ ์ถ”๊ฐ€ * refactor : ์ •์ ํŒฉํ† ๋ฆฌ๋ฉ”์†Œ๋“œ ์ ์šฉ (Race.creat(), Cars.fromNames())
1 parent efd80ef commit a1a4e38

File tree

9 files changed

+179
-29
lines changed

9 files changed

+179
-29
lines changed

โ€Žsrc/main/java/racingcar/RacingCar.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111
import java.util.List;
1212

1313
public class RacingCar {
14-
14+
public static void main(String[] args) {
15+
}
1516
public static void gameStart() {
1617
Cars cars = new Cars(getCars(InputView.inputValidatedNumberOfCar()));
1718
Race race = new Race(cars, InputView.inputValidatedNumberOfAttempts());
1819
race.start(createRandomStrategy());
1920
}
2021

21-
public static ArrayList<Car> getCars(int car) {
22+
public static void gameStartWithName() {
23+
Race race = Race.create();
24+
race.startWithName(createRandomStrategy());
25+
}
26+
27+
28+
public static List<Car> getCars(int car) {
2229
ArrayList<Car> cars = new ArrayList<>();
23-
for(int i=0; i<car; i++){
30+
for (int i = 0; i < car; i++) {
2431
cars.add(new Car());
2532
}
2633
return cars;
2734
}
2835

36+
2937
private static RandomStrategy createRandomStrategy() {
3038
return new RandomStrategy(new RandomGenerator());
3139
}

โ€Žsrc/main/java/racingcar/domain/Car.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,41 @@
33
import racingcar.strategy.MovingStrategy;
44

55
public class Car {
6+
public static final int CAR_NAME_SIZE = 5;
7+
8+
private String name;
69
private int moveCount;
710

811
public Car() {
912
this.moveCount = 1;
1013
}
1114

15+
public Car(String name) {
16+
validName(name);
17+
this.name = name;
18+
this.moveCount = 1;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
1225
public int getMoveCount() {
1326
return moveCount;
1427
}
1528

16-
1729
public void move(MovingStrategy movingStrategy) {
18-
if(movingStrategy.isMove()){
30+
if (movingStrategy.isMove()) {
1931
moveCount++;
2032
}
2133
}
2234

35+
private void validName(String name) {
36+
if (name == null || name.isEmpty()) {
37+
throw new IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ ๋นˆ๊ฐ’์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
38+
}
39+
if (name.length() > CAR_NAME_SIZE) {
40+
throw new IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ 5์ž๋ฅผ ์ดˆ๊ณผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
41+
}
42+
}
2343
}

โ€Žsrc/main/java/racingcar/domain/Cars.java

+28
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import racingcar.ui.ResultView;
55

66
import java.util.ArrayList;
7+
import java.util.Arrays;
78
import java.util.Collections;
89
import java.util.List;
10+
import java.util.stream.Collectors;
911

1012
public class Cars {
1113

@@ -15,11 +17,37 @@ public Cars(List<Car> cars) {
1517
this.cars = new ArrayList<>(cars);
1618
}
1719

20+
public static Cars fromNames(String[] names) {
21+
validCarNames(names);
22+
List<Car> carList = Arrays.stream(names)
23+
.map(Car::new)
24+
.collect(Collectors.toList());
25+
return new Cars(carList);
26+
}
27+
28+
private static void validCarNames(String[] names) {
29+
if (Arrays.stream(names).distinct().count() < names.length) {
30+
throw new IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
31+
}
32+
}
33+
1834
public void moveAll(MovingStrategy strategy) {
1935
cars.forEach(car -> car.move(strategy));
2036
}
2137

2238
public List<Car> getCurrentStatus() {
2339
return Collections.unmodifiableList(cars);
2440
}
41+
42+
public List<String> getWinners() {
43+
int maxMoveCount = cars.stream()
44+
.mapToInt(Car::getMoveCount)
45+
.max()
46+
.orElse(0);
47+
48+
return cars.stream()
49+
.filter(car -> car.getMoveCount() == maxMoveCount)
50+
.map(Car::getName)
51+
.collect(Collectors.toList());
52+
}
2553
}

โ€Žsrc/main/java/racingcar/domain/Race.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package racingcar.domain;
22

33
import racingcar.strategy.MovingStrategy;
4+
import racingcar.ui.InputView;
45
import racingcar.ui.ResultView;
56

67
public class Race {
@@ -12,6 +13,12 @@ public Race(Cars cars, int totalAttempts) {
1213
this.totalAttempts = totalAttempts;
1314
}
1415

16+
public static Race create() {
17+
String[] carNames = InputView.inputdNameOfCar();
18+
int attempts = InputView.inputValidatedNumberOfAttempts();
19+
return new Race(Cars.fromNames(carNames), attempts);
20+
}
21+
1522
public void start(MovingStrategy strategy) {
1623
System.out.println("์‹คํ–‰ ๊ฒฐ๊ณผ");
1724
ResultView.viewRacingCar(cars.getCurrentStatus());
@@ -20,4 +27,13 @@ public void start(MovingStrategy strategy) {
2027
ResultView.viewRacingCar(cars.getCurrentStatus());
2128
}
2229
}
30+
public void startWithName(MovingStrategy strategy) {
31+
System.out.println("์‹คํ–‰ ๊ฒฐ๊ณผ");
32+
ResultView.viewRacingCarWithName(cars.getCurrentStatus());
33+
for (int attempt = 1; attempt < totalAttempts; attempt++) {
34+
cars.moveAll(strategy);
35+
ResultView.viewRacingCarWithName(cars.getCurrentStatus());
36+
}
37+
ResultView.viewRacingCarWinner(cars.getWinners());
38+
}
2339
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package racingcar.strategy;
22

3+
@FunctionalInterface
34
public interface MovingStrategy {
45
boolean isMove();
56
}

โ€Žsrc/main/java/racingcar/ui/InputView.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class InputView {
66

7+
public static final String CAR_NAME_DELIMITER = ",";
78
public static Scanner SCANNER = new Scanner(System.in);
89
public static final int CAR_MIN = 0;
910
public static final int NUM_MIN = 0;
@@ -21,7 +22,10 @@ public static int inputNumberOfAttempts() {
2122
System.out.println("์‹œ๋„ํ•  ํšŒ์ˆ˜๋Š” ๋ช‡ ํšŒ ์ธ๊ฐ€์š”?");
2223
return SCANNER.nextInt();
2324
}
24-
25+
public static String inputNameOfCar() {
26+
System.out.println("๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”(์ด๋ฆ„์€ ์‰ผํ‘œ(,)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„).");
27+
return SCANNER.nextLine();
28+
}
2529
public static int inputValidatedNumberOfCar() {
2630
int input = inputNumberOfCar();
2731
validateInput(input, "์ฐจ๋Ÿ‰ ์ˆ˜", CAR_MIN);
@@ -32,6 +36,11 @@ public static int inputValidatedNumberOfAttempts() {
3236
validateInput(input, "์‹œ๋„ ํšŸ์ˆ˜", NUM_MIN);
3337
return input;
3438
}
39+
public static String[] inputdNameOfCar() {
40+
String input = inputNameOfCar();
41+
input.split(CAR_NAME_DELIMITER);
42+
return input.split(",");
43+
}
3544

3645
private static void validateInput(int value, String fieldName, int min) {
3746
if (value <= min) {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
package racingcar.ui;
22

33
import racingcar.domain.Car;
4+
import racingcar.domain.Cars;
45

56
import java.util.List;
7+
import java.util.stream.Collectors;
68

79
public class ResultView {
810

911
public static void viewRacingCar(List<Car> cars) {
10-
for(Car car : cars) {
11-
for(int i = 0; i < car.getMoveCount(); i++){
12+
for (Car car : cars) {
13+
for (int i = 0; i < car.getMoveCount(); i++) {
1214
System.out.print("-");
1315
}
1416
System.out.println();
1517
}
1618
System.out.println();
1719

1820
}
21+
22+
public static void viewRacingCarWithName(List<Car> cars) {
23+
for (Car car : cars) {
24+
System.out.print(car.getName() + " : ");
25+
for (int i = 0; i < car.getMoveCount(); i++) {
26+
System.out.print("-");
27+
}
28+
System.out.println();
29+
}
30+
System.out.println();
31+
}
32+
33+
public static void viewRacingCarWinner(List<String> winners) {
34+
System.out.println(String.join(",", winners) + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค.");
35+
}
1936
}

โ€Žsrc/test/java/racingcar/RacingCarTest.java

+65-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import racingcar.ui.InputView;
1010

1111
import java.io.ByteArrayInputStream;
12-
import java.util.ArrayList;
12+
import java.util.List;
1313
import java.util.Scanner;
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,34 +30,34 @@ void setUp() {
3030

3131
@Test
3232
@DisplayName("์ „์ง„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋ฉด ์ „์ง„")
33-
void move(){
34-
car.move(()->true);
33+
void move() {
34+
car.move(() -> true);
3535
assertThat(car.getMoveCount()).isEqualTo(2);
3636
}
3737

3838
@Test
3939
@DisplayName("์ „์ง„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉˆ์ถค")
40-
void stop(){
41-
car.move(()->false);
40+
void stop() {
41+
car.move(() -> false);
4242
assertThat(car.getMoveCount()).isEqualTo(1);
4343
}
4444

4545
@Test
4646
@DisplayName("์ „์ง„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋ฉด ๋ชจ๋‘ ์ „์ง„")
47-
void moveAll(){
48-
cars.moveAll(()->true);
47+
void moveAll() {
48+
cars.moveAll(() -> true);
4949

5050
assertAll(
5151
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(2),
5252
() -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(2),
5353
() -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(2)
54-
);
54+
);
5555
}
5656

5757
@Test
5858
@DisplayName("์ „์ง„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋‘ ๋ฉˆ์ถค")
59-
void stopAll(){
60-
cars.moveAll(()->false);
59+
void stopAll() {
60+
cars.moveAll(() -> false);
6161

6262
assertAll(
6363
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(1),
@@ -68,13 +68,13 @@ void stopAll(){
6868

6969
@Test
7070
@DisplayName("random ๊ฐ’์€ 0๊ณผ 9์‚ฌ์ด ๊ฐ’")
71-
void random(){
71+
void random() {
7272
assertThat(RandomGenerator.generate()).isBetween(0, 9);
7373
}
7474

7575
@Test
7676
@DisplayName("์ž๋™์ฐจ ๋Œ€์ˆ˜๊ฐ€ 0๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฆฌํ„ด")
77-
void inputCar(){
77+
void inputCar() {
7878
String input = "0";
7979
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes())));
8080

@@ -84,7 +84,7 @@ void inputCar(){
8484

8585
@Test
8686
@DisplayName("์‹œ๋„ํ•  ํšŒ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฆฌํ„ด")
87-
void inputAttempts(){
87+
void inputAttempts() {
8888
String input = "0";
8989
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes())));
9090

@@ -95,9 +95,60 @@ void inputAttempts(){
9595
@Test
9696
@DisplayName("๋ฐ˜ํ™˜๋œ ์ž๋™์ฐจ List์˜ ํฌ๊ธฐ๋Š” ์ž…๋ ฅ๋œ ํฌ๊ธฐ์™€ ๊ฐ™๋‹ค.")
9797
public void getCars() {
98-
ArrayList<Car> cars = RacingCar.getCars(CAR);
98+
List<Car> cars = RacingCar.getCars(CAR);
9999
assertThat(cars.size()).isEqualTo(CAR);
100100
}
101101

102+
@Test
103+
@DisplayName("์ž๋™์ฐจ ์ด๋ฆ„์ด ๊ณต๋ž€์ผ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฆฌํ„ด")
104+
void inputCarName_empty() {
105+
assertThatThrownBy(() -> new Car(""))
106+
.isInstanceOf(IllegalArgumentException.class);
107+
}
108+
109+
@Test
110+
@DisplayName("์ž๋™์ฐจ ์ด๋ฆ„์ด 5์ž ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฆฌํ„ด")
111+
void inputCarName_5์ž์ดˆ๊ณผ() {
112+
assertThatThrownBy(() -> new Car("hyundai"))
113+
.isInstanceOf(IllegalArgumentException.class);
114+
}
115+
116+
@Test
117+
@DisplayName("์ž๋™์ฐจ ์ด๋ฆ„์ด ์ค‘๋ณต๋  ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฆฌํ„ด")
118+
void inputCarName_์ค‘๋ณต๋œ์ด๋ฆ„() {
119+
String[] names = {"pobi", "pobi"};
120+
assertThatThrownBy(() -> Cars.fromNames(names))
121+
.isInstanceOf(IllegalArgumentException.class);
122+
}
123+
124+
@Test
125+
@DisplayName("์ž๋™์ฐจ ์ด๋ฆ„์€ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„")
126+
void inputCarName_split() {
127+
String input = "pobi,crong,honux";
128+
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes())));
129+
assertThat(InputView.inputdNameOfCar()).containsExactly(input.split(","));
130+
131+
}
132+
133+
@Test
134+
@DisplayName("๋‹จ์ผ ์šฐ์Šน์ž ๊ฒฐ์ • ")
135+
void getWinner() {
136+
Car pobi = new Car("pobi");
137+
Car crong = new Car("crong");
138+
Cars cars = new Cars(List.of(pobi, crong));
139+
pobi.move(() -> true);
140+
141+
assertThat(cars.getWinners()).containsExactly("pobi");
142+
}
143+
144+
@Test
145+
@DisplayName("๊ณต๋™ ์šฐ์Šน์ž ๊ฒฐ์ • ")
146+
void getWinners() {
147+
Car pobi = new Car("pobi");
148+
Car crong = new Car("crong");
149+
Cars cars = new Cars(List.of(pobi, crong));
150+
151+
assertThat(cars.getWinners()).containsExactly("pobi", "crong");
152+
}
102153

103154
}

0 commit comments

Comments
ย (0)