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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand Down
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
자동차를 객체로 생성해서 사용한다
자동차 객체는 이름과 점수를 가진다

이름은 한줄로 받은 후에 ","을 기준으로 분리해서 ArrayList에 입력한다.
입력 받은 후 for문을 활용하여 객체를 생성한다
89 changes: 89 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

import java.util.ArrayList;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
ArrayList<RacingCar> cars = new ArrayList<>();
ArrayList<RacingCar> winners = new ArrayList<>();

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

for (String name : names) {
try{
RacingCar RC = new RacingCar(name);
cars.add(RC);
} catch (IllegalArgumentException e){
System.out.println("오류 발생 : " + e.getMessage());
System.out.println("\"" +name +"\"" + "은 사용하실 수 없습니다.");
System.exit(0);
}
}

System.out.println("시도할 회수는 몇회인가요?");
int attempt = Integer.parseInt(Console.readLine());
System.out.println("실행 결과\n");

for (int i = 0; i < attempt; i++) {
for (RacingCar car : cars) {
car.updateScoreOnMove();
System.out.print(car.name + " : ");
car.printScore();
}
System.out.println();
}
int maxScore = 0;
for (RacingCar car : cars) {
if (car.getScore() > maxScore) {
maxScore = car.getScore();
}
}

for (RacingCar car : cars) {
if (car.getScore() == maxScore) {
winners.add(car);
}
}

System.out.print("최종 우승자 :");
for (RacingCar car : winners) {
System.out.print(car.getName() + " ");
}
}

private static class RacingCar {
private final String name;
private int score = 0;

public RacingCar(String name) {
checkValidLength(name);
this.name = name;
}

public void checkValidLength(String name) {
if (name.length() > 5) { // 이름이 5자 초과일 때
throw new IllegalArgumentException("이름은 5자 이하만 가능합니다.");
}
}

public void updateScoreOnMove() {
if(Randoms.pickNumberInRange(0,9)>=4) {
score++;
}
}

public void printScore() {
for (int i = 0; i < score; i++){
System.out.print("-");
}
System.out.println();
}

public String getName() {
return name;
}

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