-
Notifications
You must be signed in to change notification settings - Fork 54
[정준영_BackEnd]1주차 과제 제출합니다. #62
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
9ad51a7
69fafb5
72b2395
e657c6c
9cf6c20
9720c85
b593fc2
4454dae
1b64d78
87a90de
247f236
cc62257
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 |
|---|---|---|
|
|
@@ -2,6 +2,6 @@ | |
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| new Racing().start(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package racingcar; | ||
|
|
||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position; | ||
|
|
||
| public Car(String name) | ||
| { | ||
| verificationName(name); | ||
| this.name = name; | ||
| this.position = 0; | ||
| } | ||
|
|
||
| public void verificationName(String name) | ||
| { | ||
| if (name.length() > 5) | ||
| { | ||
| throw new IllegalArgumentException("차 이름은 5자 이하만 가능합니다."); | ||
| } | ||
| } | ||
|
|
||
| public void move() | ||
| { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) | ||
| { | ||
| this.position++; | ||
| } | ||
| } | ||
|
|
||
| public String getName() {return name;} | ||
|
|
||
| public int getPosition() {return position;} | ||
| } | ||
|
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. EOF 에 대해 알아보시면 좋을거같아요
Author
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. 오 생각을 못해본 부분이었어요;; 덕분에 좋은 공부가 되네요. 다음부터는 신경 써볼게요! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
|
|
||
|
|
||
| public class Racing | ||
| { | ||
| private final List<Car> cars = new ArrayList<>(); | ||
| private int rounds; | ||
|
|
||
| public void start() | ||
| { | ||
| inputCars(); | ||
| inputRounds(); | ||
| play(); | ||
| printWinner(); | ||
| } | ||
|
|
||
| private void inputCars() | ||
| { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String[] names = Console.readLine().split(","); | ||
| for (String name : names) | ||
| { | ||
| cars.add(new Car(name.trim())); | ||
| } | ||
| } | ||
|
|
||
| private void inputRounds() | ||
| { | ||
| System.out.println("시도할 횟수는 몇회인가요?"); | ||
| rounds = Integer.parseInt(Console.readLine().trim()); | ||
| verificationRound(rounds); | ||
| } | ||
|
|
||
| private void verificationRound(int value) | ||
| { | ||
| if (value <= 0) | ||
| { | ||
| throw new IllegalArgumentException("시도 회수는 1 이상이어야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| private void play() | ||
| { | ||
| for (int i = 0; i < rounds; i++) | ||
| { | ||
| raceRound(); | ||
| printRaceStatus(); | ||
| } | ||
| } | ||
|
|
||
| private void raceRound() | ||
| { | ||
| for (Car car : cars) | ||
| { | ||
| car.move(); | ||
| } | ||
| } | ||
|
|
||
| private void printRaceStatus() | ||
| { | ||
| for (Car car : cars) | ||
| { | ||
| System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private void printWinner() | ||
| { | ||
| int maxPosition = findMaxPosition(); | ||
| List<String> winners = getWinners(maxPosition); | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
|
|
||
| private List<String> getWinners(int maxPosition) | ||
| { | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) | ||
| { | ||
| if (car.getPosition() == maxPosition) | ||
| { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| private int findMaxPosition() | ||
| { | ||
| int max = 0; | ||
| for (Car car : cars) | ||
| { | ||
| if (car.getPosition() > max) | ||
| { | ||
| max = car.getPosition(); | ||
| } | ||
|
Comment on lines
+92
to
+100
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. 저는 개인적으로 이런 형식의 코드를 쓸때 Stream을 사용하는것을 선호하는데, 준영님의 의견은 어떤지 궁금합니다. |
||
| } | ||
| return max; | ||
| } | ||
| } | ||
|
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. 동일하게 EOF입니다 |
||
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.
매직넘버의 개념을 처음 알았습니다;; 확실히 협업에서는 신경써야 하는 부분 같네요. 그러면 제 코드에서는 0과 9를 상수화 하는게 좋을까요 아니면 숫자 전부 다 하는 것이 좋을까요? 상수화 기준을 잡기가 모호하네요;;
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.
제 개인적인 생각이지만 매직넘버를 상수화 할때 숫자의 의도를 알수있는지 여부가 중요한거같아요
처럼 0과같이 존재 여부, 인덱스 시작점 등 직관적인 숫자들은 매직넘버화할 필요가 없지만 다음과같은 경우에는 상수화가 필요할수있겠네요.
지금 코드에서 0~9중에 숫자 하나를 랜덤으로 뽑아서 4이상일때의 조건이라는것을 알수있지만 "왜 이 숫자인지" 0,9,4 라는 숫자가 해당 코드만 보고서는 그 의미를 알 수 없습니다.
이처럼 비즈니스 로직에서 규칙,조건을 의미하는 숫자들은 상수화하는편이 좋은거같습니다.