Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/main/java/racingcar/Application.java
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(",");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join, repeat, split 등 자바에서 제공하는 api를 적극적으로 사용해 주셨네요!

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

Choose a reason for hiding this comment

The 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());
}
}
37 changes: 37 additions & 0 deletions src/main/java/racingcar/Car.java
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repeat 👍


public String getName(){
return name;
}

public int getPosition() {
return position;
}
}
25 changes: 25 additions & 0 deletions src/main/java/racingcar/GameResult.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런것들도 따로 메서드로 분리할 수 있을 것 같아요!
ex) private int findWinnerPosition(), private int findMaxPosition()


for (Car cars : winners) {
if (cars.getPosition() == winnerPosition) {
winners.add(cars);
}

}
return winners;
}
}