-
Notifications
You must be signed in to change notification settings - Fork 54
[BackEnd_김성은]2주차 과제 제출합니다 #68
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
a681877
ed754d4
980d6fd
f57bc2e
9e22f97
1ef6c52
698aa72
3e81eff
0380abd
0f1759c
896b7ff
3bceec9
62b44e9
549f6cd
5d703b1
1fb5f2a
ee828d3
d5bf22f
d9f3f71
25e0afe
c4b21b3
6daf792
f560ec2
26248a9
3aa2d9c
154044c
acbd24c
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,10 @@ | ||
| 자동차 이름 입력기능 | ||
| 쉼표를 기준으로 구분 | ||
| 5자 이하만 가능 | ||
| 이름이 조건에 맞지 않을 시 에러 메시지 | ||
| 이동할 횟수 입력 기능 | ||
| 랜덤을 사용한 자동차 전진 기능 | ||
| 전진과 자동차 이름 동시 출력 기능 | ||
| 완료 후 우승자 표시 기능 | ||
| 우승자가 여러 명일 경우 쉼표 구분 | ||
| 잘못된 값을 입력할 시 종료하는 기능 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package controller; | ||
|
|
||
| import service.CarService; | ||
|
|
||
| import view.GameView; | ||
|
|
||
| public class GameController { | ||
|
|
||
|
|
||
|
|
||
| private final GameView gameView; | ||
| private final CarService carService; | ||
|
|
||
| public GameController() { | ||
| gameView = new GameView(); | ||
| carService = new CarService(); | ||
| } | ||
|
|
||
| /*public static GameController getInstance() { | ||
| if(gameController == null) { | ||
| gameController = new GameController(); | ||
| } | ||
| return gameController; | ||
| }*/ | ||
|
|
||
| public void raceSet(String[] names) { | ||
| gameView.printResult(); | ||
| carService.fill(names); | ||
| } | ||
|
|
||
| public void race(int count) { | ||
| String result = carService.getResult(count); | ||
| gameView.printResult(result); | ||
| } | ||
|
|
||
| public void raceResult() { | ||
| String winners = carService.getWinners(); | ||
| gameView.printWinners(winners); | ||
| } | ||
|
|
||
| public void close() { | ||
| carService.close(); | ||
| gameView.close(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package domain; | ||
|
|
||
| public class Car implements Comparable<Car> { | ||
| private static final int STOP = 3; | ||
| private final String name; | ||
| private int distance; | ||
|
|
||
| public Car(final String name, int distance) { | ||
|
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. 매개변수에 final을 붙여주셨네요. 이유도 궁금합니다. |
||
| this.name = name; | ||
| this.distance = distance; | ||
| } | ||
|
|
||
| public Car(final String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
|
|
||
| public void move(final int value) { | ||
| if(value > STOP) { | ||
| distance++; | ||
| } | ||
| } | ||
|
|
||
| public int getDistance() { return distance; } | ||
| public String getName() { return name; } | ||
|
Comment on lines
+24
to
+25
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. 메서드들도 줄넘김 규칙 통일해주시고요 |
||
|
|
||
| @Override | ||
| public int compareTo(Car o) { | ||
| return o.distance - this.distance; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package exception; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| public class GameInputException { | ||
|
|
||
|
|
||
|
|
||
| public GameInputException() { | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public void validateNameLength(String[] names) { | ||
| for(String name : names) { | ||
| if(name.length() > 5) { | ||
| throw new IllegalArgumentException(GameMessage.nameError.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void validateNumber(String number) { | ||
| if(!number.matches(GameMessage.REGEX.getMessage())) { | ||
| throw new IllegalArgumentException(GameMessage.countError.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void validateNumberZero(String number) { | ||
| if(number.length() > 1 && number.charAt(0) == '0') { | ||
| throw new IllegalArgumentException(GameMessage.countError.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void close() { | ||
|
|
||
| } | ||
|
Comment on lines
+34
to
+36
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package game; | ||
|
|
||
| import controller.GameController; | ||
|
|
||
| import exception.GameInputException; | ||
|
|
||
| import view.InputView; | ||
|
|
||
| public class RacingGame { | ||
|
|
||
| // private static RacingGame defaultRacingGame; | ||
| private final InputView inputView; | ||
| private final GameInputException gameInputException; | ||
| private final GameController gameController; | ||
|
|
||
| public RacingGame() { | ||
| inputView = new InputView(); | ||
| gameInputException = new GameInputException(); | ||
| gameController = new GameController(); | ||
| } | ||
|
|
||
| /*public static RacingGame getInstance() { | ||
| if(defaultRacingGame == null) { | ||
| defaultRacingGame = new RacingGame(); | ||
| } | ||
| return defaultRacingGame; | ||
| }*/ | ||
|
Comment on lines
+21
to
+27
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. 주석들은 차라리 지워주셔도 좋을 것 같습니다. |
||
|
|
||
| public void run() { | ||
| String[] names = preHandleNames(); | ||
| int count = preHandleCount(); | ||
| gameController.raceSet(names); | ||
| gameController.race(count); | ||
| gameController.raceResult(); | ||
| } | ||
|
|
||
| private String[] preHandleNames() { | ||
| String input = inputView.inputCarNames(); | ||
| String[] names = input.split(","); | ||
| gameInputException.validateNameLength(names); | ||
| return names; | ||
| } | ||
|
|
||
| private int preHandleCount() { | ||
| String input = inputView.inputRaceCount(); | ||
| gameInputException.validateNumber(input); | ||
| gameInputException.validateNumberZero(input); | ||
| return Integer.parseInt(input); | ||
| } | ||
|
|
||
| public void close() { | ||
| gameController.close(); | ||
| gameInputException.close(); | ||
| inputView.close(); | ||
| //defaultRacingGame = null; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package message; | ||
|
|
||
| public enum GameMessage { | ||
|
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. 이렇게 모으면 무슨 장점이 있을까요? |
||
|
|
||
| start("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), | ||
| count("시도할 회수는 몇회인가요?"), | ||
| result("실행 결과"), | ||
| winner("최종 우승자"), | ||
| equal(" : "), | ||
| bar("-"), | ||
| nameError("자동차의 이름은 5자리 이내입니다."), | ||
| countError("회수는 음수가 아닌 정수로 입력해주세요."), | ||
| REGEX("[0-9]+"), | ||
| newLine("\n"); | ||
|
|
||
| private final String message; | ||
|
|
||
| GameMessage(final String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { return message; } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,23 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import game.RacingGame; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| // TODO: 프로그램 구현; | ||
| RacingGame game =new RacingGame(); | ||
|
|
||
| try { | ||
| game.run(); | ||
| } | ||
| finally { | ||
| game.close(); | ||
| } | ||
|
Comment on lines
+15
to
+20
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. 전역적으로 try-catch하면 어떤 장점과 단점이 있을지 생각해보시면 좋을 것 같습니다. |
||
| } | ||
| } | ||
| //수정 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package service; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import domain.Car; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
|
|
||
| public class CarService { | ||
|
|
||
| private final ArrayList<Car> carList; | ||
| private final StringBuilder sb; | ||
|
|
||
|
|
||
| public CarService() { | ||
| carList = new ArrayList<>(); | ||
| sb = new StringBuilder(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public void close() { | ||
|
|
||
| } | ||
|
|
||
| public void fill(String[] carNameArr,int[] distance) { | ||
| int i = 0; | ||
| for(String name : carNameArr) { | ||
| carList.add(new Car(name,distance[i++])); | ||
| } | ||
| } | ||
|
|
||
| public void fill(String[] carNameArr) { | ||
| for(String name : carNameArr) { | ||
| carList.add(new Car(name)); | ||
| } | ||
| } | ||
|
|
||
| public String getResult(int count) { | ||
| while(count-- > 0) { | ||
| race(); | ||
| if(count != 0) { | ||
| sb.append(GameMessage.newLine.getMessage()); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private void race() { | ||
| for(Car car : carList) { | ||
| int randomNumber = Randoms.pickNumberInRange(0,9); | ||
| car.move(randomNumber); | ||
| raceRecord(car); | ||
| } | ||
| } | ||
|
|
||
| private void raceRecord(Car car) { | ||
| sb.append(car.getName()).append(GameMessage.equal.getMessage()); | ||
| buildBar(car.getDistance()); | ||
| } | ||
|
|
||
| private void buildBar(int distance) { | ||
| while(distance-- > 0) { | ||
| sb.append(GameMessage.bar.getMessage()); | ||
| } | ||
| sb.append(GameMessage.newLine.getMessage()); | ||
| } | ||
|
|
||
| public String getWinners() { | ||
| Collections.sort(carList); | ||
| int winDistance = carList.get(0).getDistance(); | ||
| ArrayList<String> winnerList = makeWinnerList(winDistance); | ||
| return String.join(", ",winnerList); | ||
| } | ||
|
|
||
| private ArrayList<String> makeWinnerList(int winDistance) { | ||
| ArrayList<String> winnerList = new ArrayList<>(); | ||
| for(Car car : carList) { | ||
| int distance = car.getDistance(); | ||
| if(winDistance > distance) { break; } | ||
| winnerList.add(car.getName()); | ||
| } | ||
| return winnerList; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package view; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| public class GameView { | ||
|
|
||
| //private static GameView defaultGameView; | ||
|
|
||
| public GameView() { | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public void printResult() { | ||
| System.out.println(GameMessage.newLine.getMessage() + GameMessage.result.getMessage()); | ||
| } | ||
|
|
||
| public void printResult(String result) { | ||
| System.out.println(result); | ||
| } | ||
|
|
||
| public void printWinners(String winners) { | ||
| System.out.println(GameMessage.winner.getMessage() + GameMessage.equal.getMessage() + winners); | ||
| } | ||
|
|
||
| public void close() { | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package view; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| public class InputView { | ||
|
|
||
|
|
||
|
|
||
| public InputView() { | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public String inputCarNames() { | ||
| System.out.println(GameMessage.start.getMessage()); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| public String inputRaceCount() { | ||
| System.out.println(GameMessage.count.getMessage()); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| public void close() { | ||
| Console.close(); | ||
|
|
||
| } | ||
|
|
||
| } |
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.
생각보다 이런 줄넘김이나 네이밍 규칙같은 코드 컨벤션 잘 지켜주시면 좋습니다.