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
1 change: 1 addition & 0 deletions Java_Racingcar_2026-1
Submodule Java_Racingcar_2026-1 added at 079e54
14 changes: 13 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package racingcar;
import java.util.*;

public class Application {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Application에 로직이 많이 몰려 있어서 입력/검증/실행/출력을 메소드 또는 클래스로 분리하면 가독성이 더 좋아질 것 같습니다.

public static void main(String[] args) {
// TODO: 프로그램 구현
InputView input = new InputView();
InputValidator iv = new InputValidator();

List<Car> cars = iv.CarNamesValidator(input.inputCarName());
int n = iv.IntegerValidator(input.inputRepeatN());


Gaming g = new Gaming(cars, n);
g.start();

Winner w = new Winner(cars);
System.out.println("최종 우승자 : " + String.join(", ", w.findWinner()));
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar;

public class Car {
private final String name;
private int moveCount;

public Car(String name) {
this.name = name;
this.moveCount = 0;
}

public void move() {
moveCount++;
}
Comment on lines +4 to +14
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍



public String getName() {
return name;
}
public int getMoveCount() {
return moveCount;
}
}
28 changes: 28 additions & 0 deletions src/main/java/racingcar/Gaming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.List;

public class Gaming {
private final int n;
private final List<Car> cars;
public Gaming(List<Car> cars, int n){
this.cars = cars;
this.n = n;
}

public void start() {
System.out.printf("%n실행 결과%n");
for (int i=n;i>0;i--) {
for (Car car : cars) {
int ran = Randoms.pickNumberInRange(0,9);
final int movingNumber = 4;
if (ran >= movingNumber) {car.move();}
String howMove = "-".repeat(car.getMoveCount());
System.out.printf("%s : %s%n", car.getName(),howMove);
}
System.out.println(" "); //한칸 띄기용
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/racingcar/InputValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racingcar;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class InputValidator {
public List<Car> CarNamesValidator(String name) {
List<Car> cars = new ArrayList<>();
for (String a : name.split(",")) {
CarNameValidator(a);
cars.add(new Car(a));
}
Set<String> dupCheck = new HashSet<>();
for (Car c : cars){dupCheck.add(c.getName());}
if (dupCheck.size() != cars.size()) {throw new IllegalArgumentException("중복이름있음");}
return cars;
}
Comment on lines +15 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이름 검증 메서드에서 같이 검증하면 좋을 것 같습니다.


private void CarNameValidator(String name) {
if (name.isEmpty()) {throw new IllegalArgumentException("왜안적음");}
if (name.length() > 5) {throw new IllegalArgumentException("이름5자초과됨");}
if (name.contains(" ")) {throw new IllegalArgumentException("공백포함됨");}
}
Comment on lines +21 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

검증 책임을 별도 메서드로 나눈 점은 좋지만, 메서드명은 소문자로 시작하도록 자바 네이밍 컨벤션을 맞추면 좋을 것 같습니다


public int IntegerValidator(String n) {
int count;
try {count = Integer.parseInt(n);}
catch (NumberFormatException e) {throw new IllegalArgumentException("숫자안적음");}

if (count <= 0) {throw new IllegalArgumentException("0번이하");}
return count;
}
}
15 changes: 15 additions & 0 deletions src/main/java/racingcar/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package racingcar;

import java.util.Scanner;

public class InputView {
private final Scanner in = new Scanner(System.in);
public String inputCarName() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
return in.nextLine();
}
public String inputRepeatN() {
System.out.println("시도할 회수는 몇회인가요?");
return in.nextLine();
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/Winner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar;
import java.util.*;

public class Winner {
private final List<Car> cars;
public Winner(List<Car> cars) {
this.cars = cars;
}
public List<String> findWinner() {
int max = 0;
List<String> winners = new ArrayList<>();
for (Car car : cars) {
if (car.getMoveCount() > max) {
max = car.getMoveCount();
winners.clear();
winners.add(car.getName());
} else if (car.getMoveCount() == max) {
winners.add(car.getName());
}
}
return winners;
}
}