-
Notifications
You must be signed in to change notification settings - Fork 29
[권민상] 1주차 과제 제출 #7
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,84 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.*; | ||
|
|
||
| class Car { | ||
| String name; | ||
| int moveCount; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| this.moveCount = 0; | ||
| } | ||
|
|
||
| public void move() { | ||
| moveCount++; | ||
| } | ||
|
|
||
| public int getMoveCount() { | ||
| return moveCount; | ||
| } | ||
| } | ||
|
|
||
| public class Application { | ||
|
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. Application에 로직이 많이 몰려 있어서 입력/검증/실행/출력을 메소드 또는 클래스로 분리하면 가독성이 더 좋아질 것 같습니다. |
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Scanner in = new Scanner(System.in); | ||
|
|
||
| //부릉부릉 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String a = in.nextLine(); | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : a.split(",")) { | ||
| if (name.isEmpty()) {throw new IllegalArgumentException("왜안적음");} | ||
|
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. isBlank() 도 알아두시면 좋을 것 같습니다. |
||
| if (name.length() > 5) {throw new IllegalArgumentException("이름5자초과됨");} | ||
| if (name.contains(" ")) {throw new IllegalArgumentException("공백포함됨");} | ||
| cars.add(new Car(name)); | ||
| } | ||
| Set<String> dupCheck = new HashSet<>(); | ||
| for (Car c : cars){dupCheck.add(c.name);} | ||
|
|
||
| if (dupCheck.size() != cars.size()) {throw new IllegalArgumentException("중복이름있음");} | ||
|
|
||
| //끼이익쾅 | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| int n; | ||
| try { | ||
| n = in.nextInt(); | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("0번이하"); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("숫자안적음"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| System.out.printf("%n실행 결과%n"); | ||
| for (int i=n;i>0;i--) { | ||
| for (Car car : cars) { | ||
| int ran = Randoms.pickNumberInRange(0,9); | ||
| if (ran >= 4) {car.move();} | ||
| String howMove = "-".repeat(car.getMoveCount()); | ||
| System.out.printf("%s : %s%n", car.name,howMove); | ||
| } | ||
| System.out.println(" "); //한칸 띄기용 | ||
| } | ||
|
|
||
| //띠리리리리리링 | ||
| int max = 0; | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getMoveCount() > max){ | ||
| max = car.getMoveCount(); | ||
| winners.clear(); | ||
| winners.add(car.name); | ||
| } else if (car.getMoveCount() == max) { | ||
| winners.add(car.name); | ||
| } | ||
| } | ||
|
|
||
| //조아요 조아요 | ||
|
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.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.
rivate으로 두고 getter를 통해 접근하도록 하면 캡슐화 측면에서 좋아질 것이라고 생각합니다