-
Notifications
You must be signed in to change notification settings - Fork 99
[그리디]김태우 자동차 경주 미션 3,4단계 제출합니다. #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b54030a
be7dab9
b6c587f
f4008b0
752c8ff
dfb8eff
3d04c89
17908bd
9a52efc
f49d534
c9aa77c
59f0f6b
5802688
d4a1d27
9bdb50d
ec25b46
440de18
5067417
0741dfc
f88cc82
d91abd6
9141db8
86dbc22
0317ab8
637c951
9667e7b
c3eea9e
3332542
183e363
cf2d38b
423daa1
b455832
97c8af5
2b71ca3
d5a1b45
1a045b4
61a2844
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
package domain; | ||
|
||
import util.Generater; | ||
|
||
import util.GenerateNumber; | ||
|
||
public class Car { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. domain 패키지에도 Car가 있고 racingcar 패키지에도 Car가 있네요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전에 racingcar 패키지에서 Car를 만들었었는데, 이번 미션을 진행하면서는 처음부터 설계 방향을 다시 잡고자 새로 구현했습니다. 그래서 이전 미션에서 생성된 Car가 깃허브에 남아 있는 것 같습니다. |
||
private static final int STANDARD = 4; | ||
|
||
private final String carName; | ||
private final CarName carName; | ||
private int carPosition; | ||
|
||
private Car(String carName) { | ||
private Car(CarName carName) { | ||
this.carName = carName; | ||
this.carPosition = 0; | ||
} | ||
|
||
public static Car namePositionOf(String carName) { | ||
public static Car createCarByCarName(CarName carName) { | ||
return new Car(carName); | ||
} | ||
|
||
public void move() { | ||
int randomNumber = Generater.generateNumber(); | ||
public void moveCar() { | ||
int randomNumber = GenerateNumber.generateNumber(); | ||
if (randomNumber >= STANDARD) { | ||
carPosition++; | ||
} | ||
} | ||
|
||
public String getCarName() { | ||
return carName; | ||
return carName.getCarName(); | ||
} | ||
|
||
public int getCarPosition() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package domain; | ||
|
||
public class CarName { | ||
private final String carName; | ||
|
||
public CarName(String carName) { | ||
checkNull(carName); | ||
checkLength(carName); | ||
this.carName = carName; | ||
} | ||
|
||
private static void checkNull(String carName) { | ||
if (carName == null || carName.isBlank()) { | ||
throw new IllegalArgumentException("이름은 공백으로 입력될 수 없습니다."); | ||
} | ||
} | ||
|
||
private static void checkLength(String carName) { | ||
if (carName.length() > 5) { | ||
throw new IllegalArgumentException("자동차 이름은 최대 5글자까지만 가능합니다. 입력값:" + carName); | ||
} | ||
} | ||
|
||
public String getCarName() { | ||
return carName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,39 @@ | ||
package domain; | ||
|
||
import view.ResultView; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class GameManage { | ||
private final Cars cars; | ||
private final Winners winners; | ||
|
||
public GameManage(Cars cars, Winners winners) { | ||
public GameManage(Cars cars) { | ||
this.cars = cars; | ||
this.winners = winners; | ||
} | ||
|
||
public void raceOneRound() { | ||
for (Car car : cars.getCars()) { | ||
car.move(); | ||
car.moveCar(); | ||
} | ||
} | ||
|
||
public void race(int round) { | ||
for (int i = 0; i < round; i++) { | ||
raceOneRound(); | ||
ResultView.printRoundResult(cars.getCars()); | ||
} | ||
findWinner(); | ||
private Map<Integer, List<Car>> groupPosition() { | ||
return cars.getCars().stream().collect(Collectors.groupingBy(Car::getCarPosition)); | ||
} | ||
|
||
private int maxPosition() { | ||
return cars.getCars().stream().mapToInt(Car::getCarPosition).max().orElse(0); | ||
} | ||
public Winners createWinners() { | ||
Map<Integer, List<Car>> carsByPosition = groupPosition(); | ||
|
||
public void findWinner() { | ||
List<String> winnerNames = cars.getCars().stream() | ||
.filter(car -> car.getCarPosition() == maxPosition()) | ||
List<Car> winnersGroup = carsByPosition.entrySet().stream() | ||
.max(Map.Entry.comparingByKey()) | ||
.map(Map.Entry::getValue) | ||
.orElse(Collections.emptyList()); | ||
|
||
List<String> winnerNames = winnersGroup.stream() | ||
.map(Car::getCarName) | ||
.collect(Collectors.toList()); | ||
|
||
winners.setWinners(winnerNames); | ||
} | ||
|
||
public Winners getWinners() { | ||
return winners; | ||
return Winners.of(winnerNames); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package util; | ||
|
||
import java.util.Random; | ||
|
||
public class GenerateNumber { | ||
private static final Random random = new Random(); | ||
|
||
public static int generateNumber() { | ||
return random.nextInt(10); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,44 @@ | ||
package view; | ||
|
||
import domain.Cars; | ||
|
||
import java.util.Arrays; | ||
import java.util.InputMismatchException; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static List<String> inputCarName() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
String inputName = scanner.nextLine(); | ||
return Arrays.asList(inputName.split(",")); | ||
public static Cars inputCarName() { | ||
while (true) { | ||
try { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
String[] inputName = scanner.nextLine().split(","); | ||
for (int i = 0; i < inputName.length; i++) { | ||
inputName[i] = inputName[i].trim(); | ||
} | ||
List<String> names = Arrays.asList(inputName); | ||
|
||
return Cars.createCars(names); | ||
|
||
} catch (IllegalArgumentException input) { | ||
System.out.println("입력 오류이므로 다시 입력해주세요."); | ||
} | ||
} | ||
} | ||
|
||
public static int inputRound() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
return scanner.nextInt(); | ||
while (true) { | ||
try { | ||
int round = scanner.nextInt(); | ||
scanner.nextLine(); | ||
return round; | ||
} catch (InputMismatchException input) { | ||
System.out.println("회수는 숫자로 입력해 주세요!!"); | ||
scanner.nextLine(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package domain; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CarNameTest { | ||
|
||
@Test | ||
void 정상적인_이름이면_생성된다() { | ||
CarName carName = new CarName("car1"); | ||
assertEquals("car1", carName.getCarName()); | ||
} | ||
|
||
@Test | ||
void null이나공백이면_예외가_발생한다() { | ||
assertThrows(IllegalArgumentException.class, () -> new CarName(null)); | ||
assertThrows(IllegalArgumentException.class, () -> new CarName(" ")); | ||
} | ||
|
||
@Test | ||
void 다섯글자를_초과하면_예외가_발생한다() { | ||
assertThrows(IllegalArgumentException.class, () -> new CarName("aaaaaa")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
멋지게 바꾸어 주셨네요!