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
12 changes: 10 additions & 2 deletions 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: 프로그램 구현
View view = new View();
Controller controller = new Controller(view);

try {
controller.startRace();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
throw e;
}
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/racingcar/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package racingcar;

import java.util.ArrayList;
import java.util.List;

public class Controller {
private final View view;
private final List<Member> members = new ArrayList<>();

Choose a reason for hiding this comment

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

현재는 무조건 하나의 게임만 존재하기 떄문에 이렇게 구현해도 괜찮지만, 한번 이 Controller가 상태를 갖지 않도록 해볼까요?


public Controller(View view) {
this.view = view;
}

//레이스 시작
public void startRace() {
// 경주차 등록
String[] names = view.getMemberNames();
for (String name : names) {
members.add(new Member(name.trim(),0));
}

// 시도 횟수
int tryCount = view.getTryCount();

// 경주 진행
for (int i = 0; i < tryCount; i++) {
for (Member m : members) {
m.startRandom();
}
view.printProgress(members);
}

// 우승자 계산
int max = 0;
for (Member m : members) {
if (m.getGo() > max) {
max = m.getGo();
}
}
List<Member> winners = new ArrayList<>();
for (Member m : members)
if (m.getGo() == max) {
winners.add(m);
}
view.printWinners(winners);
}
}
37 changes: 37 additions & 0 deletions src/main/java/racingcar/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.Random;

public class Member {
private String name;
private int go;
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

변수가 무엇을 의미하는 지 변수만을 보고 알기가 어려워요..


public Member(String name, int go) {
validateName(name);
this.name = name;
this.go = 0;
}

public String getName() {
return name;
}

public int getGo() {
return go;
}
//랜덤 뽑기
public void startRandom() {
int randomValue = Randoms.pickNumberInRange(0, 9);
if (randomValue >= 4) { // 4 이상이면 전진
go++;
}
}
//예외처리
private void validateName(String name) {
if (name == null || name.isBlank() || name.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 1~5자 사이여야 합니다.");
}
}
}
53 changes: 53 additions & 0 deletions src/main/java/racingcar/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package racingcar;

import java.util.List;
import java.util.Scanner;

public class View {
private final Scanner scanner;

public View() {
this.scanner = new Scanner(System.in);
}
//경주차 이름 입력
public String[] getMemberNames() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
List<String> lst = List.of(scanner.nextLine().split(","));

Choose a reason for hiding this comment

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

split을 통해 배열로도 받을 수 있는 데 List로 받으신 이유가 특별히 있나요?


for (String s : lst) {
String trimmed = s.trim();

if (trimmed.isEmpty()) {
throw new IllegalArgumentException("자동차 이름이 공백");
}

if (trimmed.length() > 5) {
throw new IllegalArgumentException("이름 오류");
}
Comment on lines +20 to +26

Choose a reason for hiding this comment

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

View에서 검사까지 하도록 일부로 노리신 건가요? 책임 분리 해볼까요?
또한 현재 예외 메시지만을 보고는 정확한 도메인 이해가 잘 되지 않아요.

}

return lst.stream()
.map(String::trim)
.toArray(String[]::new);
}
//시도 회수 결정
public int getTryCount() {
System.out.println("시도할 회수는 몇회인가요?");
int tryNum = scanner.nextInt();
if (tryNum <= 0) {
throw new IllegalArgumentException("시도 횟수는 이상");
}
return tryNum;
}
//경주차 간거 표시
public void printProgress(List<Member> members) {
for (Member m : members) {
System.out.println(m.getName() + " : " + "-".repeat(m.getGo()));

Choose a reason for hiding this comment

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

member에서 get을 사용하지 않고 값을 보여주도록 해볼까요?
get을 쓰면 안좋은 이유가 뭐가 있을까요?

}
System.out.println();
}
//우승자 발표
public void printWinners(List<Member> winners) {
System.out.println("최종 우승자 : " + String.join(", ", winners.stream().map(Member::getName).toList()));
}
}