-
Notifications
You must be signed in to change notification settings - Fork 33
[스프링 화요일 3팀] 차서영 미션 제출합니다. #15
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
o0Orangee
wants to merge
6
commits into
TEAM-ALOM:main
Choose a base branch
from
o0Orangee: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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d5a73e
domain 생성
o0Orangee 05d84f5
utils, view 생성
o0Orangee 6ffa098
controller 구현, view 수정, cars 수정, 메인 구현
o0Orangee b49a775
테스트 작성(CarName, TryCount)
o0Orangee 1bb36c6
테스트 작성(Car, Distance)
o0Orangee 0b6af28
테스트 작성(Parser)
o0Orangee 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # domain | ||
| - ## Car(자동차) ## | ||
| - 이름 | ||
| - 전진 거리 | ||
|
|
||
| - ## CarName(자동차 이름) ## | ||
| - 미입력 or 5글자 초과 확인 | ||
|
|
||
| - ## Distance(이동 거리) ## | ||
| - 정지 or 거리 증가 | ||
|
|
||
| - ## Cars(자동차 리스트) ## | ||
| - 자동차들의 정보(중복 이름 X) | ||
| - 우승자 판별 | ||
|
|
||
| - ## TryConut(시도 횟수) ## | ||
| - 음수 입력 X (횟수 상한은 없나??) | ||
|
|
||
| # ~~dto~~ | ||
|
|
||
| # controller | ||
| - 이름 입력받아서 Cars로 저장 | ||
|
|
||
| # utils | ||
| - ## Parser ## | ||
| - 이름: 쉼표 기준으로 분리 | ||
| - 시도 횟수 | ||
|
|
||
| - ## RandomNumberGenerator ## | ||
| - 0~9 랜덤 숫자 생성기 | ||
|
|
||
| # view | ||
| - ## InputView ## | ||
| - 자동차 이름 입력 | ||
| - 시도 횟수 입력 | ||
|
|
||
| - ## OutView ## | ||
| - 실행 결과 출력 | ||
| - 우승자 출력 |
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 RacingcarController { | ||
| } |
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,28 @@ | ||
| package domain; | ||
|
|
||
| public class Car { | ||
|
|
||
| private static final int CAN_MOVE_NUMBER = 4; | ||
| private final CarName carName; | ||
| private final Distance distance; | ||
|
|
||
|
|
||
| public Car(String carName) { | ||
| this.carName = new CarName(carName); | ||
| this.distance = new Distance(); | ||
| } | ||
|
|
||
| public void move(int number) { | ||
| if (number >= CAN_MOVE_NUMBER) { | ||
| distance.increaseDistance(); | ||
| } | ||
| } | ||
|
|
||
| public String getCarName() { | ||
| return carName.getCarName(); | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return distance.getDistance(); | ||
| } | ||
| } |
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,23 @@ | ||
| package domain; | ||
|
|
||
| public class CarName { | ||
| private final String carName; | ||
|
|
||
| public CarName(String carName) { | ||
| validateCarName(carName); | ||
| this.carName = carName; | ||
| } | ||
|
|
||
| public String getCarName() { | ||
| return carName; | ||
| } | ||
|
|
||
| private void validateCarName(String carName) { //이름 글자수 확인(없거나, 5글자 초과일 시) | ||
| if (carName.isEmpty()) { | ||
| throw new IllegalArgumentException("이름이 반드시 존재해야 합니다."); | ||
| } | ||
| if (carName.length() > 5) { | ||
| throw new IllegalArgumentException("이름은 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,53 @@ | ||
| package domain; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Cars { | ||
|
|
||
| private static final String DUPLICATED_NAMES_MASSAGE= "자동차의 이름은 중복될 수 없습니다."; | ||
| private static final int DEFAULT_DISTANCE = 0; | ||
| private final List<Car> cars; | ||
|
|
||
| public Cars(List<String> carNames) { | ||
| final List<Car> cars= generateCars(carNames); | ||
| validateDuplication(carNames); | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| private List<Car> generateCars(List<String> cars) { | ||
| return cars.stream() | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| // 이름 중복 확인 | ||
| private void validateDuplication(List<String> carNames) { | ||
| Set<String> duplicationCheck = new HashSet<>(carNames); | ||
| if (carNames.size() != duplicationCheck.size()) { | ||
| throw new IllegalArgumentException(DUPLICATED_NAMES_MASSAGE); | ||
| } | ||
| } | ||
|
|
||
| // 최대 이동 거리 구하기 | ||
| private int findMaxDistance() { | ||
| return cars.stream() | ||
| .mapToInt(Car::getDistance) | ||
| .max() | ||
| .orElse(DEFAULT_DISTANCE); | ||
| } | ||
|
|
||
| // 최대 이동 거리와 같은 위치에 있는 자동차들 찾기 | ||
| public List<Car> findWinner() { | ||
| int maxDistance = findMaxDistance(); | ||
| return cars.stream() | ||
| .filter(car -> car.getDistance() == maxDistance) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return 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,19 @@ | ||
| package domain; | ||
|
|
||
| public class Distance { | ||
|
|
||
| private static final int INITIAL_VALUE = 0; | ||
| private int distance; | ||
|
|
||
| public Distance() { | ||
| this.distance = INITIAL_VALUE; | ||
| } | ||
|
|
||
| public void increaseDistance() { | ||
| distance++; | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return 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,32 @@ | ||
| package domain; | ||
|
|
||
| public class TryCount { | ||
|
|
||
| private static final String NOT_POSITIVE_INTEGER_MESSAGE = "시도 횟수는 양의 정수여야 합니다"; | ||
| private static final int MINIMUM_COUNT = 1; | ||
|
|
||
| private int tryCount; | ||
|
|
||
| public TryCount(int tryCount) { | ||
| validate(tryCount); | ||
| this.tryCount = tryCount; | ||
| } | ||
|
|
||
| private void validate(int tryCount) { | ||
| if (tryCount <= 0) { | ||
| throw new IllegalArgumentException(NOT_POSITIVE_INTEGER_MESSAGE); | ||
| } | ||
| } | ||
|
|
||
| public void decrease() { | ||
| tryCount--; | ||
| } | ||
|
|
||
| public boolean isRemain() { | ||
| return tryCount >= MINIMUM_COUNT; | ||
| } | ||
|
|
||
| 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,23 @@ | ||
| package utils; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Parser { | ||
|
|
||
| private static final String NOT_POSITIVE_INTEGER = "시도 횟수는 정수여야 합니다"; | ||
| public static List<String> parseNames(String input) { | ||
| return Arrays.stream(input.split(",")) | ||
| .map(String::trim) | ||
|
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. 저는 공백을 포함하는 이름이 나왔을 때는 오류 처리를 하게 하였는데(이름 중간에 공백 나오면 에러) trim을 통해 이름 앞 뒤 공백을 없애는 방법으로 코드 작성하는 방식이 예외 처리 허용 부분에서저랑 좀 다르게 작성한 것 같습니다..! |
||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static int parseCount(String input) { | ||
| try { | ||
| return Integer.parseInt(input); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException(NOT_POSITIVE_INTEGER); | ||
| } | ||
| } | ||
| } | ||
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,7 @@ | ||
| package utils; | ||
|
|
||
| public class RandomNumberGenerator { | ||
| public int generate() { | ||
| return (int) (Math.random()*10); | ||
| } | ||
| } |
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 view; | ||
|
|
||
| import utils.Parser; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private static final String READ_CAR_NAMES_MESSAGE = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; | ||
| private static final String READ_TRY_COUNT_MESSAGE = "시도할 횟수는 몇회인가요?"; | ||
|
|
||
| private final Scanner scanner; | ||
|
|
||
| public InputView(Scanner scanner) { | ||
| this.scanner = scanner; | ||
| } | ||
|
|
||
| public List<String> readCarNames() { | ||
| System.out.println(READ_CAR_NAMES_MESSAGE); | ||
| String input = scanner.nextLine(); | ||
| return Parser.parseNames(input); | ||
| } | ||
|
|
||
| public int readTryCount() { | ||
| System.out.println(READ_TRY_COUNT_MESSAGE); | ||
| String input = scanner.nextLine(); | ||
| return Parser.parseCount(input); | ||
| } | ||
| } |
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,39 @@ | ||
| package view; | ||
|
|
||
| import domain.Car; | ||
| import domain.Cars; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| private static final String RESULT_MESSAGE = "실행 결과"; | ||
| private static final String STATUS_FORMAT = "%s : %s\n"; | ||
| private static final String WINNER_FORMAT = "%s가 최종 우승했습니다."; | ||
| private static final String HYPHEN = "-"; | ||
|
|
||
| public void printResult() { | ||
| System.out.println(RESULT_MESSAGE); | ||
| } | ||
|
|
||
| public void printStatus(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| String currentDistance = getCurrentDistance(car.getDistance()); | ||
| System.out.printf(STATUS_FORMAT, car.getCarName(), currentDistance); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void printWinners(List<Car> cars) { | ||
| List<String> carNames = cars.stream() | ||
| .map(Car::getCarName) | ||
| .toList(); | ||
| String winners = String.join(",", carNames); | ||
| System.out.printf(WINNER_FORMAT, winners); | ||
| } | ||
|
|
||
| private String getCurrentDistance(int distance) { | ||
| return HYPHEN.repeat(distance); | ||
| } | ||
| } |
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.
에러 메세지는 Enum Class를 사용해서 따로 모아두는 방법도 좋을 것 같아요!