-
Notifications
You must be signed in to change notification settings - Fork 29
[손국균_BackEnd] 1주차 과제 제출합니다. #10
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,105 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| GameManager gameManager = new GameManager(); | ||
| gameManager.start(); | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| private final String name; | ||
| private int distance = 0; | ||
| public Car (String name) { | ||
| validateName(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| private void validateName(String name){ | ||
| if (name == null || name.isBlank() || name.length() > 5 || name.contains(" ")) { | ||
| throw new IllegalArgumentException ("올바른 값을 입력해주세요. (문자열을 입력하세요.)"); | ||
| } | ||
| } | ||
|
|
||
| public void move(int randomNumber) { | ||
| if (randomNumber >= 4) { | ||
| distance ++; | ||
| } | ||
| } | ||
|
|
||
| public void printStatus() { | ||
| System.out.println(name + " : " + "-".repeat(distance)); | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return distance; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
|
|
||
| class GameManager { | ||
|
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.
다음과 같은 요구사항이 존재합니다. GameManager 클래스에 정의된 메소드들은 너무 많은 일을 하고 있는 거 같아요 👀 왜 한 가지 일만 해야하는지, 현 상황에서 분리를 할려면 코드를 어떻게 작성해야하는지 고민해보면 좋을 거 같습니다 ! |
||
| public void start() { | ||
| List<Car> cars = createCars(); | ||
| System.out.println("시도할 회수는 몇 회인가요?"); | ||
| String inputNum = Console.readLine(); | ||
| int attempts = Integer.parseInt(inputNum); | ||
| if (attempts <= 0 ) { | ||
| throw new IllegalArgumentException ("0이상 입력하세요."); | ||
| } | ||
|
Comment on lines
+52
to
+55
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.
|
||
| for(int i = 1; i <= attempts; i++){ | ||
| for (Car car : cars){ | ||
| int randomNumber = Randoms.pickNumberInRange(0, 9); | ||
| car.move(randomNumber); | ||
| car.printStatus(); | ||
| } | ||
| System.out.println(); | ||
| System.out.println("실행 결과"); | ||
| } | ||
| WinnerPicker winnerPicker = new WinnerPicker(); | ||
| winnerPicker.pirntWinner(cars); | ||
| } | ||
|
|
||
| private static List<Car> createCars() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String inputName = Console.readLine(); | ||
| String[] names = inputName.split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : names) { | ||
| cars.add(new Car(name)); | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| for (int j = i + 1; j < cars.size(); j++) { | ||
| if (cars.get(i).getName().equals(cars.get(j).getName())) { | ||
| throw new IllegalArgumentException("중복된 값 입니다."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+74
to
+83
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.
depth가 지금 4인 거 같아요. 메소드로 분리하는 방법도 있고, Set를 통해 중복 검사를 하는 방법도 있을 거 같아요 |
||
| return cars; | ||
| } | ||
| } | ||
|
|
||
| class WinnerPicker { | ||
| public void pirntWinner(List<Car> cars) { | ||
| int maxdistance = 0; | ||
| for (Car car : cars) { | ||
| if (car.getDistance() > maxdistance) { | ||
| maxdistance = car.getDistance(); | ||
| } | ||
| } | ||
|
Comment on lines
+91
to
+95
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. 자바에서 기본적으로 제공해주는 라이브러리 중 Math가 있습니다. max()메소드를 활용한다면 어떻게 개선될 지 고민해보시면 좋을 거 같아요 ! |
||
|
|
||
| List<String> winnercars = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getDistance() == maxdistance) { | ||
| winnercars.add(car.getName()); | ||
| } | ||
| } | ||
| System.out.println("최종 우승자 : " + String.join(", ", winnercars)); | ||
| } | ||
| } | ||
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.
Python은 파일 하나에 여러개의 클래스를 구현해서 사용하는 것으로 알고 있는데, Java는 해당 방식보다는 클래스 파일을 분리해서 개발을 하는 거 같아요 ! 2주차 과제에서는 한 번 쪼개보면 좋을 거 같습니다