-
Notifications
You must be signed in to change notification settings - Fork 54
[엄성현_BackEnd] 2주차 과제 제출합니다. #71
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
Open
castleeom0001-collab
wants to merge
14
commits into
BCSDLab-Edu:main
Choose a base branch
from
castleeom0001-collab:main
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5f3dd9f
feat: 경주할 자동차의 이름 입력(5자 이내)
castleeom0001-collab e2c86f3
fear: 경주할 자동차의 이름 입력(5자 이내)
castleeom0001-collab 94ef1d9
fear: 시도할 횟수 입력
castleeom0001-collab 17cc2b7
feat: 각 차수별 실행 결과 출력
castleeom0001-collab d84ddbf
feat: 최동 우승자 출력
castleeom0001-collab 043be21
feat: 자동차 객체 생성
castleeom0001-collab 8a5a890
feat: 0에서 9 사이에서 무작위 값 구하기
castleeom0001-collab 7ea4c16
feat: 숫자가 4이상이면 자동차 전진
castleeom0001-collab 04e2fbc
feat: 자동차가 전진한 횟수만큼 -로 표시
castleeom0001-collab a2972f0
feat: 자동차가 전진한 횟수만큼 -로 표시
castleeom0001-collab 790556b
feat: 입력받은 만큼 경주 반복
castleeom0001-collab 2ef1141
feat: 가장 많이 전진한 자동차를 우승자로 선정
castleeom0001-collab 26284cc
feat: 오류(자동차 이름 5자 이내, 시도 횟수 0 이상, 시도 횟수는 숫자)
castleeom0001-collab e929b22
feat: 수정 완료
castleeom0001-collab 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 hidden or 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,19 @@ | ||
| 입력 | ||
| 경주할 자동차의 이름 입력(5자 이내) | ||
| 시도할 횟수 입력 | ||
| 출력 | ||
| 각 차수별 실행 결과 출력 | ||
| 최종 우승자 출력 | ||
| 자동차 | ||
| 자동차 객체 생성 | ||
| 0에서 9 사이에서 무작위 값 구하기 | ||
| 숫자가 4이상이면 자동차 전진 | ||
| 자동차가 전진한 횟수만큼 -로 표시 | ||
| 경주 게임 | ||
| 입력받은 만큼 경주 반복 | ||
| 가장 많이 전진한 자동차를 우승자로 선정 | ||
| 우승자가 여러명일 경우 쉼표로 구분 | ||
| 오류 | ||
| 자동차 이름 5자 이내 | ||
| 시도 횟수 0 이상 | ||
| 시도 횟수는 숫자 |
This file contains hidden or 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,7 @@ | ||
| package racingcar; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| RacingGame racingGame = new RacingGame(); | ||
| racingGame.start(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,11 @@ | ||
| package racingcar; | ||
| public class Car { | ||
| private final String name; | ||
| private int position=0; | ||
| public Car(String name){this.name = name;} | ||
|
|
||
| public void move(int number){if(number>=4){position++;}} | ||
|
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 String getName(){return name;} | ||
| public int getPosition(){return position;} | ||
| public String getPositionSign(){return "-".repeat(position);} | ||
| } | ||
This file contains hidden or 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,22 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import java.util.*; | ||
| public class Input{ | ||
| public static List<Car> readCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input = Console.readLine(); | ||
| String[] names = input.split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : names) { | ||
| Validator.validateCarName(name.trim()); | ||
| cars.add(new Car(name.trim())); | ||
| } | ||
| return cars; | ||
| } | ||
| public static int readTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| String input = Console.readLine(); | ||
| return Validator.validateTryCount(input); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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,15 @@ | ||
| package racingcar; | ||
| import java.util.*; | ||
| public class Output { | ||
| public static void printRoundResult(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.println(car.getName() + " : " + car.getPositionSign()); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| public static void printWinners(List<Car> winners) { | ||
| String result = String.join(", ", | ||
| winners.stream().map(Car::getName).toList()); | ||
| System.out.println("최종 우승자 : " + result); | ||
| } | ||
| } |
This file contains hidden or 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,37 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.*; | ||
| public class RacingGame { | ||
| private List<Car> cars; | ||
| private int tryCount; | ||
| public void start() { | ||
| cars = Input.readCarNames(); | ||
| tryCount = Input.readTryCount(); | ||
| System.out.println("\n실행 결과"); | ||
| for (int i = 0; i < tryCount; i++) { | ||
| raceOnce(); | ||
| Output.printRoundResult(cars); | ||
| } | ||
| List<Car> winners = getWinners(); | ||
| Output.printWinners(winners); | ||
| } | ||
| private void raceOnce() { | ||
| for (Car car : cars) { | ||
| int number = Randoms.pickNumberInRange(0, 9); | ||
| car.move(number); | ||
| } | ||
| } | ||
| private List<Car> getWinners() { | ||
| int maxPosition = cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElse(0); | ||
|
Comment on lines
+25
to
+28
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. Stream 활용 좋아요 👍 |
||
| List<Car> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getPosition() == maxPosition) { | ||
| winners.add(car); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
| } | ||
This file contains hidden or 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,19 @@ | ||
| package racingcar; | ||
| public class Validator { | ||
| public static void validateCarName(String name) { | ||
| if (name == null || name.trim().isEmpty() || name.length() > 5) { | ||
| throw new IllegalArgumentException("자동차 이름은 1~5자 이내여야 합니다."); | ||
| } | ||
| } | ||
| public static int validateTryCount(String input) { | ||
| try { | ||
| int count = Integer.parseInt(input); | ||
| if (count < 0) { | ||
| throw new IllegalArgumentException("시도 횟수는 0 이상이어야 합니다."); | ||
| } | ||
| return count; | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,38 +1,32 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.test.NsTest; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest; | ||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class ApplicationTest extends NsTest { | ||
| private static final int MOVING_FORWARD = 4; | ||
| private static final int STOP = 3; | ||
|
|
||
| private static final int MOVING_FORWARD=4; | ||
| private static final int STOP=3; | ||
| @Test | ||
| void 전진_정지() { | ||
| assertRandomNumberInRangeTest( | ||
| () -> { | ||
| run("pobi,woni", "1"); | ||
| assertThat(output()).contains("pobi : -", "woni : ", "최종 우승자 : pobi"); | ||
| run("pobi,woni","1"); | ||
| assertThat(output()).contains("pobi : -","woni : ","최종 우승자 : pobi"); | ||
| }, | ||
| MOVING_FORWARD, STOP | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void 이름에_대한_예외_처리() { | ||
| assertSimpleTest(() -> | ||
| assertThatThrownBy(() -> runException("pobi,javaji", "1")) | ||
| assertThatThrownBy(() -> runException("pobi,javaji","1")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void runMain() { | ||
| Application.main(new String[]{}); | ||
| } | ||
| } | ||
| } |
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.
EOF 에 대해 알아보시면 좋을거같아요