-
Notifications
You must be signed in to change notification settings - Fork 29
[권민상] 2주차 과제 제출 #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
079e54f
c432503
d00b252
f62ec32
87b6a21
b033584
31f1ff5
6835cf9
001976d
ec92a60
0a00c16
0827898
361d468
14f90b4
c862109
010b52e
84a195c
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,19 @@ | ||
| package racingcar; | ||
| import java.util.*; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| InputView input = new InputView(); | ||
| InputValidator iv = new InputValidator(); | ||
|
|
||
| List<Car> cars = iv.CarNamesValidator(input.inputCarName()); | ||
| int n = iv.IntegerValidator(input.inputRepeatN()); | ||
|
|
||
|
|
||
| Gaming g = new Gaming(cars, n); | ||
| g.start(); | ||
|
|
||
| Winner w = new Winner(cars); | ||
| System.out.println("최종 우승자 : " + String.join(", ", w.findWinner())); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package racingcar; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int moveCount; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| this.moveCount = 0; | ||
| } | ||
|
|
||
| public void move() { | ||
| moveCount++; | ||
| } | ||
|
Comment on lines
+4
to
+14
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. 👍 |
||
|
|
||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getMoveCount() { | ||
| return moveCount; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Gaming { | ||
| private final int n; | ||
| private final List<Car> cars; | ||
| public Gaming(List<Car> cars, int n){ | ||
| this.cars = cars; | ||
| this.n = n; | ||
| } | ||
|
|
||
| public void start() { | ||
| System.out.printf("%n실행 결과%n"); | ||
| for (int i=n;i>0;i--) { | ||
| for (Car car : cars) { | ||
| int ran = Randoms.pickNumberInRange(0,9); | ||
| final int movingNumber = 4; | ||
| if (ran >= movingNumber) {car.move();} | ||
| String howMove = "-".repeat(car.getMoveCount()); | ||
| System.out.printf("%s : %s%n", car.getName(),howMove); | ||
| } | ||
| System.out.println(" "); //한칸 띄기용 | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class InputValidator { | ||
| public List<Car> CarNamesValidator(String name) { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String a : name.split(",")) { | ||
| CarNameValidator(a); | ||
| cars.add(new Car(a)); | ||
| } | ||
| Set<String> dupCheck = new HashSet<>(); | ||
| for (Car c : cars){dupCheck.add(c.getName());} | ||
| if (dupCheck.size() != cars.size()) {throw new IllegalArgumentException("중복이름있음");} | ||
| return cars; | ||
| } | ||
|
Comment on lines
+15
to
+19
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 void CarNameValidator(String name) { | ||
| if (name.isEmpty()) {throw new IllegalArgumentException("왜안적음");} | ||
| if (name.length() > 5) {throw new IllegalArgumentException("이름5자초과됨");} | ||
| if (name.contains(" ")) {throw new IllegalArgumentException("공백포함됨");} | ||
| } | ||
|
Comment on lines
+21
to
+25
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. 검증 책임을 별도 메서드로 나눈 점은 좋지만, 메서드명은 소문자로 시작하도록 자바 네이밍 컨벤션을 맞추면 좋을 것 같습니다 |
||
|
|
||
| public int IntegerValidator(String n) { | ||
| int count; | ||
| try {count = Integer.parseInt(n);} | ||
| catch (NumberFormatException e) {throw new IllegalArgumentException("숫자안적음");} | ||
|
|
||
| if (count <= 0) {throw new IllegalArgumentException("0번이하");} | ||
| return count; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private final Scanner in = new Scanner(System.in); | ||
| public String inputCarName() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| return in.nextLine(); | ||
| } | ||
| public String inputRepeatN() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| return in.nextLine(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package racingcar; | ||
| import java.util.*; | ||
|
|
||
| public class Winner { | ||
| private final List<Car> cars; | ||
| public Winner(List<Car> cars) { | ||
| this.cars = cars; | ||
| } | ||
| public List<String> findWinner() { | ||
| int max = 0; | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getMoveCount() > max) { | ||
| max = car.getMoveCount(); | ||
| winners.clear(); | ||
| winners.add(car.getName()); | ||
| } else if (car.getMoveCount() == max) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return 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.
Application에 로직이 많이 몰려 있어서 입력/검증/실행/출력을 메소드 또는 클래스로 분리하면 가독성이 더 좋아질 것 같습니다.