-
Notifications
You must be signed in to change notification settings - Fork 3
Doyeon/week2 과제 제출 #70
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
1e3cbe6
b4ad72e
3c7c32b
9c208da
a7ae223
1817670
33ecbdd
c07fb69
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 |
|---|---|---|
| @@ -1,7 +1,24 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| // initialize - 1 자동차 목록 입력 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input = Console.readLine(); | ||
| String[] carNames = input.split(","); | ||
|
|
||
| // initialize - 2 시도 횟수 입력 | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| String sTryCount = Console.readLine(); | ||
| int tryCount = Validator.tryCountValidator(sTryCount); | ||
|
|
||
| Cars cars = new Cars(carNames); | ||
|
|
||
| RacingGame.playGame(cars, tryCount, new RandomMovingStrategy()); | ||
|
|
||
| System.out.println("실행 결과"); | ||
| RacingGame.printWinners(cars); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package racingcar; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car() { this.name = null; } | ||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
| public String getName() { return name; } | ||
| public int getPosition() { return position; } | ||
| public void setPosition(int position) { this.position = position; } | ||
| public void strategyMove(MovingStrategy strategy) { | ||
| if (strategy.canMove()) { | ||
| position++; | ||
| } | ||
| } | ||
| public void PrintInfo() { | ||
| System.out.println("Name: " + name + " Position: " + position); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Cars { | ||
| private final List<Car> cars; | ||
|
|
||
| private Cars() { cars = new ArrayList<>(); } | ||
| public Cars(List<Car> cars) { this.cars = cars; } | ||
|
|
||
| public Cars(String[] carNames) { | ||
| this.cars = new ArrayList<>(); | ||
| for (String carName : carNames) { | ||
| Validator.carNameValidator(carName); | ||
| cars.add(new Car(carName.trim())); | ||
| } | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return cars; | ||
| } | ||
|
|
||
| public void printInfo() { | ||
| for (Car car : cars) { | ||
| System.out.print(car.getName() + " : "); | ||
| for (int i = 0; i < car.getPosition(); i++) { | ||
| System.out.print("-"); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| int maxPosition = 0; | ||
|
|
||
| for (Car car : cars) { | ||
| maxPosition = Math.max(maxPosition, car.getPosition()); | ||
| } | ||
|
|
||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getPosition() == maxPosition) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package racingcar; | ||
|
|
||
| public interface MovingStrategy { | ||
| boolean canMove(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package racingcar; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class RacingGame{ | ||
| private RacingGame() {}; | ||
|
|
||
| public static void playGame(Cars cars, int count, MovingStrategy strategy) { | ||
| for (int i = 0; i < count; i++) { | ||
| for(Car car : cars.getCars()) { | ||
| car.strategyMove(strategy); | ||
| } | ||
| cars.printInfo(); | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public static void printWinners(Cars cars) { | ||
| List<String> winners = cars.getWinners(); | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
|
Member
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. join 사용은 생각 못했네요 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class RandomMovingStrategy implements MovingStrategy { | ||
| @Override | ||
| public boolean canMove() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package racingcar; | ||
|
|
||
| public class Validator { | ||
| final static int MAX_NAME_LENGTH = 5; | ||
|
Member
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 Validator() {} | ||
| public static int tryCountValidator(String tryCountInput) { | ||
| int tryCount = 0; | ||
|
|
||
| // 1. 빈 입력값 예외 처리 | ||
| if (tryCountInput == null || tryCountInput.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("시도 횟수는 비어 있을 수 없습니다."); | ||
| } | ||
|
|
||
| try { | ||
| tryCount = Integer.parseInt(tryCountInput); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("시도 횟수는 양의 정수여야 합니다."); | ||
| } | ||
|
|
||
| // 양수인지 검증 | ||
| if (tryCount <= 0) { | ||
| throw new IllegalArgumentException("시도 횟수는 양의 정수여야 합니다."); | ||
| } | ||
|
|
||
| return (tryCount); | ||
| } | ||
|
|
||
| public static void carNameValidator(String name) { | ||
| if (name == null || name.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("자동차 이름은 빈 값이 될 수 없습니다."); | ||
| } | ||
|
|
||
| if (name.length() > MAX_NAME_LENGTH) { | ||
| throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없습니다."); | ||
| } | ||
|
|
||
| if (name.contains(",")) { | ||
| throw new IllegalArgumentException("자동차 이름에 ','를 포함할 수 없습니다."); | ||
| } | ||
|
Member
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 comment
The reason will be displayed to describe this comment to others. Learn more.
개인적인 의견으로는 Car이나 Cars의 프린트하는 부분이나 비지니스로직 실행하는 부분이 다른 부분에서 값을 받아서 처리하는게 확장성이 더 좋을 것 같다는 생각이 있어요!