-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[자동차 경주] 김민겸 미션 제출합니다. #2397
Open
Mingyum-Kim
wants to merge
16
commits into
woowacourse-precourse:main
Choose a base branch
from
Mingyum-Kim:Mingyum-Kim-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[자동차 경주] 김민겸 미션 제출합니다. #2397
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e1cb1e5
docs:readme 생성
Mingyum-Kim c9f117c
feat: 경주할 자동차의 이름을 입력받고 중복을 제거하는 기능 구현
Mingyum-Kim 0fa3d77
feat: 이름이 1자 이상, 5자 이하임을 검증하는 기능 구현
Mingyum-Kim 6062b34
feat: 자동차의 이름 리스트를 자동차로 변환하는 기능 구현
Mingyum-Kim f561bc7
feat: 커스텀 예외처리 클래스 구현
Mingyum-Kim 9b63408
feat: 콘솔 입출력을 담당하는 클래스 생성
Mingyum-Kim bf40421
feat: 자동차의 정보를 저장하는 클래스 생성
Mingyum-Kim 77be8c9
feat: 시도할 횟수를 입력하는 기능 구현
Mingyum-Kim ba7ea29
feat: 입력한 횟수만큼 자동차를 전진시키도록 구현
Mingyum-Kim d624451
feat: 무작위 값을 통해 자동차를 전진시키는 기능 구현
Mingyum-Kim 5b539da
feat: 자동차의 상태를 출력하는 기능 구현
Mingyum-Kim f049fbd
feat: 우승자를 가려내고 출력하는 기능 구현
Mingyum-Kim 9cbca00
feat: 어플리케이션 실행 기능 구현
Mingyum-Kim a1c63e1
test: Car 도메인의 테스트 코드 작성
Mingyum-Kim 8bfb104
test: Cars 도메인 테스트 코드 생성
Mingyum-Kim 311f70a
docs: 시스템 흐름도 추가
Mingyum-Kim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 💪 프로젝트 개요 | ||
|
||
자동차 경주 게임을 구현한다. | ||
자동차의 이름과 전진을 시도할 횟수를 입력받고, 자동차를 전진시킨 후 우승자를 선정한다. | ||
|
||
# 📝 구현 기능 목록 | ||
|
||
### 자동차 이름을 입력하는 기능 | ||
|
||
- [x] `경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)`를 출력한다. | ||
- [x] 자동차의 이름을 입력받는다. | ||
- [x] 빈 문자열이 아님을 검증한다. | ||
- [x] 올바른 구분자로 구분되었음을 검증한다. | ||
- [x] 중복되는 이름이 없음을 검증한다. | ||
- [x] 5자 이하의 이름인지 검증한다. | ||
|
||
### 시도할 횟수를 입력하는 기능 | ||
|
||
- [x] `시도할 회수는 몇회인가요?`를 출력한다. | ||
- [x] 시도할 횟수를 입력한다. | ||
- [x] 빈 문자열이 아님을 검증한다. | ||
- [x] 1 이상의 숫자로 이루어져 있음을 검증한다. | ||
|
||
### 자동차를 전진하는 기능 | ||
|
||
- [x] 입력한 횟수만큼 자동차 전진을 시도한다. | ||
- [x] 무작위 값을 생성하고 전진하는 조건을 검사한다. | ||
- [x] 조건에 부합한다면 자동차를 전진시킨다. | ||
|
||
### 자동차의 상태를 출력하는 기능 | ||
|
||
- [x] 모든 자동차의 이름과 이동한 갯수를 뷰에 전달한다. | ||
- [x] 뷰에서 자동차 이름을 출력하고 이동한 갯수를 `-`로 출력한다. | ||
|
||
### 우승자를 출력하는 기능 | ||
|
||
- [x] 가장 많이 이동한 자동차를 선정한다. | ||
- [x] `최종 우승자 : pobi`와 같이 우승자를 출력한다. | ||
|
||
# 🛠 시스템 흐름도 | ||
|
||
![img.png](흐름도.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
package racingcar; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import racingcar.controller.GameManager; | ||
import racingcar.view.InputView; | ||
import racingcar.view.OutputView; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
GameManager gameManager = new GameManager( | ||
new InputView(), | ||
new OutputView() | ||
); | ||
gameManager.run(); | ||
Console.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package racingcar.controller; | ||
|
||
import java.util.List; | ||
import racingcar.domain.Cars; | ||
import racingcar.view.InputView; | ||
import racingcar.view.OutputView; | ||
|
||
public class GameManager { | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public GameManager(InputView inputView, OutputView outputView) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
} | ||
|
||
public void run() { | ||
Cars cars = new Cars(inputView.readCarNames()); | ||
int count = inputView.readTryCount(); | ||
|
||
for (int i = 0; i < count; i++) { | ||
play(cars); | ||
} | ||
complete(cars); | ||
} | ||
|
||
private void play(Cars cars) { | ||
cars.moveCars(); | ||
outputView.printResult(cars); | ||
} | ||
|
||
private void complete(Cars cars) { | ||
List<String> winners = cars.selectWinners(); | ||
outputView.printWinners(winners); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package racingcar.domain; | ||
|
||
import racingcar.global.util.RandomNumberGenerator; | ||
|
||
public class Car { | ||
private static final int THRESHOLD = 4; | ||
private static final int START = 0; | ||
private static final int END = 9; | ||
public static final int INTERVAL = 1; | ||
private Name name; | ||
private int moved; | ||
|
||
public Car(Name name) { | ||
this.name = name; | ||
this.moved = 0; | ||
} | ||
|
||
public void move() { | ||
if (canMove()) { | ||
moved += INTERVAL; | ||
} | ||
} | ||
|
||
private boolean canMove() { | ||
int number = RandomNumberGenerator.generate(START, END); | ||
if (number >= THRESHOLD) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int moved() { | ||
return moved; | ||
} | ||
|
||
public String name() { | ||
return name.getValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.List; | ||
|
||
public class Cars { | ||
private final List<Car> cars; | ||
|
||
public Cars(List<String> cars) { | ||
this.cars = generateToCars(cars); | ||
} | ||
|
||
private List<Car> generateToCars(List<String> cars) { | ||
return cars.stream() | ||
.map(name -> new Car(new Name(name))) | ||
.toList(); | ||
} | ||
|
||
public void moveCars() { | ||
for (Car car : cars) { | ||
car.move(); | ||
} | ||
} | ||
|
||
public List<String> selectWinners() { | ||
int maxMoved = getMaxMoved(); | ||
|
||
return cars.stream() | ||
.filter(car -> car.moved() == maxMoved) | ||
.map(Car::name) | ||
.toList(); | ||
} | ||
|
||
private int getMaxMoved() { | ||
return cars.stream() | ||
.mapToInt(car -> car.moved()) | ||
.max() | ||
.orElseThrow(IllegalStateException::new); | ||
} | ||
|
||
public int size() { | ||
return cars.size(); | ||
} | ||
|
||
public Car get(int i) { | ||
return cars.get(i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package racingcar.domain; | ||
|
||
import racingcar.global.exception.CustomException; | ||
import racingcar.global.exception.ErrorMessage; | ||
|
||
public class Name { | ||
public static final int MIN = 1; | ||
public static final int MAX = 5; | ||
private final String name; | ||
|
||
public Name(String name) { | ||
Validator.validateRange(name.length(), MIN, MAX); | ||
this.name = name; | ||
} | ||
|
||
public String getValue() { | ||
return name; | ||
} | ||
|
||
public static class Validator { | ||
private static void validateRange(int length, int start, int end) { | ||
if (isInvalidRange(length, start, end)) { | ||
throw CustomException.from(ErrorMessage.INVALID_LENGTH_ERROR); | ||
} | ||
} | ||
|
||
private static boolean isInvalidRange(int number, int start, int end) { | ||
return number < start || number > end; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/racingcar/global/exception/CustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racingcar.global.exception; | ||
|
||
public class CustomException extends IllegalArgumentException { | ||
private static final String PREFIX = "[ERROR] "; | ||
|
||
private CustomException(ErrorMessage errorMessage) { | ||
super(PREFIX + errorMessage.getMessage()); | ||
} | ||
|
||
public static CustomException from(ErrorMessage errorMessage) { | ||
return new CustomException(errorMessage); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/racingcar/global/exception/ErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package racingcar.global.exception; | ||
|
||
public enum ErrorMessage { | ||
BLANK_INPUT_ERROR("빈 문자열이 입력되었습니다."), | ||
INVALID_LENGTH_ERROR("잘못된 길이의 문자열을 입력하였습니다. "), | ||
DUPLICATED_CAR_ERROR("자동차의 이름이 중복되었습니다."), | ||
INVALID_TRY_COUNT_ERROR("잘못된 시도 횟수의 입력입니다."); | ||
|
||
private final String message; | ||
|
||
ErrorMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return this.message; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/racingcar/global/util/RandomNumberGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package racingcar.global.util; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class RandomNumberGenerator { | ||
public static int generate(int start, int end) { | ||
return Randoms.pickNumberInRange(start, end); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package racingcar.view; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import racingcar.global.exception.CustomException; | ||
import racingcar.global.exception.ErrorMessage; | ||
import racingcar.view.console.ConsoleReader; | ||
import racingcar.view.console.ConsoleWriter; | ||
|
||
public class InputView { | ||
private static final String CAR_NAMES_NOTICE = "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)`를 출력한다."; | ||
public static final String CAR_NAMES_SEPARATOR = ","; | ||
public static final String TRY_COUNT_NOTICE = "시도할 회수는 몇회인가요?"; | ||
|
||
public List<String> readCarNames() { | ||
ConsoleWriter.printlnMessage(CAR_NAMES_NOTICE); | ||
return Validator.validateCarNames(ConsoleReader.enterMessage()); | ||
} | ||
|
||
public int readTryCount() { | ||
ConsoleWriter.printlnMessage(TRY_COUNT_NOTICE); | ||
return Validator.validateNumber(ConsoleReader.enterMessage()); | ||
} | ||
|
||
private static class Validator { | ||
private static List<String> validateCarNames(String message) { | ||
List<String> cars = parseStringToList(message, CAR_NAMES_SEPARATOR); | ||
validateDuplicated(cars); | ||
return cars; | ||
} | ||
|
||
private static void validateDuplicated(List<String> items) { | ||
if (hasDuplicated(items)) { | ||
throw CustomException.from(ErrorMessage.DUPLICATED_CAR_ERROR); | ||
} | ||
} | ||
|
||
private static boolean hasDuplicated(List<String> items) { | ||
return items.size() != calculateUniqueCount(items); | ||
} | ||
|
||
private static int calculateUniqueCount(List<String> items) { | ||
return (int) items.stream() | ||
.distinct() | ||
.count(); | ||
} | ||
|
||
private static List<String> parseStringToList(String message, String separator) { | ||
return Arrays.stream(split(message, separator)).toList(); | ||
} | ||
|
||
private static String[] split(String message, String separator) { | ||
return message.split(separator, -1); | ||
} | ||
|
||
private static int validateNumber(String message) { | ||
if (isNotNumber(message)) { | ||
throw CustomException.from(ErrorMessage.INVALID_TRY_COUNT_ERROR); | ||
} | ||
return Integer.parseInt(message); | ||
} | ||
|
||
private static boolean isNotNumber(String str) { | ||
return !str.matches("^[1-9]+$"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package racingcar.view; | ||
|
||
import java.util.List; | ||
import racingcar.domain.Car; | ||
import racingcar.domain.Cars; | ||
import racingcar.view.console.ConsoleWriter; | ||
|
||
public class OutputView { | ||
private static final String RESULT_NOTICE = "실행 결과"; | ||
private static final String WINNER_NOTICE = "최종 우승자 : %s"; | ||
private static final String WINNER_SEPARATOR = ", "; | ||
|
||
public void printResult(Cars cars) { | ||
ConsoleWriter.printlnMessage(RESULT_NOTICE); | ||
printCarsStatus(cars); | ||
ConsoleWriter.println(); | ||
} | ||
|
||
private void printCarsStatus(Cars cars) { | ||
for (int i = 0; i < cars.size(); i++) { | ||
Car car = cars.get(i); | ||
printCarStatus(car, i); | ||
} | ||
} | ||
|
||
private void printCarStatus(Car car, int i) { | ||
String name = car.name(); | ||
int moved = car.moved(); | ||
ConsoleWriter.printlnMessage(name + " : " + "-".repeat(moved)); | ||
} | ||
|
||
public void printWinners(List<String> winners) { | ||
ConsoleWriter.printlnFormat( | ||
WINNER_NOTICE, | ||
generateWinnerResult(winners) | ||
); | ||
} | ||
|
||
private String generateWinnerResult(List<String> winners) { | ||
return String.join(WINNER_SEPARATOR, winners); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package racingcar.view.console; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import racingcar.global.exception.CustomException; | ||
import racingcar.global.exception.ErrorMessage; | ||
|
||
public final class ConsoleReader { | ||
public static String enterMessage() { | ||
return Validator.validate(Console.readLine()); | ||
} | ||
|
||
private static class Validator { | ||
public static String validate(String message) { | ||
validateBlankInput(message); | ||
return message; | ||
} | ||
|
||
private static void validateBlankInput(String message) { | ||
if (message.isBlank()) { | ||
throw CustomException.from(ErrorMessage.BLANK_INPUT_ERROR); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
retry를 사용하고 싶을 때는, 아래와 같이 사용한다.