-
Notifications
You must be signed in to change notification settings - Fork 11
[이혁진] 자동차 경주 게임 step2 코드리뷰 부탁드립니다 #17
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
base: hyukjin-step2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package racing; | ||
|
||
import racing.domain.RacingGame; | ||
import racing.dto.RacingGameResult; | ||
import racing.view.InputView; | ||
import racing.view.OutputView; | ||
|
||
public class RacingMain { | ||
|
||
private static RacingGame racingGame = new RacingGame(); | ||
|
||
public static void main(String[] args) { | ||
racingGame.setCars(InputView.readCarInfo()); | ||
RacingGameResult result = racingGame.play(InputView.readTryRound()); | ||
|
||
OutputView.printRacingGameResult(result); | ||
OutputView.printWinners(result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.CarDTO; | ||
|
||
import java.util.Objects; | ||
|
||
public class Car { | ||
|
||
private String name; | ||
private int distance; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getDistance() { | ||
return distance; | ||
} | ||
|
||
public int move() { | ||
return this.distance++; | ||
} | ||
|
||
public CarDTO toCarDTO() { | ||
return new CarDTO(name, distance); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Car car = (Car) o; | ||
return distance == car.distance && | ||
Objects.equals(name, car.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, distance); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Car{" + | ||
"name='" + name + '\'' + | ||
", distance=" + distance + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.RacingGameResult; | ||
import racing.utils.RandomValueGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class RacingGame { | ||
|
||
private static final int FORWARD_CONDITION = 4; | ||
private static final int BOUNDARY = 10; | ||
|
||
private List<Car> cars; | ||
|
||
public void setCars(String carNames) { | ||
String[] carArrays = carNames.split(","); | ||
cars = new ArrayList<>(); | ||
|
||
for (int i = 0; i < carArrays.length; i++) { | ||
cars.add(new Car(carArrays[i])); | ||
} | ||
} | ||
|
||
public RacingGameResult play(int tryRound) { | ||
for (int i = 0; i < tryRound; i++) { | ||
moveCars(); | ||
} | ||
return new RacingGameResult(cars.stream().map(Car::toCarDTO).collect(Collectors.toList())); | ||
} | ||
|
||
public void moveCars() { | ||
for (Car car : cars) { | ||
moveSelectedCar(car, RandomValueGenerator.generate(BOUNDARY)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 랜덤값을 moveSelectedCar() 메소드 안에서 구현해도 될 것 같은데, 매개변수로 넣어준 이유는 테스트를 쉽게하기 위해서 인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 moveSelectedCar를 테스트하기 위해서 했습니다! |
||
} | ||
} | ||
|
||
public void moveSelectedCar(Car car, int randomValue) { | ||
if (isMoveCondition(randomValue)) { | ||
car.move(); | ||
} | ||
} | ||
|
||
public boolean isMoveCondition(int randomValue) { | ||
return randomValue > FORWARD_CONDITION; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
RacingGame that = (RacingGame) o; | ||
return Objects.equals(cars, that.cars); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(cars); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return cars.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package racing.dto; | ||
|
||
import java.util.Objects; | ||
|
||
public class CarDTO { | ||
|
||
private String name; | ||
private int distance; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. domain에서 가지고 있는 정보를 여기서도 가지고 있어야 하나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. domain이 get set을 최대한 하면 안되게 DTO를 사용했습니다. DTO는 Data Transfer Object로, 해당 도메인에서 필요한 데이터를 DTO로 담아서 마치 모델의 분신처럼 다른 레이어들과 데이터 주고 받기를 가능하게하는 역할을 합니다. |
||
public CarDTO(String carName, int distance) { | ||
this.name = carName; | ||
this.distance = distance; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getDistance() { | ||
return distance; | ||
} | ||
|
||
public void setDistance(int distance) { | ||
this.distance = distance; | ||
} | ||
|
||
public void drawCarTrack(String TRACK) { | ||
System.out.print(name); | ||
for (int i = 0; i < distance; i++) { | ||
System.out.print(TRACK); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
CarDTO carDTO = (CarDTO) o; | ||
return distance == carDTO.distance && | ||
Objects.equals(name, carDTO.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, distance); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CarDTO{" + | ||
"name='" + name + '\'' + | ||
", distance=" + distance + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package racing.dto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class RacingGameResult { | ||
|
||
private static final String TRACK = "-"; | ||
|
||
private List<CarDTO> cars; | ||
|
||
public RacingGameResult(List<CarDTO> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public void visualize() { | ||
for (CarDTO car : cars) { | ||
car.drawCarTrack(TRACK); | ||
} | ||
} | ||
|
||
public List<String> getWinnersName(){ | ||
List<CarDTO> sortedCar = sortCars(); | ||
List<CarDTO> winners = findWinners(sortedCar, getFirstCar(sortedCar)); | ||
|
||
return winners.stream() | ||
.map(CarDTO::getName) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<CarDTO> findWinners(List<CarDTO> sortedCar, int maxDistance) { | ||
List<CarDTO> winners = new ArrayList<>(); | ||
for (CarDTO carDTO : sortedCar) { | ||
if(!isMaxDistance(maxDistance, carDTO)) { | ||
break; | ||
} | ||
winners.add(carDTO); | ||
} | ||
return winners; | ||
} | ||
|
||
private boolean isMaxDistance(int maxDistance, CarDTO carDTO) { | ||
return carDTO.getDistance() == maxDistance; | ||
} | ||
|
||
private int getFirstCar(List<CarDTO> sortedCar) { | ||
return sortedCar.get(0).getDistance(); | ||
} | ||
|
||
private List<CarDTO> sortCars() { | ||
return cars.stream() | ||
.sorted(Comparator.comparing(CarDTO::getDistance) | ||
.reversed()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package racing.utils; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomValueGenerator { | ||
|
||
private static Random random = new Random(); | ||
|
||
public static int generate(int bound) { | ||
return random.nextInt(bound); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package racing.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static Scanner scanner = new Scanner(System.in); | ||
|
||
public static String readCarInfo() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름을 쉼표(,)를 기준으로 구분)."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static int readTryRound() { | ||
System.out.println("시도할 회수는 몇 회 인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package racing.view; | ||
|
||
import racing.dto.RacingGameResult; | ||
|
||
public class OutputView { | ||
|
||
public static void printRacingGameResult(RacingGameResult result) { | ||
System.out.println("실행 결과"); | ||
|
||
result.visualize(); | ||
} | ||
|
||
public static void printWinners(RacingGameResult result) { | ||
System.out.println(result.getWinnersName() + "가 최종 우승했습니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.CarDTO; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
public class CarTest { | ||
|
||
private Car car; | ||
|
||
@Before | ||
public void setUp() { | ||
car = new Car("javajigi"); | ||
} | ||
|
||
@Test | ||
public void moveTest() { | ||
car.move(); | ||
assertThat(car.getDistance()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void toCarDTOTest(){ | ||
car.move(); // distance + 1 | ||
assertThat(car.toCarDTO().toString()).isEqualTo(new CarDTO("javajigi", 1).toString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.RacingGameResult; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class RacingGameTest { | ||
|
||
private RacingGame racingGame; | ||
|
||
@Before | ||
public void setup() { | ||
racingGame = new RacingGame(); | ||
} | ||
|
||
@Test | ||
public void playGameTest() { | ||
racingGame.setCars("pobi"); | ||
RacingGameResult result = racingGame.play(5); | ||
|
||
assertThat(result).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void setCarsTest() { | ||
List<Car> cars = new ArrayList<>(); | ||
|
||
cars.add(new Car("hyukjin")); | ||
cars.add(new Car("hwajoong")); | ||
cars.add(new Car("changhwan")); | ||
|
||
racingGame.setCars("hyukjin,hwajoong,changhwan"); | ||
|
||
assertThat(racingGame.toString()).isEqualTo(cars.toString()); | ||
} | ||
|
||
@Test | ||
public void moveSelectedCarTest() { | ||
Car car = new Car("hyukjin"); | ||
racingGame.moveSelectedCar(car, 5); | ||
|
||
assertThat(car.getDistance()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void isMoveConditionTest(){ | ||
assertThat(racingGame.isMoveCondition(5)).isTrue(); | ||
} | ||
|
||
} |
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.
사용하는 곳이 있는 메소드인가요?
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.
없네요 지워야겠습니다 !