-
Notifications
You must be signed in to change notification settings - Fork 55
[황명하_BackEnd] 2주차 과제 제출합니다 #57
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
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,64 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Scanner sc = new Scanner(System.in); | ||
|
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. 여기서는 굳이 scanner가 필요하지 않을 것 같아요 |
||
|
|
||
| List<Car> cars = getCars(sc); | ||
| int count = getTrialCount(sc); | ||
|
|
||
| raceCars(cars, count); | ||
| List<String> winners = getWinners(cars); | ||
|
|
||
| printWinners(winners); | ||
| } | ||
|
|
||
| private static List<Car> getCars(Scanner sc) { | ||
|
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. 메소드를 분리해주셨네요 좋습니다 |
||
| System.out.print("차 이름들을 입력하세요 (쉼표로 구분): "); | ||
| String[] names = Console.readLine().split(","); | ||
|
|
||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : names) { | ||
| cars.add(new Car(name.trim())); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| private static int getTrialCount(Scanner sc) { | ||
| System.out.print("시도할 횟수를 입력하세요: "); | ||
| return sc.nextInt(); | ||
| } | ||
|
|
||
| private static void raceCars(List<Car> cars, int count) { | ||
| for (int i = 0; i < count; i++) { | ||
| for (Car car : cars) { | ||
| car.movement(); | ||
| System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static List<String> getWinners(List<Car> cars) { | ||
| int maxPosition = 0; | ||
| for (Car car : cars) { | ||
| maxPosition = Math.max(maxPosition, car.getPosition()); | ||
| } | ||
|
|
||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
|
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. 좀 더 함수들을 세분화할 수 있을 것 같아요 |
||
| if (car.getPosition() == maxPosition) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| private static void printWinners(List<String> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package racingcar; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다."); | ||
| } | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void movement() { | ||
| int randomNumber = pickNumberInRange(0, 9); | ||
| if (randomNumber >= 4) { | ||
| position++; | ||
| } | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } |
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.
클래스가 여러 책임을 가지고 있어서 더 분리할 수 있을 것 같아요