-
Notifications
You must be signed in to change notification settings - Fork 55
[서유정_BackEnd] 2주차 과제 제출합니다. #45
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,33 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import static camp.nextstep.edu.missionutils.Console.readLine; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); | ||
| String[] names = readLine().split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
|
|
||
| for (String name: names) { | ||
| cars.add(new Car(name)); | ||
| } | ||
|
|
||
| System.out.println("시도할 횟수는 몇 회인가요?"); | ||
| int count = Integer.parseInt(readLine()); | ||
|
|
||
| System.out.println("실행 결과"); | ||
| for (int i = 0; i < count; i++) { | ||
| for (Car car: cars){ | ||
| car.move(); | ||
| System.out.println(car.getName() + " : " + car.getPositionToString()); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
Comment on lines
+21
to
+27
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. 함수를 사용해 코드를 기능 단위로 분리할 수 있습니다. 분리된 함수에 역할이 명확히 드러나도록 이름을 잘 지어 놓으면 함수 이름만 읽어도 어떤 작업을 하는 코드인지 알 수 있고, 전체적인 흐름 파악도 쉬워져요. |
||
|
|
||
| GameResult gameResult = new GameResult(); | ||
| gameResult.getWinners(); | ||
| System.out.print("최종 우승자 : " + String.join(", ") + gameResult.getWinners()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package racingcar; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange; | ||
|
|
||
| public class Car { | ||
| private String name; | ||
| private int position = 0; | ||
|
|
||
|
Comment on lines
+4
to
+8
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 Car(String name){ | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException("오류: 문자열은 최대 5글자까지만 입력할 수 있습니다."); | ||
| } | ||
| this.name = name; | ||
| } | ||
|
Comment on lines
+9
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. Car의 생성자가 검증의 책임을 갖도록 해주셨네요. 이 부분을 private 메서드로 분리하는건 어떨까요? |
||
|
|
||
| public Car(int position){ | ||
| this.position = position; | ||
| } | ||
|
|
||
| public void move(){ | ||
| if (pickNumberInRange(0, 9) >= 4) { | ||
| position += 1; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+24
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. 조건문의 4와 같은 값을 상수로 선언하면 어떤 장점이 생길까요? |
||
|
|
||
| public String getPositionToString(){ | ||
| return "-".repeat(position); | ||
| } | ||
|
Comment on lines
+26
to
+28
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. repeat 👍 |
||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class GameResult { | ||
| private List<Car> winners; | ||
|
|
||
| public List<Car> getWinners() { | ||
| int winnerPosition = 0; | ||
|
|
||
| for (Car cars : winners) { | ||
| if (cars.getPosition() > winnerPosition) { | ||
| winnerPosition = cars.getPosition(); | ||
| } | ||
| } | ||
|
Comment on lines
+11
to
+15
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 (Car cars : winners) { | ||
| if (cars.getPosition() == winnerPosition) { | ||
| winners.add(cars); | ||
| } | ||
|
|
||
| } | ||
| 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.
join, repeat, split 등 자바에서 제공하는 api를 적극적으로 사용해 주셨네요!