-
Notifications
You must be signed in to change notification settings - Fork 7
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
[10주차] ejy #62
base: ejy
Are you sure you want to change the base?
The head ref may contain hidden characters: "9\uC8FC\uCC28"
[10주차] ejy #62
Changes from all commits
76c972d
02e55ea
760b00c
e8af171
7c96cdb
b751542
194d32c
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import service.Game; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
Game game = new Game(); | ||
game.play(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package domain; | ||
|
||
public class Car { | ||
private static final String BLANK = " "; | ||
private static final int MAXIMAL_LENGTH = 5; | ||
|
||
private final String name; | ||
private int position; | ||
|
||
public Car(String name) { | ||
validate(name); | ||
this.name = name; | ||
this.position = 0; | ||
} | ||
|
||
public Car(String name, int position) { | ||
validate(name); | ||
this.name = name; | ||
this.position = position; | ||
} | ||
|
||
private void validate(String name) { | ||
if (name.length() > MAXIMAL_LENGTH) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 5자 이하여야 한다."); | ||
} | ||
if (name.contains(BLANK)) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 공백은 포함하지 않아야 한다."); | ||
} | ||
} | ||
|
||
public boolean isSamePosition(Car car) { | ||
return car.position == this.position; | ||
} | ||
|
||
public int compareTo(Car car) { | ||
return this.position - car.position; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
public void go() { | ||
position++; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static view.OutputView.printGameStatus; | ||
|
||
public class Cars { | ||
private final List<Car> cars; | ||
|
||
public Cars(List<String> cars) { | ||
validate(cars); | ||
this.cars = cars | ||
.stream() | ||
.map(Car::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private void validate(List<String> cars) { | ||
boolean checkName = cars.stream() | ||
.distinct() | ||
.count() != cars.size(); | ||
if (checkName) { | ||
throw new IllegalArgumentException("[ERROR] 중복된 이름입니다."); | ||
} | ||
} | ||
|
||
public void moveCars() { | ||
cars.stream() | ||
.filter(car -> Engine.isPower()) | ||
.forEach(Car::go); | ||
} | ||
|
||
public void printCars() { | ||
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. 도메인이 뷰의 역할도 하네요? |
||
cars.forEach(car -> | ||
printGameStatus(car.getName(), car.getPosition())); | ||
} | ||
|
||
public Winners findWinner() { | ||
Car maxPositionCar = findMaxPositionCar(); | ||
return new Winners(cars.stream() | ||
.filter(maxPositionCar::isSamePosition) | ||
.map(Winner::new) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
private Car findMaxPositionCar() { | ||
return cars.stream() | ||
.max(Car::compareTo) | ||
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 입력된 차량이 없습니다.")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package domain; | ||
|
||
import utils.RandomUtils; | ||
|
||
public class Engine { | ||
private static final int START_INCLUSIVE = 0; | ||
private static final int END_INCLUSIVE = 9; | ||
private static final int GO_POINT = 4; | ||
|
||
private Engine() { | ||
|
||
} | ||
|
||
public static boolean isPower() { | ||
return RandomUtils.nextInt(START_INCLUSIVE, END_INCLUSIVE) >= GO_POINT; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package domain; | ||
|
||
public class GameCounter { | ||
private static final int ZERO = 0; | ||
|
||
private int counter; | ||
|
||
public GameCounter(String round) { | ||
validate(round); | ||
this.counter = Integer.parseInt(round); | ||
} | ||
|
||
private void validate(String value) { | ||
int result; | ||
try { | ||
result = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 숫자여야 한다."); | ||
} | ||
if (result <= ZERO) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 자연수여야 한다."); | ||
} | ||
} | ||
|
||
public int nextRound() { | ||
return counter--; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package domain; | ||
|
||
public class Winner { | ||
private final Car winner; | ||
|
||
public Winner(Car winner) { | ||
this.winner = winner; | ||
} | ||
|
||
public String getWinnerName() { | ||
return winner.getName(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Winners { | ||
private static final String DELIMITER = ", "; | ||
|
||
private List<Winner> winners; | ||
|
||
public Winners(List<Winner> winners) { | ||
this.winners = new ArrayList<>(winners); | ||
} | ||
|
||
public String winnersToString() { | ||
return winners.stream() | ||
.map(Winner::getWinnerName) | ||
.collect(Collectors.joining(DELIMITER)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package service; | ||
|
||
import domain.Cars; | ||
import domain.GameCounter; | ||
import domain.Winners; | ||
import view.OutputView; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
import static view.InputView.inputCarNames; | ||
import static view.InputView.inputGameCount; | ||
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. 도 좋지만 |
||
|
||
public class Game { | ||
private static final int FINAL_ROUND = 0; | ||
|
||
public void play() { | ||
Cars cars = new Cars(inputCarNames()); | ||
GameCounter gameCounter = inputGameCount(); | ||
OutputView.printGamePreview(); | ||
while (gameCounter.nextRound() > FINAL_ROUND) { | ||
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.
|
||
cars.moveCars(); | ||
cars.printCars(); | ||
OutputView.nextLine(); | ||
} | ||
Winners winners = cars.findWinner(); | ||
OutputView.printGameResult(winners); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomUtils { | ||
private static final Random RANDOM = new Random(); | ||
private static final int ZERO = 0; | ||
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. 0은 |
||
|
||
private RandomUtils() { | ||
|
||
} | ||
|
||
public static int nextInt(final int startInclusive, final int endInclusive) { | ||
if (startInclusive > endInclusive || startInclusive < ZERO) { | ||
throw new IllegalArgumentException("[ERROR] 잘못된 랜덤값 설정"); | ||
} | ||
|
||
if (startInclusive == endInclusive) { | ||
return startInclusive; | ||
} | ||
|
||
return startInclusive + RANDOM.nextInt(endInclusive - startInclusive + 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package view; | ||
|
||
import domain.GameCounter; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class InputView { | ||
private static final BufferedReader BUFFERED_READER = new BufferedReader(new InputStreamReader(System.in)); | ||
private static final String DELIMITER = ","; | ||
|
||
private InputView() { | ||
|
||
} | ||
|
||
public static GameCounter inputGameCount() { | ||
OutputView.printGameCounterInput(); | ||
String inputNumber = nextLine(); | ||
validateGameCount(inputNumber); | ||
return new GameCounter(inputNumber); | ||
} | ||
|
||
private static void validateGameCount(String input) { | ||
if (input == null || input.isEmpty()) { | ||
throw new IllegalArgumentException("[ERROR] 숫자를 입력해주세요."); | ||
} | ||
} | ||
|
||
public static List<String> inputCarNames() { | ||
OutputView.printCarInput(); | ||
String input = nextLine(); | ||
validateCarNames(input); | ||
return Arrays.asList(input.split(DELIMITER)); | ||
} | ||
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. 어떤 부분에선 도메인으로 어떤 부분에서는 원시타입으로 리턴하는데 |
||
|
||
private static void validateCarNames(String input) { | ||
if (input == null || input.isEmpty()) { | ||
throw new IllegalArgumentException("[ERROR] 이름을 입력해 주세요."); | ||
} | ||
} | ||
|
||
private static String nextLine() { | ||
try { | ||
return BUFFERED_READER.readLine(); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("[ERROR] 잘못된 입력입니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package view; | ||
|
||
import domain.Winners; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public class OutputView { | ||
private static final String POSITION_DISPLAY = "-"; | ||
|
||
private OutputView() { | ||
|
||
} | ||
|
||
private static void printCarName(final String carName) { | ||
System.out.print(carName + " : "); | ||
} | ||
|
||
private static void printCarPosition(final int carPosition) { | ||
IntStream.range(0, carPosition) | ||
.mapToObj(s -> POSITION_DISPLAY) | ||
.forEach(System.out::print); | ||
System.out.println(); | ||
} | ||
|
||
public static void printGameStatus(final String carName, final int carPosition) { | ||
printCarName(carName); | ||
printCarPosition(carPosition); | ||
} | ||
|
||
public static void printGameResult(final Winners winners) { | ||
System.out.print("최종 우승자: "); | ||
System.out.println(winners.winnersToString()); | ||
} | ||
|
||
public static void printGameCounterInput() { | ||
System.out.println("시도할 횟수는 몇회인가요? "); | ||
} | ||
|
||
public static void printCarInput() { | ||
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
} | ||
|
||
public static void printGamePreview() { | ||
System.out.println("\n실행결과"); | ||
} | ||
|
||
public static void nextLine() { | ||
System.out.println(); | ||
} | ||
} |
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.
외부 객체가
Car
의 행동을 결정하네요?