-
Notifications
You must be signed in to change notification settings - Fork 54
[김성은_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
Open
pizzaa0930
wants to merge
24
commits into
BCSDLab-Edu:main
Choose a base branch
from
pizzaa0930: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
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a681877
기능 정리
pizzaa0930 ed754d4
도메인 파일 car클래스 정의
pizzaa0930 980d6fd
메시지 구현
pizzaa0930 f57bc2e
메시지 구현
pizzaa0930 9e22f97
경주하는 차의 정보 배열 정리
pizzaa0930 1ef6c52
게임 내 신호
pizzaa0930 698aa72
우승자 출력
pizzaa0930 3e81eff
게임의 전체 기능 조정
pizzaa0930 0380abd
차 이름 지정 시 예외사항
pizzaa0930 0f1759c
게임 실행
pizzaa0930 896b7ff
기능 테스트
pizzaa0930 3bceec9
기능 테스트
pizzaa0930 62b44e9
Docs: 기능을 조금 더 보기 편하게 수정했습니다
pizzaa0930 549f6cd
Refactor: 싱글톤 패턴 방식에서 퍼블릭으로 전환하여 일반 클래스를 사용했습니다
pizzaa0930 5d703b1
Refactor: 싱글톤 패턴 방식을 삭제하였고 그 과정에서 close 메서드가 비게되었습니다
pizzaa0930 1fb5f2a
Refactor: 싱글톤 패턴 방식을 삭제하였습니다
pizzaa0930 ee828d3
Refactor: 싱글톤 패턴 방식을 삭제하고 new로 새로 정의하였습니다
pizzaa0930 d5bf22f
Refactor: 싱글톤 패턴 방식을 삭제하였습니다
pizzaa0930 d9f3f71
Refactor: 싱글톤 패턴 방식을 일반 클래스 new로 정의하여 메인으로 동작을 수행하게 했습니다
pizzaa0930 25e0afe
Refactor: new정의가 하나 빠져있어서 수정했습니다
pizzaa0930 c4b21b3
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 6daf792
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 f560ec2
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 6042c5e
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 자동차 이름 입력기능 | ||
| 쉼표를 기준으로 구분 | ||
| 5자 이하만 가능 | ||
| 이름이 조건에 맞지 않을 시 에러 메시지 | ||
| 이동할 횟수 입력 기능 | ||
| 랜덤을 사용한 자동차 전진 기능 | ||
| 전진과 자동차 이름 동시 출력 기능 | ||
| 완료 후 우승자 표시 기능 | ||
| 우승자가 여러 명일 경우 쉼표 구분 | ||
| 잘못된 값을 입력할 시 종료하는 기능 |
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,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(); | ||
| //gameController = null; | ||
| } | ||
|
|
||
| } |
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,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) { | ||
| 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; | ||
| } | ||
| } | ||
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,38 @@ | ||
| 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; | ||
|
|
||
| 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() { | ||
| //defaultGameInputException = null; | ||
| } | ||
|
|
||
| } | ||
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,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; | ||
| }*/ | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| } |
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,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; } | ||
|
|
||
| } |
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,22 @@ | ||
| 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(); | ||
| } | ||
| } | ||
| } |
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,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; | ||
| //private static CarService defaultCarService; | ||
|
|
||
| public CarService() { | ||
| carList = new ArrayList<>(); | ||
| sb = new StringBuilder(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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; | ||
| } | ||
|
|
||
| } |
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,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() { | ||
| //defaultGameView = null; | ||
| } | ||
|
|
||
| } |
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,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(); | ||
|
|
||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
domain과 model의 차이점을 알아두시면 좋을 것 같습니다.