Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dongwonkwon #2375

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1. 자동차 클래스

- 자동차 이름 입력 + 예외 처리


2. 실행 클래스

- 무작위로 전진
- 최종 우승자 출력
- 여러 우승자일 경우, 쉼표로 구분해 출력
10 changes: 9 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Car car = new Car();
Racing racing = new Racing();
racing.printSetCarName();
car.setCarName();

racing.printSetCount();
racing.setCount();

racing.printTotalRacing(car.getCarInfo(), racing.getCount());
}
}
29 changes: 29 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class Car {

private Map<String, Integer> carInfo = new LinkedHashMap<String, Integer>();

public Map<String, Integer> getCarInfo() {
return carInfo;
}

public void setCarName() {
String str = Console.readLine();
String[] names = str.split(",");

for (String name : names) {
if (name.length() > 5) {
throw new IllegalArgumentException("Invalid argument: " + name);
}
carInfo.put(name,0);
}
}

}
59 changes: 59 additions & 0 deletions src/main/java/racingcar/Racing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package racingcar;

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

import java.util.Collections;
import java.util.Map;

public class Racing {

private int count;
public void printSetCarName() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
}

public void printSetCount() {
System.out.println("시도할 회수는 몇회인가요?");
}

public int getCount() {
return count;
}

public void setCount() {
String str = Console.readLine();
count = Integer.parseInt(str);
}

public Map<String, Integer> printRacing(Map<String, Integer> carInfo) {
for (Map.Entry<String, Integer> entry : carInfo.entrySet()) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (randomNumber >= 4) {
entry.setValue(entry.getValue() + 1);
}
System.out.println(entry.getKey() + " : " + "-".repeat(entry.getValue()));
}
return carInfo;
}

public void printTotalRacing(Map<String, Integer> carInfo, int count) {
System.out.println("실행 결과");
for (int i = 0; i<count; i++) {
printRacing(carInfo);
}
int maxValueInMap=(Collections.max(carInfo.values()));
System.out.print("최종 우승자 : ");
int cnt = 0;
for (Map.Entry<String, Integer> entry : carInfo.entrySet()) {
cnt++;
if (cnt == 1) {
System.out.println(entry.getKey());
}
else if (entry.getValue() == maxValueInMap) {
System.out.print("," + entry.getKey());
}
}
}

}