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

[김세진] 프리코스 미션 제출합니다. #134

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions CarRaceController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class CarRaceController {
private CarRaceModel model;
private CarRaceView view;
private int raceNum; // 사용자 입력값 / 레이스 횟수

public CarRaceController(CarRaceModel model, CarRaceView view) {
this.model = model;
this.view = view;
}

public void startRace(int raceNum) {
this.raceNum = raceNum;
for (int nowRace = 0; nowRace < raceNum; nowRace++) {
model.race();
view.printRaceInfo(model.getCarList(), model.getCarRaceInfo());
}
view.printResult(model.getCarList(), model.getCarRaceInfo(), model.getWinnerLength());
}
}
45 changes: 45 additions & 0 deletions CarRaceModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CarRaceModel {
private List<String> carList; // 사용자 입력값 / 자동차 이름
private List<String> carRaceInfo; // 자동차마다의 레이스 상황값
private Random random;
private int winnerLen = 0; // 레이스에서의 최대 길이

public CarRaceModel(List<String> carList) {
this.carList = carList;
this.carRaceInfo = new ArrayList<>(carList.size());
for (int i = 0; i < carList.size(); i++) {
this.carRaceInfo.add("");
}
this.random = new Random();
}

public void race() {
for (int i = 0; i < carList.size(); i++) {
raceStart(i);
}
}

private void raceStart(int i) {
int randomNumber = random.nextInt(10);
if (randomNumber <= 4) {
carRaceInfo.set(i, carRaceInfo.get(i) + "-");
winnerLen = Math.max(winnerLen, carRaceInfo.get(i).length());
}
}

public List<String> getCarList() {
return carList;
}

public List<String> getCarRaceInfo() {
return carRaceInfo;
}

public int getWinnerLength() {
return winnerLen;
}
}
26 changes: 26 additions & 0 deletions CarRaceView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.List;

public class CarRaceView {
public void printRaceInfo(List<String> carList, List<String> carRaceInfo) {
for (int i = 0; i < carList.size(); i++) {
System.out.println(carList.get(i) + " : " + carRaceInfo.get(i));
}
System.out.println();
}

public void printResult(List<String> carList, List<String> carRaceInfo, int winnerLen) {
boolean firstWinner = true;
System.out.print("최종 우승자 : ");

for (int i = 0; i < carList.size(); i++) {
if (carRaceInfo.get(i).length() == winnerLen) {
if (firstWinner) {
firstWinner = false;
System.out.print(carList.get(i));
continue;
}
System.out.print(", " + carList.get(i));
}
}
}
}
86 changes: 86 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {

static Scanner scanner ;
static List<String> carList ;

public static void main(String[] args) {
scanner = new Scanner(System.in);

System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분");
getCarList();

System.out.println("시도할 회수는 몇회인가요?");
int raceNum = getRaceNum();

// int raceNum = Integer.parseInt(scanner.nextLine());
System.out.println();

CarRaceModel model = new CarRaceModel(carList);
CarRaceView view = new CarRaceView();
CarRaceController controller = new CarRaceController(model, view);
controller.startRace(raceNum);
}

public static List<String> getCarList(){
carList = new ArrayList<>();

while (true) {
String input = scanner.nextLine().trim();

try {
validateInput(input);
break;
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] " + e.getMessage());
}
}
return carList ;
}

public static List<String> checkCarName(String input) { // 입력받은 차 이름 반환
List<String> checkedNames = new ArrayList<>();
String[] names = input.split(",");

for (String name : names) {
if (name.length() <= 5) {
checkedNames.add(name);
}
}
return checkedNames;
}

private static void validateInput(String input) throws IllegalArgumentException {
if (input.isEmpty()) {
throw new IllegalArgumentException("입력이 비어있습니다.");
}

String[] names = input.split(",");
for (String name : names) {
if (name.trim().isEmpty()) {
throw new IllegalArgumentException("빈 자동차 이름이 있습니다.");
}
if (name.trim().length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하여야 합니다.");
}
carList.add(name);
}
}

public static int getRaceNum(){
int raceNum ;
while (true) {
try {
raceNum = scanner.nextInt();
break;
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] " + e.getMessage());
scanner.nextLine();
}
}
return raceNum;
}
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# java-racingcar-precourse
# java-racingcar-precourse

1. 안내문구 프린트하기
2. 사용자 문자 입력받기
3. 사용자 입력 쉼표로 분리하기
4. 사용자 숫자 입력받기
5. 입력된 숫자만큼 무작위로 자동차 주행하기
1. else, switch/case 사용 불가
6. 우승자 선정