Skip to content
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

5단계 - 자동차 경주(리팩토링) #5184

Open
wants to merge 30 commits into
base: sudo-init
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
69a9998
split study test
sudo-init Nov 1, 2023
cc3c46d
split study test
sudo-init Nov 1, 2023
61eb83c
step 1 practice
sudo-init Nov 2, 2023
5bd4cbd
first commit
sudo-init Nov 2, 2023
bd57b26
step2 mission completed.
sudo-init Nov 3, 2023
f479491
step2 refactoring completed.
sudo-init Nov 6, 2023
bc303b0
step2 feedback is reflected.
sudo-init Nov 7, 2023
befcc95
tmp(backup)
sudo-init Nov 7, 2023
99a12a0
tmp(backup)
sudo-init Nov 8, 2023
c6f7504
feat(feature) - 모든 기능 구현
sudo-init Nov 9, 2023
4a7efd7
Merge branch 'sudo-init' into step3
sudo-init Nov 9, 2023
7177fcf
refactory and test(add moveCar Test etc..)
sudo-init Nov 10, 2023
7d4799e
refactor (#5075): 피드백 요청사항 반영 및 리팩토링, 테스트 코드 수정
sudo-init Nov 13, 2023
e01686f
feat: 입력한 이름 개수 만큼 자동차 생성
sudo-init Nov 15, 2023
7bb20e5
feat: 자동차 이름5자리 제한 기능 추가
sudo-init Nov 15, 2023
e2bfae9
refactor and test: 자동차 이름을 관리하는 객체 정의
sudo-init Nov 15, 2023
3512e4f
chore
sudo-init Nov 15, 2023
885aa7a
feat and refactor: 출력 결과에 자동차 이름이 출력되도록
sudo-init Nov 16, 2023
14dd353
backup
sudo-init Nov 16, 2023
57963e5
Tmp backup
sudo-init Nov 17, 2023
0d3d66e
refactor and feat: 우승자 선정 및 출력 기능 추가 & 리팩토링
sudo-init Nov 20, 2023
b8cbdb7
Merge branch 'sudo-init' into step4
sudo-init Nov 20, 2023
1aac7df
commit test
sudo-init Nov 22, 2023
543acdc
Merge branch 'step4' of https://github.com/sudo-init/java-racingcar i…
sudo-init Nov 22, 2023
2b1933c
fix: 잘못된 merge에 대한 수정
sudo-init Nov 22, 2023
a83d263
chore: 자동차 이름 예외 발생
sudo-init Nov 22, 2023
1b388ca
refactoring: RaceResult 클래스 삭제
sudo-init Nov 25, 2023
524dea6
refactor and test: Name과 Location 클래스 생성
sudo-init Nov 27, 2023
89471d7
Merge branch 'sudo-init' into step5
sudo-init Nov 27, 2023
ffd0a88
chore: merge conflict resolved.
sudo-init Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/main/java/study/race/model/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

public class Car {

private String name;
private int currentLocation;
private CarMovingDistance carMovingDistance;
private Name name;
private Location location;

public Car(String name) {
this.name = name;
this.carMovingDistance = new CarMovingDistance();
this.currentLocation = carMovingDistance.initMovingDistance();
this.name = new Name(name);
this.location = new Location();
}

public String getName() {
return this.name;
return this.name.getName();
}

public int getCurrentLocation() {
return this.currentLocation;
public int getLocation() {
return this.location.getLocation();
}

public void move(int movingDistance) {
this.currentLocation = this.carMovingDistance.updateMovingDistance(this.currentLocation, movingDistance);
public void move() {
this.location.updateMovingDistance();
}
}
23 changes: 0 additions & 23 deletions src/main/java/study/race/model/CarMovingDistance.java

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/java/study/race/model/Cars.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public void addCar(String name) {
this.carList.add(new Car(name));
}

public void moveCarAt(int i, int movingDistance) {
this.carList.get(i).move(movingDistance);
public void moveCarAt(int i) {
this.carList.get(i).move();
}

public int size() {
Expand All @@ -34,7 +34,7 @@ public Car get(int i) {
}

public int getCarDistanceAt(int i) {
return this.carList.get(i).getCurrentLocation();
return this.carList.get(i).getLocation();
}

public Winners getWinners() {
Expand All @@ -44,15 +44,15 @@ public Winners getWinners() {
private int findMaxDistance() {
int maxDistance = 0;
for (Car car : carList) {
maxDistance = Math.max(maxDistance, car.getCurrentLocation());
maxDistance = Math.max(maxDistance, car.getLocation());
}
return maxDistance;
}

private Winners findWinners(int maxDistance) {
Winners winners = new Winners(maxDistance);
for (Car car : carList) {
winners.checkAndAddWinner(car.getName(), car.getCurrentLocation());
winners.checkAndAddWinner(car.getName(), car.getLocation());
}
return winners;
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/study/race/model/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package study.race.model;

import java.util.Random;

public class Location {

private static final int STARTING_POINT = 0;
private static final int FORWARD = 1;
private static final int FORWARD_CONDITION_THRESHOLD = 4;
private static final int RANDOM_LIMIT_NUMBER = 10;

private int location;
private Random random;

public Location() {
this.location = STARTING_POINT;
random = new Random();
}

public void updateMovingDistance() {
if (checkForwardCondition()) {
this.location += FORWARD;
}
}

private boolean checkForwardCondition() {
return random.nextInt(RANDOM_LIMIT_NUMBER) >= FORWARD_CONDITION_THRESHOLD;
}

public int getLocation() {
return this.location;
}
}
22 changes: 22 additions & 0 deletions src/main/java/study/race/model/Name.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package study.race.model;

public class Name {

private final int CAR_NAME_LENGTH_LIMIT = 5;

private String name;

public Name(String name) {
checkOverLength(name);
this.name = name;
}

private void checkOverLength(String name) {
if (name.length() > CAR_NAME_LENGTH_LIMIT)
throw new IllegalArgumentException("String length exceeds the allowed maximum(5).");
}

public String getName() {
return this.name;
}
}
5 changes: 1 addition & 4 deletions src/main/java/study/race/model/Racing.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

public class Racing {

private static final int RANDOM_LIMIT_NUMBER = 10;

private Random random;
private Cars cars;

Expand All @@ -16,8 +14,7 @@ public Racing(Cars cars) {

public void start() {
for (int i = 0; i < this.cars.size(); i++) {
int movingDistance = random.nextInt(RANDOM_LIMIT_NUMBER);
this.cars.moveCarAt(i, movingDistance);
this.cars.moveCarAt(i);
}
}

Expand Down
19 changes: 1 addition & 18 deletions src/main/java/study/race/model/RacingRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@

public class RacingRegister {

private final int CAR_NAME_LENGTH_LIMIT = 5;

private String[] carNameList;

public Cars register(String carName) {
this.carNameList = carName.split(",");
checkCarNameValid(carNameList);
return carRegister(carNameList);
}

private void checkCarNameValid(String[] names) {
for (String name : names) {
isOverLength(name);
}
}

private void isOverLength(String name) {
if (name.length() > CAR_NAME_LENGTH_LIMIT)
throw new IllegalArgumentException("String length exceeds the allowed maximum(5).");
return carRegister(carName.split(","));
}

private Cars carRegister(String[] carNameList) {
Expand Down
20 changes: 0 additions & 20 deletions src/test/java/study/CarRacingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,6 @@ void generateCarsTest() {
assertThat(cars.size()).isEqualTo(CAR_NAMES.split(",").length);
}

@ParameterizedTest(name = "moving distance: {0}, current location: {1}")
@DisplayName("When moving distance is 4 or more, car moving Test")
@CsvSource({"4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"})
void moveCar(int movingDistance, int expected) {
Car car = moveCars.get(0);
car.move(movingDistance);
int result = car.getCurrentLocation();
assertThat(result).isEqualTo(expected);
}

@ParameterizedTest(name = "moving distance: {0}, current location: {1}")
@DisplayName("When moving distance is 3 or less, car moving Test")
@CsvSource({"0, 0", "1, 0", "2, 0", "3, 0"})
void stopCar(int movingDistance, int expected) {
Car car = stopCars.get(0);
car.move(movingDistance);
int result = car.getCurrentLocation();
assertThat(result).isEqualTo(expected);
}

@ParameterizedTest()
@DisplayName("Generating car as many as car names")
@ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"})
Expand Down