-
Notifications
You must be signed in to change notification settings - Fork 52
[김성은_BackEnd]1주차 과제 제출합니다 #61
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 12 commits
a681877
ed754d4
980d6fd
f57bc2e
9e22f97
1ef6c52
698aa72
3e81eff
0380abd
0f1759c
896b7ff
3bceec9
62b44e9
549f6cd
5d703b1
1fb5f2a
ee828d3
d5bf22f
d9f3f71
25e0afe
c4b21b3
6daf792
f560ec2
6042c5e
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,6 @@ | ||
| 자동차 이름 입력기능(쉼표를 기준으로 구분,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 static GameController gameController; | ||
|
|
||
| private final GameView gameView; | ||
| private final CarService carService; | ||
|
|
||
| private GameController() { | ||
| gameView = GameView.getInstance(); | ||
| carService = CarService.getInstance(); | ||
| } | ||
|
|
||
| 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(); | ||
| gameController = null; | ||
| } | ||
|
|
||
| } | ||
|
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. https://velog.io/@junho5336/No-newline-at-end-of-file 개행 경고에 대한 해결법입니다! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package domain; | ||
|
|
||
| public class Car implements Comparable<Car> { | ||
|
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. domain과 model의 차이점을 알아두시면 좋을 것 같습니다. |
||
| private static final int STOP = 3; | ||
| private final String name; | ||
| private int distance; | ||
|
|
||
| public Car(final String name, int distance) { | ||
| 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; } | ||
|
|
||
| @Override | ||
| public int compareTo(Car o) { | ||
| return o.distance - this.distance; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package exception; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| public class GameInputException { | ||
|
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. 커스텀 예외 좋네요. 어떻게 알게되었는지도 궁금합니다 👍 |
||
|
|
||
| private static GameInputException defaultGameInputException; | ||
|
|
||
| private GameInputException() { | ||
| } | ||
|
|
||
| public static GameInputException getInstance() { | ||
| if(defaultGameInputException == null) { | ||
| defaultGameInputException = new GameInputException(); | ||
| } | ||
| return defaultGameInputException; | ||
| } | ||
|
|
||
| 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() { | ||
| defaultGameInputException = null; | ||
| } | ||
|
|
||
| } | ||
| 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; | ||
|
|
||
| private RacingGame() { | ||
| inputView = InputView.getInstance(); | ||
| gameInputException = GameInputException.getInstance(); | ||
| gameController = GameController.getInstance(); | ||
| } | ||
|
|
||
| public static RacingGame getInstance() { | ||
| if(defaultRacingGame == null) { | ||
| defaultRacingGame = new RacingGame(); | ||
| } | ||
| return defaultRacingGame; | ||
| } | ||
|
|
||
| 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 { | ||
|
|
||
| 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,17 @@ | ||
| 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: 프로그램 구현 | ||
| RacingGame game = RacingGame.getInstance(); | ||
| game.run(); | ||
| game.close(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| 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; | ||
| private static CarService defaultCarService; | ||
|
|
||
| private CarService() { | ||
| carList = new ArrayList<>(); | ||
| sb = new StringBuilder(); | ||
| } | ||
|
|
||
| public static CarService getInstance() { | ||
| if(defaultCarService == null) { | ||
| defaultCarService = new CarService(); | ||
| } | ||
| return defaultCarService; | ||
| } | ||
|
|
||
| public void close() { | ||
| defaultCarService = null; | ||
| } | ||
|
|
||
| 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,35 @@ | ||
| package view; | ||
|
|
||
| import message.GameMessage; | ||
|
|
||
| public class GameView { | ||
|
|
||
| private static GameView defaultGameView; | ||
|
|
||
| private GameView() { | ||
| } | ||
|
|
||
| public static GameView getInstance() { | ||
| if(defaultGameView == null) { | ||
| defaultGameView = new GameView(); | ||
| } | ||
| return defaultGameView; | ||
| } | ||
|
|
||
| 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() { | ||
| defaultGameView = null; | ||
| } | ||
|
|
||
| } |
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.
모든 클래스를 싱글톤으로 하신 이유가 있을까요?
매번 close로 초기화한다면 new로 주입하는게 더 나았을 수도 있지 않았을지 궁금합니다.