Skip to content

Commit

Permalink
요구사항 2 구현
Browse files Browse the repository at this point in the history
1. 각 시도 결과를 가지고 있는 PhaseResult 추가
   - 각 PhaseResult는 자동차의 이름을 Key, 이동 결과를 Value로 하는 Map을 가지고 있음
   - PhaseResult는 현재 선두의 이름을 List의 형태로 반환하는 메소드를 가지고있음
2. PhaseResult들을 가지고있는 총 게임 결과 GameResult 추가
   - 시도 번호로 각 시도 결과를 가져올 수 있음
   - 게임 우승자는 마지막 PhaseResult의 선두의 이름을 가져옴
3. ParticipateCars 의 주석은 어떤게 더 보기좋은지 궁금해서 주석 처리하였음
  • Loading branch information
Gobukgol committed May 2, 2020
1 parent ef2c8ad commit 5880c41
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/main/java/racing/RacingGameApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class RacingGameApplication {
public static void main(String[] args) {
InputValueDto inputValueDto = InputView.getInputValue();
InputValueDto inputValueDto = InputView.getInputValueWithName();

RacingGame racingGame = new RacingGame(inputValueDto);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
public class Car {
private static final int MOVE_CONIDTION = 4;
private int distance;
private String name;

public Car(String name) {
this.name = name;
this.distance = 0;
}

public String getName() {
return name;
}

public int attemptToMove(int randomNumber) {
if (isMove(randomNumber)) {
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/racing/domain/GameResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,33 @@


public class GameResult {
private final List<RacingResult> racingResults;
private final List<PhaseResult> phaseResults;
private int numberOfPhase;

public GameResult() {
this.racingResults = new ArrayList<>();
}

public GameResult(int numberOfPhase) {
this.racingResults = new ArrayList<>();
this.phaseResults = new ArrayList<>();
this.numberOfPhase = numberOfPhase;
}

public void addEachRacingResult(RacingResult racingResult) {
this.racingResults.add(racingResult);
public void addEachRacingResult(PhaseResult phaseResult) {
this.phaseResults.add(phaseResult);
}

public RacingResult findRacingResultByPhase(int phase) {
return racingResults.get(phase - 1);
public PhaseResult findRacingResultByPhase(int phase) {
try {
return phaseResults.get(phase - 1);
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException(String.format("존재하지 않는 시도 횟수입니다. - %d \n", phase));
}
}

public int getNumberOfPhase() {
return numberOfPhase;
}

public List<String> getWinner() {
PhaseResult lastResult = findRacingResultByPhase(numberOfPhase);

return lastResult.getCurrentLeads();
}
}
38 changes: 30 additions & 8 deletions src/main/java/racing/domain/ParticipateCars.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import racing.domain.common.NumberGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ParticipateCars {
private final List<Car> cars;

public ParticipateCars(int numberOfCars) {
this.cars = createCars(numberOfCars);
public ParticipateCars(String[] namesOfCars) {
cars = createCars(namesOfCars);
}

public List<Integer> tryMove(NumberGenerator numberGenerator) {
Expand All @@ -19,13 +21,33 @@ public List<Integer> tryMove(NumberGenerator numberGenerator) {
.collect(Collectors.toList());
}

private List<Car> createCars(int numberOfCars) {
List<Car> cars = new ArrayList<>();
public Map<String, Integer> tryMoveWithName(NumberGenerator numberGenerator) {

for (int i = 0; i < numberOfCars; i++) {
cars.add(new Car());
/* Map<String, Integer> moveResults = new LinkedHashMap<>();
for (Car car : cars) {
moveResults.put(car.getName(),car.attemptToMove(numberGenerator.generateNumber()));
}
return cars;
return moveResults;*/

return cars.stream()
.collect(Collectors.toMap(Car::getName,
car -> car.attemptToMove(numberGenerator.generateNumber()),
(x, y) -> x,
LinkedHashMap::new));
}

private List<Car> createCars(String[] namesOfCars) {
/* List<Car> cars = new ArrayList<>();
for (int i = 0 ; i < namesOfCars.length ; i++) {
cars.add(new Car(namesOfCars[i]));
}*/

return Arrays.stream(namesOfCars)
.map(String::trim)
.map(Car::new)
.collect(Collectors.toList());
}
}
30 changes: 30 additions & 0 deletions src/main/java/racing/domain/PhaseResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package racing.domain;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class PhaseResult {
private final Map<String, Integer> raceResults;

public PhaseResult(Map<String, Integer> raceResults) {
this.raceResults = raceResults;
}

public Map<String, Integer> getRaceResults() {
return raceResults;
}

public List<String> getCurrentLeads() {
int max = raceResults.entrySet().stream()
.mapToInt(Map.Entry::getValue)
.max()
.orElseThrow(IllegalStateException::new);

return raceResults.entrySet().stream()
.filter(x -> x.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
}
6 changes: 3 additions & 3 deletions src/main/java/racing/domain/RacingGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public class RacingGame {

public RacingGame(InputValueDto inputValueDto) {
this.inputValueDto = inputValueDto;
this.racingPhase = new RacingPhase(inputValueDto.getNumberOfCars());
this.racingPhase = new RacingPhase(inputValueDto.getNamesOfCars());
}

public GameResult startGame(NumberGenerator numberGenerator) {
int numberOfAttempts = inputValueDto.getNumberOfAttempts();
GameResult gameResult = new GameResult(numberOfAttempts);

for (int i = 0; i < numberOfAttempts; i++) {
RacingResult eachRacingResult = racingPhase.startRace(numberGenerator);
gameResult.addEachRacingResult(eachRacingResult);
PhaseResult eachPhaseResult = racingPhase.startRace(numberGenerator);
gameResult.addEachRacingResult(eachPhaseResult);
}

return gameResult;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/racing/domain/RacingPhase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
public class RacingPhase {
private final ParticipateCars participateCars;

public RacingPhase(int numberOfCars) {
this.participateCars = new ParticipateCars(numberOfCars);
public RacingPhase(String[] namesOfCars) {
this.participateCars = new ParticipateCars(namesOfCars);
}

public RacingResult startRace(NumberGenerator numberGenerator) {
return new RacingResult(participateCars.tryMove(numberGenerator));
public PhaseResult startRace(NumberGenerator numberGenerator) {
return new PhaseResult(participateCars.tryMoveWithName(numberGenerator));
}


}
15 changes: 0 additions & 15 deletions src/main/java/racing/domain/RacingResult.java

This file was deleted.

23 changes: 22 additions & 1 deletion src/main/java/racing/dto/InputValueDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package racing.dto;

public class InputValueDto {
final private int numberOfCars;
private int numberOfCars;
private String[] namesOfCars;
final private int numberOfAttempts;

public InputValueDto(int numberOfCars, int numberOfAttempts) {
Expand All @@ -10,6 +11,12 @@ public InputValueDto(int numberOfCars, int numberOfAttempts) {
this.numberOfAttempts = numberOfAttempts;
}

public InputValueDto(String[] namesOfCars, int numberOfAttempts) {
validateInputValue(namesOfCars, numberOfAttempts);
this.namesOfCars = namesOfCars;
this.numberOfAttempts = numberOfAttempts;
}

public int getNumberOfCars() {
return numberOfCars;
}
Expand All @@ -18,6 +25,10 @@ public int getNumberOfAttempts() {
return numberOfAttempts;
}

public String[] getNamesOfCars() {
return namesOfCars;
}

private void validateInputValue(int numberOfCars, int numberOfAttempts) {
if (numberOfCars <= 0) {
throw new IllegalArgumentException("참여할 자동차의 대수는 0 보다 커야합니다.");
Expand All @@ -27,4 +38,14 @@ private void validateInputValue(int numberOfCars, int numberOfAttempts) {
throw new IllegalArgumentException("시도할 횟수는 0 보다 커야합니다.");
}
}

private void validateInputValue(String[] namesOfCars, int numberOfAttempts) {
if (namesOfCars.length <= 0) {
throw new IllegalArgumentException("입력된 자동차 이름의 개수는 0 보다 커야합니다.");
}

if (numberOfAttempts <= 0) {
throw new IllegalArgumentException("시도할 횟수는 0 보다 커야합니다.");
}
}
}
9 changes: 5 additions & 4 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

public class InputView {
private static final Scanner scanner = new Scanner(System.in);
private static final String DELIMETER = ",";

public static InputValueDto getInputValue() {
public static InputValueDto getInputValueWithName() {
try {
int numberOfCars = Integer.parseInt(inputNumberOfCars());
String[] namesOfCars = inputNameOfCars().split(DELIMETER);
int numberOfAttempts = Integer.parseInt(inputNumberOfAttempts());

return new InputValueDto(numberOfCars, numberOfAttempts);
return new InputValueDto(namesOfCars, numberOfAttempts);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자를 입력해주세요.");
}
Expand All @@ -29,7 +30,7 @@ public static String inputNumberOfAttempts() {
}

public static String inputNameOfCars() {
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,)를 기준으로 구분)");
return scanner.nextLine();
}
}
39 changes: 28 additions & 11 deletions src/main/java/racing/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
package racing.view;

import racing.domain.GameResult;
import racing.domain.RacingResult;
import racing.domain.PhaseResult;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OutputView {
private static void printRacingResult(RacingResult racingResultResult) {
String result = racingResultResult.getRaceResults().stream()
.map(OutputView::visualize)
.collect(Collectors.joining("\n"));

System.out.println(result);

}

public static void printGameResult(GameResult gameResult) {
int numberOfPhase = gameResult.getNumberOfPhase();

for (int i = 1; i <= numberOfPhase; i++) {
RacingResult racingResult = gameResult.findRacingResultByPhase(i);
printRacingResult(racingResult);
PhaseResult phaseResult = gameResult.findRacingResultByPhase(i);
printRacingResult(phaseResult);
}

printWinner(gameResult.getWinner());
}

public static void printWinner(List<String> winners) {
String winner = winners.stream()
.collect(Collectors.joining(","));

System.out.println(winner + "가 최종 우승했습니다.");
}

private static void printRacingResult(PhaseResult phaseResultResult) {
String result = phaseResultResult.getRaceResults()
.entrySet().stream()
.map(OutputView::mapping)
.collect(Collectors.joining("\n"));

System.out.println(result);
System.out.println();
}

private static String mapping(Map.Entry<String, Integer> entry) {
return entry.getKey() + " : " + visualize(entry.getValue());
}

public static String visualize(int target) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/racing/domain/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CarTest {
"4, 1", "3, 0"
})
void name(int randomNumber, int expectedResult) {
Car car = new Car();
Car car = new Car("pobi");

int actualResult = car.attemptToMove(randomNumber);

Expand Down
Loading

0 comments on commit 5880c41

Please sign in to comment.