Skip to content
Open
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
100 changes: 99 additions & 1 deletion src/main/java/racingcar/Application.java
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 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python은 파일 하나에 여러개의 클래스를 구현해서 사용하는 것으로 알고 있는데, Java는 해당 방식보다는 클래스 파일을 분리해서 개발을 하는 거 같아요 ! 2주차 과제에서는 한 번 쪼개보면 좋을 거 같습니다

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 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

XX.parseInt 메소드의 경우 String을 다른 형으로 변환해주는 메소드로 알고 있습니다. 만약에 String이 아닌 다른 자료형이 들어온다면 어떻게 될까요 👀

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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));
}
}