-
Notifications
You must be signed in to change notification settings - Fork 4
[자동차 경주] 박성민 미션 제출합니다. #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
ceda29f
fcfb20c
a30e161
c5aaadb
1b87de1
3cb6d58
504b231
ea04bc6
f27fa3a
910c300
2262a0a
bf2cbe2
53c50ef
28ae9b7
bc89e7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # 입력 | ||
|
|
||
| ### 자동차 | ||
| - 숫자 n개의 자동차의 이름을 입력받는다. | ||
| - 자동차의 이름은 영어이며 5자 이하이다. | ||
| - 자동차의 이름은 쉼표로 구별한다. | ||
| ### 이동 횟수 | ||
| - 숫자 m의 자동차 이동 횟수를 입력받는다. | ||
| - 매 시도마다 0에서 9사이의 무작위 값을 구하고, 만약 4 이상의 값이라면 전진한다. | ||
| - 모든 경주가 끝이나면 우승자를 출력한다. | ||
| - 우승자는 한 명 이상일 수 있다. 만약 우승자가 여러명인 경우 쉼표로 구분한다. | ||
| ### 예외처리 | ||
| - 사용자가 잘못된 값을 입력하면 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료된다. |
| 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(); | ||
| } | ||
| } |
| 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()); | ||
| } | ||
| model.findWinner(); | ||
| view.printWinner(model.winner); | ||
| } catch (Exception e) { | ||
| System.out.println("Exception [Err_Msg] : " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| 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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isWin을 넣었는데 사용하지 않게 된 이유가 뭔가용
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) { // 최댓값 찾기 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Math.max() 메서드를 사용하는것도 좋아보여요
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InputView, OutView로 분리 했으면 관리가 더 편했을거 같아요
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 방법 같습니다 참고할게요 🤝🏻 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약 위에서
Comment on lines
+38
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트코드 대응 굿 |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model.car.size()를 매개변수로 전달해준 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
출력 기준에 맞춰 입력받은 자동차의 수 만큼 출력 해야하기 위해서 입니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 궁금해요