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

[Step5] 자동차경주(리팩토링) 코드리뷰, 라이브 강의 피드백 적용 #5482

Open
wants to merge 8 commits into
base: kbzz17
Choose a base branch
from
23 changes: 21 additions & 2 deletions src/main/java/racingCar/CarName.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package racingCar;

import java.util.Objects;

public class CarName {

private static final int NAME_LENGTH_LIMIT = 5;
Expand All @@ -11,13 +13,30 @@ public CarName(String name) {
this.name = name;
}

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

private void validName(String name) {
if (name.length() > NAME_LENGTH_LIMIT) {
throw new IllegalArgumentException("이름은 5글자를 초과할 수 없습니다");
}
}

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

@Override
public int hashCode() {
return Objects.hash(name);
}
}
49 changes: 49 additions & 0 deletions src/main/java/racingCar/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package racingCar;

import java.util.Objects;

public class Location {

private final static int MIN_LIMIT = 0;
private int location;

public Location(){
this.location = MIN_LIMIT;
}

public Location(int location) {
validLocation(location);
this.location = location;
}

public void increase(){
location++;
}

private void validLocation(int location) {
if(location < MIN_LIMIT){
throw new IllegalArgumentException("위치 값은 음수일 수 없습니다.");
}
}

public int getLocation() {
return location;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Location location1 = (Location) o;
return location == location1.location;
}

@Override
public int hashCode() {
return Objects.hash(location);
}
}
16 changes: 0 additions & 16 deletions src/main/java/racingCar/MoveStrategy.java

This file was deleted.

52 changes: 42 additions & 10 deletions src/main/java/racingCar/RacingCar.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
package racingCar;

import java.util.Objects;

public class RacingCar {

private static final int START_LOCATION = 0;
private CarName name;
private int location;
private Location location;

public RacingCar() {
this.location = START_LOCATION;
this.location = new Location();
}

public RacingCar(CarName name) {
this.name = name;
this.location = START_LOCATION;
this.location = new Location();
}

public RacingCar(CarName name, Location location) {
this.name = name;
this.location = location;
}

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

public boolean isSameLocation(int location) {
return findCurrentLocation() == location;
}

public int compareMaxDistance(int distance) {
return Math.max(findCurrentLocation(), distance);
}

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

public void move(boolean isMovable) {
if (isMovable) {
this.location++;
this.location.increase();
}
}

public String toString() {
StringBuilder sb = new StringBuilder();
return sb.append(name.findName())
return sb.append(findCarName())
.append(" : ")
.append("-".repeat(Math.max(0, location)))
.append("-".repeat(Math.max(0, location.getLocation())))
.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RacingCar racingCar = (RacingCar) o;
return Objects.equals(name, racingCar.name) && Objects.equals(location,
racingCar.location);
}

@Override
public int hashCode() {
return Objects.hash(name, location);
}
}
30 changes: 24 additions & 6 deletions src/main/java/racingCar/RacingCars.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package racingCar;

import java.util.List;
import java.util.Objects;

public class RacingCars {

private final List<RacingCar> cars;

private RacingRule rule;

public RacingCars(List<RacingCar> cars) {
this.cars = cars;
}

public RacingCars(List<RacingCar> cars, RacingRule rule) {
this.cars = cars;
this.rule = rule;
}

public List<RacingCar> findCars() {
return this.cars;
}

public void moveCars() {
for (RacingCar car : cars) {
car.move(RacingRule.isMovable(RacingRule.generateRandomDistance()));
car.move(rule.isMovable());
}
}

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

public int findFarthestLocation() {
return cars.stream()
.mapToInt(RacingCar::findCurrentLocation)
.max()
.getAsInt();
@Override
public int hashCode() {
return Objects.hash(cars, rule);
}
}
13 changes: 10 additions & 3 deletions src/main/java/racingCar/RacingReferee.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

public class RacingReferee {

public static int findFarthestLocation(RacingCars cars) {
int maxDistance = 0;
for (RacingCar car : cars.findCars()) {
maxDistance = car.compareMaxDistance(maxDistance);
}
return maxDistance;
}

public static List<String> pickWinners(RacingCars cars) {
int farthestLocation = cars.findFarthestLocation();
int farthestLocation = findFarthestLocation(cars);
return cars.findCars().stream()
.filter((car)->car.findCurrentLocation() == farthestLocation)
.filter((car) -> car.isSameLocation(farthestLocation))
.map(RacingCar::findCarName)
.map(CarName::findName)
.collect(Collectors.toList());
}

Expand Down
17 changes: 2 additions & 15 deletions src/main/java/racingCar/RacingRule.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package racingCar;

import java.util.Random;

public class RacingRule {
public static int MOVABLE_STANDARD = 4;
public static int RANDOM_STANDARD = 10;

private static final Random random = new Random();

public static boolean isMovable(int distance) {
return distance >= MOVABLE_STANDARD;
}

public static int generateRandomDistance(){
return random.nextInt(RANDOM_STANDARD);
}
public interface RacingRule {
boolean isMovable();
Comment on lines +3 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
19 changes: 19 additions & 0 deletions src/main/java/racingCar/RandomRacingRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racingCar;

import java.util.Random;

public class RandomRacingRule implements RacingRule{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public static int MOVABLE_STANDARD = 4;
public static int RANDOM_STANDARD = 10;

private static final Random random = new Random();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상수는 보통 대문자와 밑줄(_)만 사용합니다


@Override
public boolean isMovable() {
return generateRandomDistance() >= MOVABLE_STANDARD;
}

public static int generateRandomDistance(){
return random.nextInt(RANDOM_STANDARD);
}
}
2 changes: 1 addition & 1 deletion src/test/java/racingCar/CarNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CarNameTest {
@DisplayName("이름이 5자 미만일 시 이름 return")
void CarName_Correct_Length() {
CarName carName = new CarName("pobi");
assertThat(carName.findName()).isEqualTo("pobi");
assertThat(carName.getName()).isEqualTo("pobi");
}

@Test
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/racingCar/LocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package racingCar;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class LocationTest {

@Test
@DisplayName("위치 값에 음수가 들어오면 IllegalArgumentException")
void Location_MinusTest() {
Assertions.assertThatThrownBy(()->new Location(-1))
.isInstanceOf(IllegalArgumentException.class);
}
}
43 changes: 0 additions & 43 deletions src/test/java/racingCar/MoveStrategyTest.java

This file was deleted.

Loading