Skip to content

Commit 41003fe

Browse files
[자동차 경주] 5단계 - 자동차 경주(리팩토링) (#6077)
* refactor 패키지 이동 * refactor Static 제거 * refactor 히스토리 객체 분리 * refactor name 피드백 반영 * refactor isPosition 에서 인자로 CarPosition 받기 * refactor 객체에서 iterator 를 사용하게 변경 * refactor FixedCar -> CarSnapshot, CarsAtTurn -> TurnSnapshot --------- Co-authored-by: luka.redwing(이재석)/kakao <[email protected]>
1 parent 54dade5 commit 41003fe

24 files changed

+403
-287
lines changed

src/main/java/racing/CarPositions.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/racing/PlayHistory.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/main/java/racing/PositionCompare.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/racing/RacingSession.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/main/java/racing/RacingCar.java renamed to src/main/java/racing/controller/RacingCar.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package racing;
1+
package racing.controller;
22

3-
import view.InputView;
4-
import view.ResultView;
3+
import racing.model.RacingResultDto;
4+
import racing.model.RacingSession;
5+
import racing.view.InputView;
6+
import racing.view.ResultView;
57

68
public class RacingCar {
79

src/main/java/racing/Car.java renamed to src/main/java/racing/model/Car.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
package racing;
1+
package racing.model;
22

33
import java.util.ArrayList;
44
import java.util.List;
55

66
public class Car {
77

88
public static final int START_POSITION = 1;
9-
private int position;
9+
public static final int MIN_PROGRESS_NUMBER = 4;
10+
private final CarPosition position;
1011
private final CarName name;
1112

1213
private Car(CarName name) {
13-
this.position = START_POSITION;
14+
this.position = new CarPosition(START_POSITION);
1415
this.name = name;
1516
}
1617

1718
public static Car of(String name) {
1819
return new Car(new CarName(name));
1920
}
2021

21-
public int move(boolean canProgress) {
22-
if (canProgress) {
23-
this.position++;
22+
public CarPosition move(int number) {
23+
if (canProgress(number)) {
24+
this.position.progress();
2425
}
2526
return this.position;
2627
}
2728

28-
public String getName(){
29+
public boolean canProgress(int number) {
30+
return number >= MIN_PROGRESS_NUMBER;
31+
}
32+
33+
public boolean isPosition(CarPosition position) {
34+
return this.position.equals(position);
35+
}
36+
37+
public String getName() {
2938
return this.name.getName();
3039
}
3140

41+
public CarSnapshot snapshot() {
42+
return new CarSnapshot(this.name, this.position);
43+
}
44+
3245
public static List<Car> createCars(String carNames) {
3346
String[] names = carNames.split(",");
3447

src/main/java/racing/CarName.java renamed to src/main/java/racing/model/CarName.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package racing;
1+
package racing.model;
22

33
public class CarName {
4+
45
public static final int MAX_NAME_LENGTH = 5;
56
private final String name;
67

78
public CarName(String name) {
8-
if (checkNameLengthExceed(name)){
9-
throw new IllegalArgumentException("차 이름은 5자 이하여야 합니다. input: " + name);
9+
if (checkNameLengthExceed(name)) {
10+
throw new IllegalArgumentException("차 이름은 " + MAX_NAME_LENGTH + "자 이하여야 합니다. input: " + name);
1011
}
1112
this.name = name;
1213
}
@@ -15,7 +16,7 @@ public String getName() {
1516
return name;
1617
}
1718

18-
private static boolean checkNameLengthExceed(String name) {
19+
private boolean checkNameLengthExceed(String name) {
1920
return name.length() > MAX_NAME_LENGTH;
2021
}
2122

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package racing.model;
2+
3+
import java.util.Objects;
4+
5+
public class CarPosition {
6+
7+
private Integer position;
8+
9+
public CarPosition(Integer position) {
10+
this.position = position;
11+
}
12+
13+
public int getPosition() {
14+
return this.position;
15+
}
16+
17+
public void progress() {
18+
position++;
19+
}
20+
21+
public int max(int position) {
22+
return Math.max(position, this.position);
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (this == o) {
28+
return true;
29+
}
30+
if (o == null || getClass() != o.getClass()) {
31+
return false;
32+
}
33+
CarPosition that = (CarPosition) o;
34+
return Objects.equals(position, that.position);
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
return Objects.hash(position);
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package racing.model;
2+
3+
public class CarSnapshot {
4+
5+
private final CarPosition position;
6+
private final CarName name;
7+
8+
public CarSnapshot(String name, int position) {
9+
this.position = new CarPosition(position);
10+
this.name = new CarName(name);
11+
}
12+
13+
public CarSnapshot(CarName name, CarPosition position) {
14+
this.position = position;
15+
this.name = name;
16+
}
17+
18+
public String getName() {
19+
return this.name.getName();
20+
}
21+
22+
public int getPosition() {
23+
return this.position.getPosition();
24+
}
25+
26+
public int max(int position) {
27+
return this.position.max(position);
28+
}
29+
30+
public boolean isPosition(CarPosition position) {
31+
return this.position.equals(position);
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package racing.model;
2+
3+
import java.util.Collections;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
public class PlayHistory implements Iterable<TurnSnapshot> {
8+
9+
private final List<TurnSnapshot> positions;
10+
11+
public PlayHistory(List<TurnSnapshot> positions) {
12+
this.positions = positions;
13+
}
14+
15+
public List<String> findWinners() {
16+
TurnSnapshot lastTurn = getLastTurn();
17+
return lastTurn.findMaxPositionCarNames();
18+
}
19+
20+
private TurnSnapshot getLastTurn() {
21+
return this.positions.get(this.positions.size() - 1);
22+
}
23+
24+
@Override
25+
public Iterator<TurnSnapshot> iterator() {
26+
return Collections.unmodifiableList(positions).iterator();
27+
}
28+
29+
}

src/main/java/racing/RacingResultDto.java renamed to src/main/java/racing/model/RacingResultDto.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
package racing;
1+
package racing.model;
22

33
import java.util.List;
44

55
public class RacingResultDto {
6-
private final List<String> carNames;
76
private final PlayHistory playHistory;
87
private final List<String> winnerNames;
98

10-
public RacingResultDto(List<String> carNames, PlayHistory playHistory, List<String> winnerNames) {
11-
this.carNames = carNames;
9+
public RacingResultDto(PlayHistory playHistory,
10+
List<String> winnerNames) {
1211
this.playHistory = playHistory;
1312
this.winnerNames = winnerNames;
1413
}
1514

16-
public List<String> getCarNames() {
17-
return carNames;
18-
}
19-
2015
public PlayHistory getPlayHistory() {
2116
return playHistory;
2217
}

0 commit comments

Comments
 (0)