-
Notifications
You must be signed in to change notification settings - Fork 55
[이현성_BackEnd] 2주차 과제 제출입니다. #53
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
base: main
Are you sure you want to change the base?
Changes from all commits
19c05a5
f0708c3
6aea59f
ac25730
0386d0b
7795a6e
4ac2c83
fde9511
5ff600e
9caea85
db80566
434d555
fcd820f
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,8 @@ | ||
| 1. 자동차 이름입력받기 | ||
| 2. 시도할 경주횟수 입력받기 | ||
| 3. 차 객체 만들기 | ||
| 4. 경주차 리스트 만들기 | ||
| 5. 차 움직이기 | ||
| 6. 모든차의 위치 보여주기 | ||
| 7. 우승자 결정 | ||
| 8. 우승자 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,44 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| List<Car> cars = inputCarNames(); | ||
| RacingResultPrinter printer = new RacingResultPrinter(); | ||
| int attempts = inputAttemptCount(); | ||
| RacingGame game = new RacingGame(cars); | ||
| System.out.println("\n실행 결과"); | ||
| game.play(attempts); | ||
| printer.printWinners(game.findWinners()); | ||
| } | ||
|
|
||
| private static List<Car> inputCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input = Console.readLine(); | ||
| String[] carNames = input.split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
|
|
||
| for (String carName : carNames) { | ||
| carName = carName.trim(); | ||
| if (carName.length() > 5) { | ||
| throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다."); | ||
| } | ||
|
Comment on lines
+27
to
+29
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.add(new Car(carName)); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| private static int inputAttemptCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| try { | ||
| return Integer.parseInt(Console.readLine()); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("숫자를 입력해야 합니다."); | ||
| } | ||
|
Comment on lines
+39
to
+41
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. 음수가 들어오면 어떻게 될까요? |
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| private final String carName; | ||
| private int position = 0; | ||
|
|
||
| public Car(String carName) { | ||
| this.carName = carName; | ||
| } | ||
|
|
||
| public void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) | ||
|
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. 랜덤 숫자를 만드는 함수로 빼도 좋을 것 같아요 |
||
| position++; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return carName; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class RacingGame { | ||
| private final List<Car> cars; | ||
| private final RacingResultPrinter printer = new RacingResultPrinter(); | ||
| public RacingGame(List<Car> cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public void play(int attempts) { | ||
| for (int i = 0; i < attempts; i++) { | ||
| cars.forEach(Car::move); | ||
| printer.printCurrentCarPosition(cars); | ||
| } | ||
| } | ||
|
|
||
| public List<String> findWinners() { | ||
| int fastest = 0; | ||
| for (Car car : cars) { | ||
| if (car.getPosition() > fastest) { | ||
| fastest = car.getPosition(); | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+26
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.
|
||
|
|
||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getPosition() == fastest) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
| } | ||
|
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. 입력 관련 클래스도 만들 수 있을 것 같아요! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| //출력관련 클래스 | ||
| public class RacingResultPrinter { | ||
| //경주 후 차위치 출력 | ||
| public void printCurrentCarPosition(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.print(car.getName() + " : "); | ||
| System.out.println("-".repeat(car.getPosition())); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void printWinners(List<String> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } |
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.
static으로 선언한 이유가 있을까요?다른 방식은 없었는지 궁금합니당