-
Notifications
You must be signed in to change notification settings - Fork 97
[그리디] 서현진 자동차 경주 미션 3, 4단계 제출합니다. #158
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: nonactress
Are you sure you want to change the base?
Changes from 29 commits
fbfab99
1c54f83
dee665e
a9af148
c6d9acc
560ac39
aeb4ab7
dcb4ec5
c11d098
480f751
c0d037a
62b3941
864e6c0
97b4d95
035ecdd
0de29df
f660a28
7f5f429
630df3a
22dc5a0
704cf09
c19c73a
b608f5b
b471111
cd70b8a
66e64b6
68814e7
4f1b377
ced1092
fd0a317
76330fc
03ef929
3e12240
cd944bc
5ca9b26
fb563ce
482cf13
d5ef197
ad763d8
cab4c5f
b160676
2831966
ada34f1
eb25bf2
c2e519f
43190b6
f6c6e25
3fb578e
7d22b1d
72497ef
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,22 @@ | ||
|
|
||
| --- | ||
| ## 작동 방식 | ||
|
|
||
| 애플리케이션은 다음 정보를 입력하라는 메시지를 표시합니다: | ||
|
|
||
| 1. **자동차 수:** 경주에 참가할 자동차의 수 | ||
| 2. **라운드 수:** 경주가 지속될 라운드 수 | ||
| 3. **자동차 이름:** 각 자동차의 이름 | ||
|
|
||
| 필요한 정보를 입력하면 경주가 시작됩니다. 각 라운드마다 자동차는 전진할 기회를 갖습니다. | ||
| 0부터 9 사이에서 무작위로 생성된 숫자가 4보다 크면 자동차는 전진합니다. | ||
| 경주가 끝나면 각 자동차가 이동한 총 거리를 표시하고 우승자(들)를 발표합니다. | ||
|
|
||
| ## 클래스 설명 | ||
|
|
||
| * **`Car.java`**: 이름과 이동 횟수를 가진 자동차를 나타냅니다. | ||
| * **`RaceSetting.java`**: 경주를 설정하는 클래스입니다. 자동차 수, 라운드 수, 자동차 이름에 대한 사용자 입력을 받습니다. 또한 경주 실행과 자동차 이동에 관한 로직을 포함합니다. | ||
| * **`RaceWinner.java`**: 이 클래스는 `RaceSetting`을 확장하며 경주의 우승자를 결정하는 역할을 합니다. 가장 멀리 이동한 자동차를 찾아 그 이름을 우승자로 출력합니다. | ||
| * **`Race.java`**: 애플리케이션의 주요 진입점입니다. 경주를 시작하기 위해 `RaceWinner`의 인스턴스를 생성합니다. | ||
|
|
||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| public class Application { | ||
| public static void main(String[] args) { | ||
| RacingController racingController = new RacingController(); | ||
| racingController.run(); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| public class Car { | ||
|
|
||
| private String name; | ||
| private int position = 0; | ||
|
|
||
|
|
||
| public Car() { | ||
| } | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void move() { | ||
| this.position++; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Cars { | ||
| private final List<Car> cars; | ||
| GenerateRandom generateRandom = new GenerateCarRandom(); | ||
| //GenerateRandom testGenerateRandom = new TestGenerateRandom(); //단위 테스트용 코드 | ||
|
|
||
| public Cars(String[] carNames) { | ||
| this.cars = new ArrayList<>(); | ||
| for (String name : carNames) { | ||
| cars.add(new Car(name)); | ||
| } | ||
| } | ||
|
|
||
| public void moveAll() { | ||
| for (Car car : cars) { | ||
| move(car); | ||
| } | ||
| } | ||
|
|
||
| private void move(Car car) { | ||
| if (generateRandom.generate()) { | ||
| car.move(); | ||
| } | ||
| // if (testGenerateRandom.generate()) { | ||
| // car.move(); | ||
| // } | ||
| } | ||
|
|
||
| public List<String> findWinners() { | ||
| int maxPosition = findMaxPosition(); | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| isFindingFirstPizes(winners, car, maxPosition); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| private void isFindingFirstPizes(List<String> winners, Car car, int maxPosition) { | ||
|
||
| if (car.getPosition() == maxPosition) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
|
|
||
| private int findMaxPosition() { | ||
| int maxPosition = 0; | ||
| for (Car car : cars) { | ||
| maxPosition = Math.max(car.getPosition(), maxPosition); | ||
| } | ||
| return maxPosition; | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return Collections.unmodifiableList(cars); | ||
|
|
||
| /*return this.cars; | ||
| *위 방식을 안쓴 이유는 객체를 받아서 다른 메소드나 클래스에서 | ||
| *수정이 가능해 지기 때문에 위 컬렉션 메소드로 반환값 설정 | ||
| * */ | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class GenerateCarRandom implements GenerateRandom { | ||
| private Random random = new Random(); | ||
|
|
||
| @Override | ||
| public boolean generate() { | ||
| if (random.nextInt(10) > MOVE_CONDITION) { | ||
| return true; | ||
| } | ||
| return false; | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| public interface GenerateRandom { | ||
| static final int MOVE_CONDITION = 4; | ||
| boolean generate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static String[] getCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String[] splitCar = scanner.nextLine().split(","); | ||
| return splitCar; | ||
| } | ||
|
|
||
| public static int getRaceCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| int count = scanner.nextInt(); | ||
| scanner.nextLine(); // 개행 문자 제거 | ||
| return count; | ||
| } | ||
|
|
||
| public static boolean isValid(String [] cars){ | ||
| if(cars.length == 1 ){ | ||
| System.out.println("한 개 이상의 자동차를 입력해주세요!"); | ||
| return false; | ||
| } | ||
| for (String car : cars) { | ||
| if(car.trim().isEmpty()) | ||
| { | ||
| System.out.println("자동차의 이름이 공백일 순 없습니다!"); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| public static void printResultMessage() { | ||
| System.out.println("\n실행 결과"); | ||
| } | ||
|
|
||
| public static void printRoundResult(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.print(car.getName() + " : "); | ||
| printCarPosition(car); | ||
| System.out.println(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printCarPosition(Car car) { | ||
| for (int i = 0; i < car.getPosition(); i++) { | ||
|
||
| System.out.print("-"); | ||
| } | ||
| } | ||
|
|
||
| public static void printWinners(List<String> winners) { | ||
| String winnerNames = String.join(", ", winners); | ||
| System.out.println("최종 우승자 : " + winnerNames); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class RacingController { | ||
| public void run() { | ||
| String[] carNames = InputView.getCarNames(); | ||
| int raceCount = InputView.getRaceCount(); | ||
|
|
||
| Cars cars = new Cars(carNames); | ||
|
|
||
| OutputView.printResultMessage(); | ||
| for (int i = 0; i < raceCount; i++) { | ||
| cars.moveAll(); // Model의 상태 변경 요청 | ||
| OutputView.printRoundResult(cars.getCars()); // View에 출력 요청 | ||
| } | ||
|
|
||
| OutputView.printWinners(cars.findWinners()); | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class TestGenerateRandom implements GenerateRandom { | ||
| private final int TEST_NUM = 5; | ||
|
|
||
| @Override | ||
| public boolean generate() { | ||
| if (TEST_NUM > MOVE_CONDITION) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
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.
안 쓰는 코드는 삭제해주세요!
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.
넵