-
Notifications
You must be signed in to change notification settings - Fork 9
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
손현수) 자동차 경주 시뮬레이션 기능 추가 #1
base: Untaini
Are you sure you want to change the base?
Changes from all commits
62e0cd4
5e38f7e
ecddec2
c4b9717
1b780d9
32e13eb
5e45720
cd48ee3
04615d7
06b3c8b
65b263b
f788aef
681039c
be24ffb
b118782
56122be
e7f1526
0324770
1dabec0
4b6fc87
8dc465d
1fee1e4
1d07b2b
66ed54a
e76735a
dc2657f
63b1b06
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,9 @@ | ||
<h1>구현할 기능 목록</h1> | ||
|
||
- ### 사용자로부터 자동차 이름 입력받고 유효성 검사하기 | ||
- ### 사용자로부터 시도할 횟수 입력받고 유효성 검사하기 | ||
- ### 시도할 횟수만큼 라운드 반복하기 | ||
- ### 모든 자동차 전진 또는 정지하기 | ||
- ### 자동차의 상태 출력하기 | ||
- ### 최종 우승자 찾기 | ||
- ### 최종 우승자 출력하기 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
package racingcar; | ||
|
||
import racingcar.game.Car; | ||
import racingcar.game.Game; | ||
import racingcar.tool.InputUtils; | ||
|
||
import java.util.List; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// TODO 구현 진행 | ||
List<Car> cars = InputUtils.getCarListFromInput(); | ||
int totalRound = InputUtils.getTotalRoundFromInput(); | ||
Game game = new Game(cars, totalRound); | ||
|
||
game.start(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package racingcar.game; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
import racingcar.tool.StringValidator; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
public class Car { | ||
private final String name; | ||
private int position = 0; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
} | ||
|
||
public static Car createCar(String carName) { | ||
StringValidator.cannotUseCarName(carName); | ||
|
||
return new Car(carName); | ||
} | ||
|
||
public int getPosition() { | ||
return this.position; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%s : %s",this.name, getMovingPositionLine()); | ||
} | ||
|
||
public void move() { | ||
if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
this.position += 1; | ||
} | ||
} | ||
|
||
private String getMovingPositionLine() { | ||
List<String> movingPositions = Arrays.stream(new String[this.position]) | ||
.map(s -> "-") | ||
.collect(Collectors.toList()); | ||
|
||
return String.join("", movingPositions); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package racingcar.game; | ||
|
||
import racingcar.tool.PrintManager; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Game { | ||
|
||
private final List<Car> cars; | ||
private final int totalRound; | ||
|
||
public Game(List<Car> cars, int totalRound) { | ||
this.cars = cars; | ||
this.totalRound = totalRound; | ||
} | ||
|
||
public void start() { | ||
Round round = new Round(cars); | ||
|
||
PrintManager.printGameResultHead(); | ||
while (round.getCurrentRound() <= totalRound) { | ||
round.play(); | ||
} | ||
|
||
List<Car> winningCars = findWinningCars(); | ||
PrintManager.printWinningCarsName(winningCars); | ||
} | ||
|
||
private List<Car> findWinningCars() { | ||
if (cars.size() == 0) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
int maxPosition = cars.stream() | ||
.map(Car::getPosition) | ||
.max(Integer::compareTo) | ||
.get(); | ||
|
||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package racingcar.game; | ||
|
||
import racingcar.tool.PrintManager; | ||
|
||
import java.util.List; | ||
|
||
public class Round { | ||
private int currentRound; | ||
private final List<Car> cars; | ||
|
||
public Round(List<Car> cars) { | ||
this.currentRound = 1; | ||
this.cars = cars; | ||
} | ||
|
||
public int getCurrentRound() { | ||
return this.currentRound; | ||
} | ||
|
||
public void play() { | ||
moveAllCars(); | ||
PrintManager.printAllCarsStatus(cars); | ||
this.currentRound += 1; | ||
} | ||
|
||
private void moveAllCars() { | ||
cars.forEach(Car::move); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package racingcar.tool; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
import racingcar.game.Car; | ||
|
||
import java.util.List; | ||
|
||
public class InputUtils { | ||
|
||
private InputUtils() {} | ||
|
||
public static List<Car> getCarListFromInput() { | ||
while (true) { | ||
PrintManager.printCarNameInputDescription(); | ||
|
||
//for to split last comma, ' ' is going to be removed by trim func. | ||
String carNames = Console.readLine() + ' '; | ||
try { | ||
return StringConvertor.convertIntoCars(carNames); | ||
} catch (IllegalArgumentException exception) { | ||
PrintManager.printErrorMessage(exception.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public static int getTotalRoundFromInput() { | ||
while (true) { | ||
PrintManager.printTotalRoundInputDescription(); | ||
|
||
String roundInput = Console.readLine(); | ||
try { | ||
return StringConvertor.convertIntoNaturalNumber(roundInput); | ||
} catch (IllegalArgumentException exception) { | ||
PrintManager.printErrorMessage(exception.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package racingcar.tool; | ||
|
||
import racingcar.game.Car; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ListConvertor { | ||
|
||
private ListConvertor() {} | ||
|
||
public static String joinCarsName(String delimiter, List<Car> cars) { | ||
List<String> names = cars.stream() | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
|
||
return String.join(delimiter, names); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package racingcar.tool; | ||
|
||
import racingcar.game.Car; | ||
|
||
import java.util.List; | ||
|
||
public class PrintManager { | ||
|
||
private PrintManager() {} | ||
|
||
public static void printCarNameInputDescription() { | ||
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
} | ||
|
||
public static void printTotalRoundInputDescription() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
} | ||
Comment on lines
+11
to
+17
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. SRP를 위해 클래스의 책임이 명확히 구분된 것 같아서 좋습니다! 저도 해봐야겠네요 |
||
|
||
public static void printAllCarsStatus(List<Car> cars) { | ||
cars.forEach(System.out::println); | ||
System.out.println(); | ||
} | ||
|
||
public static void printWinningCarsName(List<Car> winningCars) { | ||
String winningCarsName = ListConvertor.joinCarsName(", ", winningCars); | ||
|
||
System.out.printf("최종 우승자 : %s", winningCarsName); | ||
} | ||
|
||
public static void printGameResultHead() { | ||
System.out.println("\n실행 결과"); | ||
} | ||
|
||
public static void printErrorMessage(String message) { | ||
System.out.printf("[ERROR] %s\n", message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package racingcar.tool; | ||
|
||
import racingcar.game.Car; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class StringConvertor { | ||
|
||
private StringConvertor() {} | ||
|
||
public static List<Car> convertIntoCars(String carNames) throws IllegalArgumentException { | ||
return Arrays.stream(carNames.split(",")) | ||
.map(String::trim) | ||
.map(Car::createCar) | ||
.collect(Collectors.toList()); | ||
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. 함수형 프로그래밍으로 구현하신 것이 정말 인상적인 것 같습니다. 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. 위의 코드를 말로 풀면 다음과 같습니다.
코드를 해석하는데 조금 도움이 됐으면 좋겠네요 :) 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 static Integer convertIntoNaturalNumber(String targetString) throws IllegalArgumentException { | ||
try { | ||
int number = Integer.parseInt(targetString); | ||
|
||
if (number < 1) { | ||
throw new NumberFormatException(); | ||
} | ||
|
||
return number; | ||
} catch (NumberFormatException exception) { | ||
throw new IllegalArgumentException("자연수가 아닌 값은 입력할 수 없습니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package racingcar.tool; | ||
|
||
public class StringValidator { | ||
|
||
private StringValidator() {} | ||
|
||
public static void cannotUseCarName(String carName) throws IllegalArgumentException { | ||
if (carName.length() == 0) { | ||
throw new IllegalArgumentException("자동차 이름은 공백일 수 없습니다."); | ||
} else if (carName.length() > 5) { | ||
throw new IllegalArgumentException("자동차 이름은 최대 5자리입니다. 불가능한 이름: " + carName); | ||
} | ||
} | ||
} |
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.
printCarNameInputDescription()이나 이 함수 같이 안내 문구만 각각 한 줄씩 따로 함수로 생성한 이유가 있을까요?
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.
PrintManager는 출력과 관련된 함수들을 정의해둔 클래스입니다. 그래서 출력되는 문자열을 바꾸고 싶을 때는 PrintManager만 수정하면 되게끔 만들었습니다.