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단계 - 자동차 경주(리팩토링) #6077

Merged
merged 7 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 19 deletions src/main/java/racing/CarPositions.java

This file was deleted.

44 changes: 0 additions & 44 deletions src/main/java/racing/PlayHistory.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/racing/PositionCompare.java

This file was deleted.

63 changes: 0 additions & 63 deletions src/main/java/racing/RacingSession.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package racing;
package racing.controller;

import view.InputView;
import view.ResultView;
import racing.model.RacingResultDto;
import racing.model.RacingSession;
import racing.view.InputView;
import racing.view.ResultView;

public class RacingCar {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
package racing;
package racing.model;

import java.util.ArrayList;
import java.util.List;

public class Car {

public static final int START_POSITION = 1;
private int position;
public static final int MIN_PROGRESS_NUMBER = 4;
private final CarPosition position;
private final CarName name;

private Car(CarName name) {
this.position = START_POSITION;
this.position = new CarPosition(START_POSITION);
this.name = name;
}

public static Car of(String name) {
return new Car(new CarName(name));
}

public int move(boolean canProgress) {
if (canProgress) {
this.position++;
public CarPosition move(int number) {
if (canProgress(number)) {
this.position.progress();
}
return this.position;
}

public String getName(){
public boolean canProgress(int number) {
return number >= MIN_PROGRESS_NUMBER;
}

public boolean isPosition(CarPosition position) {
return this.position.equals(position);
}

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

public CarSnapshot snapshot() {
return new CarSnapshot(this.name, this.position);
}

public static List<Car> createCars(String carNames) {
String[] names = carNames.split(",");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package racing;
package racing.model;

public class CarName {

public static final int MAX_NAME_LENGTH = 5;
private final String name;

public CarName(String name) {
if (checkNameLengthExceed(name)){
throw new IllegalArgumentException("차 이름은 5자 이하여야 합니다. input: " + name);
if (checkNameLengthExceed(name)) {
throw new IllegalArgumentException("차 이름은 " + MAX_NAME_LENGTH + "자 이하여야 합니다. input: " + name);
}
this.name = name;
}
Expand All @@ -15,7 +16,7 @@ public String getName() {
return name;
}

private static boolean checkNameLengthExceed(String name) {
private boolean checkNameLengthExceed(String name) {
return name.length() > MAX_NAME_LENGTH;
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/racing/model/CarPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racing.model;

import java.util.Objects;

public class CarPosition {

private Integer position;

public CarPosition(Integer position) {
this.position = position;
}

public int getPosition() {
return this.position;
}

public void progress() {
position++;
}

public int max(int position) {
return Math.max(position, this.position);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CarPosition that = (CarPosition) o;
return Objects.equals(position, that.position);
}

@Override
public int hashCode() {
return Objects.hash(position);
}
}
33 changes: 33 additions & 0 deletions src/main/java/racing/model/CarSnapshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package racing.model;

public class CarSnapshot {

private final CarPosition position;
private final CarName name;

public CarSnapshot(String name, int position) {
this.position = new CarPosition(position);
this.name = new CarName(name);
}

public CarSnapshot(CarName name, CarPosition position) {
this.position = position;
this.name = name;
}

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

public int getPosition() {
return this.position.getPosition();
}

public int max(int position) {
return this.position.max(position);
}

public boolean isPosition(CarPosition position) {
return this.position.equals(position);
}
}
29 changes: 29 additions & 0 deletions src/main/java/racing/model/PlayHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package racing.model;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class PlayHistory implements Iterable<TurnSnapshot> {

private final List<TurnSnapshot> positions;

public PlayHistory(List<TurnSnapshot> positions) {
this.positions = positions;
}

public List<String> findWinners() {
TurnSnapshot lastTurn = getLastTurn();
return lastTurn.findMaxPositionCarNames();
}

private TurnSnapshot getLastTurn() {
return this.positions.get(this.positions.size() - 1);
}

@Override
public Iterator<TurnSnapshot> iterator() {
return Collections.unmodifiableList(positions).iterator();
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package racing;
package racing.model;

import java.util.List;

public class RacingResultDto {
private final List<String> carNames;
private final PlayHistory playHistory;
private final List<String> winnerNames;

public RacingResultDto(List<String> carNames, PlayHistory playHistory, List<String> winnerNames) {
this.carNames = carNames;
public RacingResultDto(PlayHistory playHistory,
List<String> winnerNames) {
this.playHistory = playHistory;
this.winnerNames = winnerNames;
}

public List<String> getCarNames() {
return carNames;
}

public PlayHistory getPlayHistory() {
return playHistory;
}
Expand Down
Loading