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
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 입력

### 자동차
- 숫자 n개의 자동차의 이름을 입력받는다.
- 자동차의 이름은 영어이며 5자 이하이다.
- 자동차의 이름은 쉼표로 구별한다.
### 이동 횟수
- 숫자 m의 자동차 이동 횟수를 입력받는다.
- 매 시도마다 0에서 9사이의 무작위 값을 구하고, 만약 4 이상의 값이라면 전진한다.
- 모든 경주가 끝이나면 우승자를 출력한다.
- 우승자는 한 명 이상일 수 있다. 만약 우승자가 여러명인 경우 쉼표로 구분한다.
### 예외처리
- 사용자가 잘못된 값을 입력하면 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료된다.
7 changes: 5 additions & 2 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package racingcar;

import java.io.IOException;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
public static void main(String[] args) throws IOException {
Controller controller = new Controller();
controller.startGame();
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar;

import racingcar.Model.Model;

public class Controller {
Model model = new Model();
View view = new View();

public void startGame() {
try {
model.splitNames(view.inputCarNames());
model.frequency = view.inputFrequency();
for (int i = 0; i <= model.frequency; i++) {
model.moveCars();
view.printCarProgress(model.cars, model.cars.size());

Choose a reason for hiding this comment

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

model.car.size()를 매개변수로 전달해준 이유가 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

출력 기준에 맞춰 입력받은 자동차의 수 만큼 출력 해야하기 위해서 입니다!

Choose a reason for hiding this comment

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

저도 궁금해요

}
model.findWinner();
view.printWinner(model.winner);
} catch (Exception e) {
System.out.println("Exception [Err_Msg] : " + e.getMessage());
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/racingcar/Model/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar.Model;

import camp.nextstep.edu.missionutils.Randoms;

public class Car {
String name;
int progress;
boolean win;

public Car(String name, int progress, boolean win) {
this.name = name;
this.progress = progress;
this.win = win;
}

public String getName() {
return name;
}

public int getProgress() {
return progress;
}

public boolean isWin() {

Choose a reason for hiding this comment

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

isWin을 넣었는데 사용하지 않게 된 이유가 뭔가용

Copy link
Member Author

Choose a reason for hiding this comment

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

삭제하는 것을 까먹었네요 🤦🏻

return win;
}

public void move() {
int random = Randoms.pickNumberInRange(0, 9);
if (random >= 4) {
progress += 1;
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/racingcar/Model/Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package racingcar.Model;

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

public class Model {
public List<Car> cars = new ArrayList<>();
public List<String> winner = new ArrayList<>();
public int frequency = 0;

public void moveCars() {
for (Car car : cars) {
car.move();
}
}

public void splitNames(String carNames) {
StringTokenizer st = new StringTokenizer(carNames, ",");
while (st.hasMoreTokens()) {
String str = st.nextToken();
Car car = new Car(str, 0, false);

Choose a reason for hiding this comment

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

0과 false는 고정이니까 입력하지 않게 만들어놔도 되지 않을까용?

cars.add(car);
if (str.length() > 5) {
throw new IllegalArgumentException("Car name should be less than 5 characters");
}
}
}

public void findWinner() {
int max = 0;

for (int i = 0; i < cars.size(); i++) { // 최댓값 찾기

Choose a reason for hiding this comment

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

Math.max() 메서드를 사용하는것도 좋아보여요

Copy link
Member Author

Choose a reason for hiding this comment

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

좋은 메서드가 내장되어 있었네요 !

if (cars.get(i).getProgress() >= max) {
max = cars.get(i).getProgress();
}
}

for (int i = 0; i < cars.size(); i++) { // 우승자 찾기
if (cars.get(i).getProgress() >= max) {
winner.add(cars.get(i).getName());
}
}
}
}
55 changes: 55 additions & 0 deletions src/main/java/racingcar/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;
import racingcar.Model.Car;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class View {
public String inputCarNames() {
String carNames = "";
try {
System.out.println("자동차의 이름들을 입력하세요");
carNames = Console.readLine();
} catch (Exception e) {
System.out.println("Exception [Err_location] : " + e.getStackTrace()[0]);
throw new RuntimeException(e);
}
return carNames;
}

public int inputFrequency() {
try {
System.out.println("총 시도할 횟수를 입력하세요");
return Integer.parseInt(Console.readLine());
} catch (Exception e) {
System.out.println("Exception [Err_location] : " + e.getStackTrace()[0]);
throw new RuntimeException(e);
}
}

public void printCarProgress(List<Car> cars, int length) {
for (int index = 0; index < length; index++) {
System.out.print(cars.get(index).getName() + " : ");
int progress = cars.get(index).getProgress();
for (int i = 0; i < progress; i++) {
System.out.print("-");
}
System.out.println();
}
System.out.println();
}

public void printWinner(List<String> winner) {
System.out.print("최종 우승자 : ");
for (int i = 0; i < winner.size(); i++) {
System.out.print(winner.get(i));
if (i + 1 < winner.size()) {
System.out.print(",");
}
}
}
Comment on lines +34 to +54

Choose a reason for hiding this comment

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

InputView, OutView로 분리 했으면 관리가 더 편했을거 같아요

Copy link
Member Author

Choose a reason for hiding this comment

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

좋은 방법 같습니다 참고할게요 🤝🏻

}
8 changes: 7 additions & 1 deletion src/test/java/racingcar/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import camp.nextstep.edu.missionutils.test.NsTest;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest;
import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -33,6 +35,10 @@ class ApplicationTest extends NsTest {

@Override
public void runMain() {
Application.main(new String[]{});
try {
Application.main(new String[]{});
} catch (IOException e) {
throw new RuntimeException(e);
}
Comment on lines +38 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

만약 위에서 new IllegalArgumentException()을 던질 때 에러 메시지를 추가한다면,
여기서 에러를 캐치할 때 콘솔창에 에러 메시지를 print문을 통해 출력하면 좋을 것 같습니다!

Comment on lines +38 to +42

Choose a reason for hiding this comment

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

테스트코드 대응 굿

}
}