-
Notifications
You must be signed in to change notification settings - Fork 79
[그리디] 염지환 자동차 경주 3, 4단계 제출합니다! #114
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
b260211
* feat : 1단계 구현
JihwanYeom d5a9797
* test : Car 객체 테스트 코드 작성
JihwanYeom 92c56ec
* feat : 2단계 구현
JihwanYeom 9e69781
* test : makeWinnerList() 테스트코드 작성
JihwanYeom d187dc8
* docs : README 작성
JihwanYeom 15ed321
* docs : README 수정
JihwanYeom e38bd09
* fix : Car, Game 클래스 리뷰 1차 반영
JihwanYeom 96b822e
* test : 테스트 코드 리뷰 1차 반영
JihwanYeom 11094f8
* feat : 3단계 구현
JihwanYeom 7b7c714
* test : 4단계 부분 구현
JihwanYeom a1b7358
Merge branch 'jihwanyeom' into jihwanyeom
JihwanYeom 833a5a8
* test : Car 테스트 수정
JihwanYeom 46cbdbf
* test : Cars 테스트 코드 작성
JihwanYeom 1386774
* test : Game 테스트 코드 삭제 및 Racing 테스트 코드 작성
JihwanYeom e6e1089
* test : Controller 테스트 코드 작성
JihwanYeom 40fd209
* test : View 테스트 코드 작성
JihwanYeom 2943098
* refactor : Controller 인스턴스 변수 최소화
JihwanYeom e9de809
* refactor : 같은 거리의 자동차를 구하는 로직의 메서드 명 변경
JihwanYeom 34995d0
* feat : 사용자 입력 안내 메세지 출력 메서드 추가
JihwanYeom 759de06
* style : RacingApplicaion에 개행 추가
JihwanYeom bd2d256
* docs : README.md 업데이트
JihwanYeom df2090b
* chore : 생성자 중복제거
JihwanYeom 30742b5
Merge remote-tracking branch 'origin/jihwanyeom' into jihwanyeom
JihwanYeom c916e39
docs : README.md 오타 변경
JihwanYeom 2c48874
chore : 불필요해진 파일 삭제
JihwanYeom 0c0ba23
style : 실행파일 불필요한 개행 제거
JihwanYeom cb2c86a
feat : 이름에 대한 예외처리 추가
JihwanYeom 0050f0f
feat : 이름에 대한 예외처리를 입력단에 추가
JihwanYeom 4995016
refactor : Cars 객체 생성전략 변경
JihwanYeom f4133eb
refactor : RandomNumberGenerator 클래스 수정
JihwanYeom eed3c57
refactor : OutputView 포맷 관련 메서드 수정
JihwanYeom 1350e36
test : Cars 테스트에 Fixture 적용
JihwanYeom 38976c5
style : 코드 서식 점검
JihwanYeom 97bb64d
test : 입력 서식 검증 테스트 추가
JihwanYeom 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,9 @@ | ||
import controller.RacingController; | ||
|
||
public class RacingApplication { | ||
|
||
public static void main(String[] args) { | ||
new RacingController().run(); | ||
} | ||
|
||
} |
This file contains 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,50 @@ | ||
package controller; | ||
|
||
import domain.Car; | ||
import domain.Cars; | ||
import domain.Racing; | ||
import domain.NumberGenerator; | ||
import domain.RandomNumberGenerator; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
public class RacingController { | ||
|
||
private static final NumberGenerator randomGenerator = new RandomNumberGenerator(); | ||
|
||
public void run() { | ||
|
||
OutputView.printInputCarNames(); | ||
List<String> carNames = InputView.getCarNames(); | ||
Cars cars = Cars.from(createCars(carNames)); | ||
|
||
OutputView.printInputRoundNumber(); | ||
int roundNumber = InputView.getRoundNumber(); | ||
|
||
Racing racing = new Racing(cars); | ||
|
||
OutputView.printResult(); | ||
playRace(racing, cars, roundNumber); | ||
|
||
Cars winnerList = racing.findWinners(); | ||
OutputView.printWinners(winnerList.getCars()); | ||
} | ||
|
||
public List<Car> createCars(List<String> names) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String name : names) { | ||
cars.add(new Car(name, randomGenerator)); | ||
} | ||
return cars; | ||
} | ||
|
||
public void playRace(Racing racing, Cars cars, int roundNumber) { | ||
for (int i = 0; i < roundNumber; i++) { | ||
racing.playRound(); | ||
OutputView.printProcess(cars.getCars()); | ||
} | ||
} | ||
|
||
} |
This file contains 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,35 @@ | ||
package domain; | ||
|
||
public class Car { | ||
|
||
private static final int CRITICAL_NUMBER_TO_MOVE = 4; | ||
|
||
private final Name name; | ||
private int distance; | ||
private final NumberGenerator numberGenerator; | ||
|
||
public Car(String name, NumberGenerator numberGenerator) { | ||
this.name = Name.from(name); | ||
this.distance = 0; | ||
this.numberGenerator = numberGenerator; | ||
} | ||
|
||
public void move() { | ||
if (numberGenerator.generateNumber() >= CRITICAL_NUMBER_TO_MOVE) { | ||
distance++; | ||
} | ||
} | ||
|
||
public boolean hasSamePosition(int position) { | ||
return distance == position; | ||
} | ||
|
||
public String getName() { | ||
return name.getName(); | ||
} | ||
|
||
public int getDistance() { | ||
return distance; | ||
} | ||
|
||
} |
This file contains 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,70 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class Cars { | ||
|
||
private static final int MAX_CAR_NUMBER = 10; | ||
|
||
private final List<Car> cars; | ||
|
||
private Cars(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public static Cars from(List<Car> cars) { | ||
checkCarNumber(cars); | ||
checkDuplicates(cars); | ||
return new Cars(new ArrayList<>(cars)); | ||
} | ||
|
||
public static void checkCarNumber(List<Car> cars) { | ||
if (cars.size() > MAX_CAR_NUMBER) { | ||
throw new IllegalArgumentException("[Error] 차의 개수는 10대를 초과할 수 없습니다."); | ||
} | ||
} | ||
|
||
public static void checkDuplicates(List<Car> cars) { | ||
if (hasDuplicates(cars)) { | ||
throw new IllegalArgumentException("[Error] 차의 이름은 중복될 수 없습니다."); | ||
} | ||
} | ||
|
||
private static boolean hasDuplicates(List<Car> cars) { | ||
Set<String> nameSet = new HashSet<>(); | ||
return cars.stream() | ||
.map(Car::getName) | ||
.anyMatch(name -> !nameSet.add(name)); | ||
} | ||
|
||
public void move() { | ||
for (Car car : cars) { | ||
car.move(); | ||
} | ||
} | ||
|
||
public int getMaxDistance() { | ||
int maxDistance = 0; | ||
for (Car car : cars) { | ||
maxDistance = Math.max(maxDistance, car.getDistance()); | ||
} | ||
return maxDistance; | ||
} | ||
|
||
public Cars findCarsHasSamePosition(int position) { | ||
List<Car> carsHasSamePosition = cars.stream() | ||
.filter(car -> car.hasSamePosition(position)) | ||
.collect(Collectors.toList()); | ||
|
||
return new Cars(carsHasSamePosition); | ||
} | ||
|
||
public List<Car> getCars() { | ||
return new ArrayList<>(cars); | ||
} | ||
|
||
} |
This file contains 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,35 @@ | ||
package domain; | ||
|
||
public class Name { | ||
|
||
private static final int MAX_LENGTH = 5; | ||
|
||
private final String name; | ||
|
||
private Name(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public static Name from(final String name) { | ||
checkBlank(name); | ||
checkLength(name); | ||
return new Name(name); | ||
} | ||
|
||
public static void checkLength(String name) { | ||
if (name.length() > MAX_LENGTH) { | ||
throw new IllegalArgumentException("[Error] 이름은 " + MAX_LENGTH + "자를 초과할 수 없습니다."); | ||
} | ||
} | ||
|
||
public static void checkBlank(String name) { | ||
if (name.trim().isEmpty()) { | ||
throw new IllegalArgumentException("[Error] 이름은 공백으로 이루어질 수 없습니다."); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
} |
This file contains 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,5 @@ | ||
package domain; | ||
|
||
public interface NumberGenerator { | ||
int generateNumber(); | ||
} |
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.
추가된 요구사항에 맞게 기능 명세서도 업데이트 하셨네요!
문서화도 관리 대상입니다. 아주 좋은 습관이에요👍👍