-
Notifications
You must be signed in to change notification settings - Fork 33
[스프링 화요일 4팀] 장영재 미션 제출합니다. #18
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
Open
urinaner
wants to merge
1
commit into
TEAM-ALOM:main
Choose a base branch
from
urinaner:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| public class RacingMain { | ||
| import racingcar.controller.RacingController; | ||
|
|
||
| public class RacingMain { | ||
| private static final RacingController racingController = new RacingController(); | ||
| public static void main(String[] args) { | ||
| // TODO: MVC 패턴을 기반으로 자동차 경주 미션 구현해보기 | ||
| System.out.println("Hello, World!"); | ||
| racingController.start(); | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.model.Car; | ||
| import racingcar.model.RacingGame; | ||
| import racingcar.model.WinningResult; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class RacingController { | ||
| private final RacingGame racinggame = new RacingGame(); | ||
| private final WinningResult winningresult = new WinningResult(); | ||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputview = new OutputView(); | ||
|
|
||
| public void start() { | ||
| String[] names = inputView.getNames(); | ||
| int round = inputView.getRound(); | ||
| List<Car> cars = racinggame.saveCars(names); | ||
| roundResult(round, cars); | ||
| winningresult.winningScore(cars); | ||
| outputview.result(cars, winningresult.winningScore); | ||
| } | ||
|
|
||
| private void roundResult(int round, List<Car> cars) { | ||
| System.out.println(""); | ||
| for (int i = 0; i < round; i++) { | ||
| racinggame.updatePhase(cars); | ||
| outputview.round(cars); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package racingcar.model; | ||
| public class Car { //자동차 객체 생성 | ||
| private String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name, int position) { | ||
| this.name = name; | ||
| this.position = position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public void setPosition(int position) { | ||
| this.position = position; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package racingcar.model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class RacingGame { | ||
|
|
||
| public List<Car> saveCars(String[] carNames) { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (int i = 0; i < carNames.length; i++) { | ||
| String name = carNames[i]; | ||
| cars.add(new Car(name, 0)); | ||
| } | ||
| return cars; | ||
| } | ||
| public void updatePhase(List<Car> cars) { | ||
| Random random = new Random(); | ||
| for (int j = 0; j < cars.size(); j++) { | ||
| int randomNum = random.nextInt(10); | ||
| if (randomNum >= 4) { | ||
| cars.get(j).setPosition(cars.get(j).getPosition() + 1); | ||
| } | ||
| } | ||
| } | ||
| } |
|
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. 클래스 이름은 우승 결과 관련 클래스 같지만 내용은 우승자 점수를 구하는 내용인 것 같아서 클래스 이름을 바꾸거나 기능을 옮겨서 구현하는 편이 좋아 보여요 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package racingcar.model; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class WinningResult { | ||
| public int winningScore = 0; | ||
|
|
||
| public void winningScore(List<Car> cars) { | ||
| for (int m = 0; m < cars.size(); m++) { | ||
| if (winningScore < cars.get(m).getPosition()) { | ||
| winningScore = cars.get(m).getPosition(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
|
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. 필요한 예외처리 꼼꼼하게 잘 하신 것 같아요👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package racingcar.view; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { //입력관련 메서드 집합 | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| public String[] getNames() { | ||
| System.out.print("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String inputname = sc.next(); | ||
| String[] names = inputname.split(","); //쉼표로 분리해서 넣기 | ||
| try { | ||
| nameRange(names); //글자가 5개 이상이거나, | ||
| sameName(names); //같은 이름이 있을 경우 | ||
| } catch (IllegalArgumentException e) { | ||
| getNames(); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| private void nameRange(String[] names) { | ||
| for (String name : names) { | ||
| if (name.length() > 5) { | ||
| System.out.print("글자가 5자리 아래여야만함"); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void sameName(String[] names) { | ||
| List<String> check = new ArrayList<>(); | ||
| for (String name : names) { | ||
| if (check.contains(name)) { | ||
| System.out.print("중복이름 X"); | ||
| throw new IllegalArgumentException(); | ||
| } else { | ||
| check.add(name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public int getRound() { | ||
| System.out.print("시도할 회수는 몇회인가요?"); | ||
| String inputround = sc.next(); | ||
| int round = 0; | ||
| try { | ||
| round = Integer.parseInt(inputround); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.print("숫자나 입력하세여"); | ||
| getRound(); | ||
| } | ||
| return round; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package racingcar.view; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| public void round(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.print(car.getName()); | ||
| System.out.print(" : "); | ||
| System.out.println("-".repeat(car.getPosition())); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void result(List<Car> cars, int firstPrize) { | ||
| System.out.print(""); | ||
| List<String> winner = new ArrayList<>(); | ||
| for (int n = 0; n < cars.size(); n++) { | ||
| if (cars.get(n).getPosition() == firstPrize) { | ||
| winner.add(cars.get(n).getName()); | ||
| } | ||
| } | ||
| System.out.println(String.join(", ", winner)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package racingcar.model; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CarTest { | ||
|
|
||
| @Test | ||
| void move() { | ||
| Car car = new Car("June", 0); | ||
| System.out.println(car.getPosition()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
랜덤 변수 생성 메소드와 updatePhase 메소드를 나눠서 개발하면 메소드 길이도 짧아지고, 기능별로 나눌 수 있을 것 같아요