-
Notifications
You must be signed in to change notification settings - Fork 33
[스프링 목요일 5팀] 구나현 과제 제출합니다. #27
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
nahyunKoo
wants to merge
21
commits into
TEAM-ALOM:main
Choose a base branch
from
nahyunKoo: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 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
363791f
init
nahyunKoo 0a2264b
docs(docs/README.md) : 구현할 기능 목록 문서
nahyunKoo 44ea420
feat(getCarNames) : 경주 자동차 이름 입력 받기
nahyunKoo 7fcedd6
feat(getTryCount) : 시도 횟수 입력받기
nahyunKoo 94a84d2
feat(RacingGame) : 생성자
nahyunKoo 1899ff3
feat(tryCountValidate, carNamesValidate) : 입력 받은 차 이름과 경기 횟수 예외처리
nahyunKoo 0cccb3d
refactor(tryCountValidate, carNamesValidate) : InputView 클래스로 이동
nahyunKoo f0b640b
feat(printRaceStep, Start) : 단계출력하는 메소드 구현
nahyunKoo ddd9abe
feat(race) : 경기 진행하는 메소드 구현
nahyunKoo d6c6420
refactor(race)
nahyunKoo d969f5f
feat(printWinners) : 우승자 출력 메소드 구현
nahyunKoo 5ff3028
refactor(generateRandomDistance) : 반복문 구조 변경
nahyunKoo 3a0fc28
refactor(printRaceStep) : 구조 변경
nahyunKoo 80042f8
feat(getWinners) : 우승자 구하는 메소드 구현
nahyunKoo 5a9fab3
refactor(printRaceStep, start) : 접근제한자 변경
nahyunKoo a575cd3
refactor(race) : 단계 출력 하는 부분 수정
nahyunKoo 254f5b1
refactor : 출력 개행 수정
nahyunKoo 6a840bb
docs : 주석 추가
nahyunKoo cd2fb7e
test : 테스트 클래스 추가
nahyunKoo fe0f5e7
refactor(carNamesValidate, tryCountValidate) : 유효성 검사 도메인으로 이동
nahyunKoo 64d31fc
refactor(generateRandomDistance) : 패키지 이동
nahyunKoo 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 |
|---|---|---|
|
|
@@ -2,3 +2,7 @@ | |
|
|
||
| 자동차 경주 미션 저장소 | ||
|
|
||
| //경주할 자동차 이름 입력받기 | ||
| //시도할 횟수 입력 받기 | ||
| //경주 진행하기 | ||
| //우승자 출력하기 | ||
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,9 @@ | ||
| public class RacingMain { | ||
| import controller.RacingController; | ||
| import domain.RacingGame; | ||
|
|
||
| public class RacingMain { | ||
| public static void main(String[] args) { | ||
| // TODO: MVC 패턴을 기반으로 자동차 경주 미션 구현해보기 | ||
| System.out.println("Hello, World!"); | ||
| final RacingController racingController = new RacingController(); | ||
| racingController.run(); | ||
| } | ||
| } |
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,62 @@ | ||
| package controller; | ||
|
|
||
| import domain.RacingGame; | ||
| import view.InputView; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.*; | ||
|
|
||
|
|
||
| public class RacingController { | ||
| public void run(){ | ||
| try{ | ||
| final var cars = InputView.readCarNames(); //차이름 입력받기 | ||
| InputView.carNamesValidate(cars); //차 이름이 유효성 확인 | ||
| final var tryCount = InputView.readTryCount(); //시도 횟수 입력받기 | ||
| InputView.tryCountValidate(tryCount); //횟수 유효성 확인 | ||
|
|
||
| final var racingGame = new RacingGame(cars, tryCount); | ||
| race(racingGame); //경주 실행 | ||
|
|
||
| ResultView.printWinners(getWinners(racingGame.getCars())); //우승자 출력 | ||
|
|
||
| }catch(IllegalArgumentException e){ | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //경주 실행 | ||
| private void race(RacingGame racingGame){ | ||
| ResultView resultView = new ResultView(); | ||
| resultView.start(racingGame.getCars()); | ||
|
|
||
| for(int i = 0; i < racingGame.getTryCount(); i++){ | ||
| generateRandomDistance(racingGame.getCars()); | ||
| resultView.printRaceStep(racingGame.getCars()); | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| //난수 생성 후 거리 업데이트 하는 메소드 | ||
| private void generateRandomDistance(Map<String, Integer> cars){ | ||
| for(Map.Entry<String, Integer> entry : cars.entrySet()){ | ||
| Random random = new Random(); | ||
| int randomNum = random.nextInt(10); | ||
| RacingGame.updateDistance(entry.getKey(), randomNum); | ||
| } | ||
| } | ||
|
|
||
| //우승자 구하는 메소드 | ||
| private List<String> getWinners(Map<String, Integer> cars){ | ||
| List<String> winners = new ArrayList<>(); | ||
| int maxValue = Collections.max(cars.values()); | ||
|
|
||
| for(Map.Entry<String, Integer> entry : cars.entrySet()){ //이거 메소드로 분리하는게 가독성이 더 떨어질듯? | ||
| if(entry.getValue() == maxValue){ | ||
| winners.add(entry.getKey()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
| } |
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,32 @@ | ||
| package domain; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class RacingGame { | ||
| private static Map<String, Integer> cars; | ||
| private int tryCount; | ||
|
|
||
| public RacingGame(){ | ||
|
|
||
| } | ||
| public RacingGame(Map<String, Integer> cars, int tryCount){ | ||
| this.cars = cars; | ||
| this.tryCount = tryCount; | ||
| } | ||
|
|
||
| //자동차 distance 업데이트 메소드 | ||
| public static void updateDistance(String carName, int random){ | ||
| if(random >= 4) { | ||
| int distance = cars.get(carName) + 1; | ||
| cars.replace(carName, distance); | ||
| } | ||
| } | ||
|
|
||
| public Map<String, Integer> getCars() { | ||
| return cars; | ||
| } | ||
|
|
||
| public int getTryCount() { | ||
| return tryCount; | ||
| } | ||
| } | ||
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,54 @@ | ||
| package view; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class InputView { | ||
| private static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| //차이름 입력 메소드 | ||
| public static Map<String, Integer> readCarNames(){ | ||
| try{ | ||
| Map<String, Integer> cars = new HashMap<>(); | ||
| System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
| String line = scanner.nextLine(); | ||
| String[] str = line.split(","); | ||
| for(int i = 0; i<str.length; i++){ | ||
| cars.put(str[i], 1); | ||
| } | ||
| return cars; | ||
| }catch(IllegalArgumentException e){ | ||
| System.out.println(e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| //시도횟수 입력 메소드 | ||
| public static int readTryCount(){ | ||
| System.out.println("시도할 횟수는 몇회인가요?"); | ||
| int count = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
| return count; | ||
| } | ||
|
|
||
| //시도횟수 유효성 검사 메소드 | ||
| public static void tryCountValidate(int count){ | ||
| if(count <= 0) | ||
| throw new IllegalArgumentException("[ERROR]시도 횟수는 양수입니다."); | ||
| } | ||
|
|
||
| //차이름 유효성 검사 메소드 | ||
| public static void carNamesValidate(Map<String, Integer> cars){ | ||
| Iterator<String> keys = cars.keySet().iterator(); | ||
| while(keys.hasNext()){ | ||
| String key = keys.next(); | ||
| lengthValidate(key); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static void lengthValidate(String name){ | ||
| if(name.length() > 5) | ||
| throw new IllegalArgumentException("[ERROR]차 이름은 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,36 @@ | ||
| package view; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class ResultView { | ||
|
|
||
| //우승자 출력 메소드 | ||
| public static void printWinners(List<String> winners){ | ||
| int i; | ||
| for(i = 0; i < winners.size() - 1; i++){ | ||
| System.out.print(winners.get(i)+ ", "); | ||
| } | ||
| System.out.print(winners.get(i) + "가 최종 우승했습니다."); | ||
| } | ||
|
|
||
| //경주 단계 출력 메소드 | ||
| public void printRaceStep(Map<String, Integer> cars){ | ||
| for(Map.Entry<String, Integer> entry : cars.entrySet()){ | ||
| String name = entry.getKey(); | ||
| String distance = "-".repeat(cars.get(name)); | ||
| System.out.println(name + " : " + distance); | ||
| } | ||
| } | ||
|
|
||
| //경주 시작 출력 메소드 | ||
| public void start(Map<String, Integer> cars){ | ||
| System.out.println("실행 결과"); | ||
| printRaceStep(cars); | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
|
|
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,4 @@ | ||
| package controller; | ||
|
|
||
| public class RacingControllerTest { | ||
| } |
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,4 @@ | ||
| package domain; | ||
|
|
||
| public class RacingGameTest { | ||
| } |
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,4 @@ | ||
| package view; | ||
|
|
||
| public class InputViewTest { | ||
| } |
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,18 @@ | ||
| package view; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.assertj.*; | ||
|
|
||
| public class ResultViewTest { | ||
|
|
||
| @Test | ||
| void 기능_테스트(){ | ||
| //given | ||
|
|
||
| //when | ||
|
|
||
| //then | ||
|
|
||
|
|
||
| } | ||
| } |
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.
여긴 무슨공간인가요?
Uh oh!
There was an error while loading. Please reload this page.
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.
여백의 미랄까 ...
수정하겠습니다 !