Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bb90cf8
feat: 시도_횟수_0_이하_예외_처리
Superpinoiman Mar 28, 2026
4d4980a
feat: 시도_횟수_숫자_아님_예외_처리
Superpinoiman Mar 28, 2026
0fdd0c4
feat: 자동차_이름_5자_초과_예외_처리
Superpinoiman Mar 28, 2026
f5cc4a7
feat: 자동차_이름_공백_포함_예외_처리
Superpinoiman Mar 28, 2026
9287cb9
feat: 자동차_이름_빈값_예외_처리
Superpinoiman Mar 28, 2026
91902f8
feat: 구현해야할 목록
Superpinoiman Mar 28, 2026
0a232c0
feat: 구현해야할 목록
Superpinoiman Mar 28, 2026
bc89b37
feat: 자동차 이름과 시도횟수 입력
Superpinoiman Mar 28, 2026
ec06bea
feet: 자동차 이름과 시도횟수 입력
Superpinoiman Mar 28, 2026
53803f4
feet: 레이싱 진행 출력
Superpinoiman Mar 28, 2026
1072cf3
feet: 우승자 출력
Superpinoiman Mar 28, 2026
71aa530
refactor: Car 클래스를 분리하고 객체 데이터 접근을 메서드 호출로 변경
Superpinoiman Apr 3, 2026
4943d0f
refactor: 레이스 진행을 Race클래스로 분리
Superpinoiman Apr 4, 2026
c982b0a
refactor: 레이스 결과 출력을 RaceResult 클래스로 분리
Superpinoiman Apr 4, 2026
c52933a
refactor: cars 리스트 접근제어자 변경
Superpinoiman Apr 4, 2026
aeb86db
refactor: 불필요한 else문 제거
Superpinoiman Apr 4, 2026
a675c14
refactor: 메소드명을 직관적으로 변경
Superpinoiman Apr 4, 2026
65439ce
refactor: 불필요한 new연산자 제거
Superpinoiman Apr 4, 2026
06616b1
refactor: cars 리스트를 전용 메소드로 호출하도록 변경
Superpinoiman Apr 4, 2026
ffd1ba0
refactor: cars 리스트를 전용 메소드로 호출하도록 변경
Superpinoiman Apr 4, 2026
2bb3ace
refactor: 사용되지 않는 반환 삭제
Superpinoiman Apr 4, 2026
34e1642
refactor: 자동차이름과 라운드 수 입력기능을 RaceSetup 클래스로 분리
Superpinoiman Apr 4, 2026
8fa360d
refactor: 생성자 주입 방식으로 구조 변경
Superpinoiman Apr 4, 2026
622f466
style: 공백 추가
Superpinoiman Apr 4, 2026
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
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
구현해야할 목록

자동차 이름과 시도횟수 입력

레이싱 진행 출력

우승자 출력
9 changes: 9 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package racingcar;

public class Application {

public static void main(String[] args) {
// TODO: 프로그램 구현
RaceSetup setup = new RaceSetup();
setup.raceSetup();

Race race = new Race(setup.getCarsList(), setup.getRound());
race.progressRounds();

RaceResult result = new RaceResult(setup.getCarsList());
result.printWinner();
}
}
34 changes: 34 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar;

public class Car {
private String name;
private int score = 0;
private boolean winner = false;

Car(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public int getScore() {
return this.score;
}

public boolean isWinner() {
if (this.winner) {
return true;
}
return false;
}

public void move() {
this.score++;
}

public void win() {
this.winner = true;
}
}
45 changes: 45 additions & 0 deletions src/main/java/racingcar/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;


public class Race {
private ArrayList<Car> cars;
private int round;

Race(ArrayList<Car> cars, int round) {
this.cars = cars;
this.round = round;
}

public void progressRounds() {
System.out.println("실행 결과");
for (int i = 0; i < this.round; i++) {
moveCars();
printScore();
System.out.println(" ");
}
}

private void moveCars() {
int random;

for (int i = 0; i < this.cars.size(); i++) {
random = Randoms.pickNumberInRange(0, 9);
if (random >= 4) {
this.cars.get(i).move();
}
}
}

private void printScore() {
for (int i = 0; i < this.cars.size(); i++) {
System.out.print(this.cars.get(i).getName() + " : ");
for (int j = 0; j < this.cars.get(i).getScore(); j++) {
System.out.print("-");
}
System.out.println(" ");
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/racingcar/RaceResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package racingcar;

import java.util.ArrayList;

public class RaceResult {
private ArrayList<Car> cars;
RaceResult(ArrayList<Car> cars) {
this.cars = cars;
}

public void printWinner() {
boolean flag = true;

this.findWinner();
System.out.print("최종 우승자 : ");

for (int i = 0; i < this.cars.size(); i++) {
if (this.cars.get(i).isWinner() && !flag) {
System.out.print(", ");
System.out.print(this.cars.get(i).getName());
}
if (this.cars.get(i).isWinner() && flag) {
System.out.print(this.cars.get(i).getName());
flag = false;
}
}
}

private void findWinner() {
int maxScore = 0;

for (int i = 0; i < this.cars.size(); i++) {
if (this.cars.get(i).getScore() >= maxScore) {
maxScore = this.cars.get(i).getScore();
}
}

for (int i = 0; i < cars.size(); i++) {
if (cars.get(i).getScore() == maxScore) {
cars.get(i).win();
}
}
}
}
69 changes: 69 additions & 0 deletions src/main/java/racingcar/RaceSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;

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

public class RaceSetup {
private ArrayList<Car> cars;
private int round;

RaceSetup(){}

public void raceSetup() {
cars = this.setupCars();
this.inputRound();
}

public ArrayList<Car> getCarsList() {
return cars;
}

public int getRound() {
return round;
}

private boolean isDuplicateName(ArrayList<Car> cars) {
Set<String> nameSet = new HashSet<>();

for (int i = 0; i < cars.size() ; i++) {
if (!nameSet.add(cars.get(i).getName())) {
return true;
}
}
return false;
}

private ArrayList<Car> setupCars() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine();
String[] names = input.split(",");
ArrayList<Car> cars = new ArrayList<>();

for (int i = 0; i < names.length; i++) {
if (names[i].length() > 5 || names[i].contains(" ") || names[i].isEmpty()) {
throw new IllegalArgumentException();
}
cars.add(new Car(names[i]));
}
if (isDuplicateName(cars)) {
throw new IllegalArgumentException();
}
return cars;
}

private void inputRound() {
System.out.println("시도할 회수는 몇회인가요?");
try {
this.round = Integer.parseInt(Console.readLine().trim());
} catch(NumberFormatException e) {
throw new IllegalArgumentException();
}
if (this.round <= 0) {
throw new IllegalArgumentException();
}
System.out.println(" ");
}
}