Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
53bf7fc
feat : 이름 VO 테스트 및 구현
sdrs6921 Sep 2, 2021
4ec32f2
feat : 위치 VO 테스트 및 구현
sdrs6921 Sep 2, 2021
84505b7
feat : Car 객체 테스트 및 구현
sdrs6921 Sep 2, 2021
8686549
feat : Movement 전략 테스트 및 구현
sdrs6921 Sep 16, 2021
259d5c4
feat : Car 테스트 및 구현
sdrs6921 Sep 16, 2021
2cdce39
feat : Name equals, hashCode 테스트 및 구현
sdrs6921 Sep 17, 2021
417ad45
feat : equals, hashCode, isLargerThan 테스트 및 기능 구현
sdrs6921 Sep 17, 2021
1a2eccf
feat : Car equals(), hashCode() 테스트 및 기능 구현
sdrs6921 Oct 4, 2021
484aea8
chore : java, Junit, assertj 버전 변경
sdrs6921 Oct 4, 2021
63ed9d3
refactor : Position 불필요한 메서드 삭제
sdrs6921 Oct 4, 2021
79d6bd9
feat : Cars 테스트 및 기능 구현
sdrs6921 Oct 4, 2021
a93edc3
feat : 시도 횟수 VO 테스트 및 기능 구현
sdrs6921 Oct 4, 2021
d03d3ae
feat : InputView, InputConsoleView 기능 구현
sdrs6921 Oct 4, 2021
218ce76
refactor : Cars 불변 객체로 변경
sdrs6921 Oct 4, 2021
6435528
refactor : 테스트 코드 given, when, then 구조로 변경
sdrs6921 Oct 12, 2021
f51293e
refactor : Dto 객체 생성 적용 및 기능 테스트 및 구현
sdrs6921 Oct 12, 2021
e8432ac
feat : Names 일급 컬렉션 테스트 및 기능 구현
sdrs6921 Oct 12, 2021
4af857c
test : given, when, then 명시적으로 추가
sdrs6921 Oct 12, 2021
bb66fba
feat : 시도 횟수 차감 기능 구현
sdrs6921 Nov 4, 2021
42d8126
refactor : 메서드 이름 변경
sdrs6921 Nov 4, 2021
e7be73d
feat : 라운드 테스트작성 및 기능 구현
sdrs6921 Nov 4, 2021
9f1a305
feat : 이름 일급 컬렉션 반환 메서드 테스트 및 기능 구현
sdrs6921 Nov 4, 2021
aa7a36d
refactor : 부 생성자 추가
sdrs6921 Nov 4, 2021
4d348c6
refactor : 이름 일급컬렉션으로 생성할 수 있도록 변경
sdrs6921 Nov 4, 2021
b4bfd50
refactor : Dto 에서 엔티티를 반환하도록 변경
sdrs6921 Nov 4, 2021
a1e0b40
feat : Dto 및 Output View 기능 구현
sdrs6921 Nov 4, 2021
60bc1a6
feat : Round 테스트 수정
sdrs6921 Nov 4, 2021
caf496a
feat : GameController 기능 구현
sdrs6921 Nov 4, 2021
3a149be
refactor : 패키지 구조 수정
sdrs6921 Nov 4, 2021
6a19f77
refactor : 메서드 이름 수정
sdrs6921 Nov 4, 2021
cdf7a23
refactor : 메인 함수 기능 구현
sdrs6921 Nov 4, 2021
fcb9e4f
refactor : 이름 중복 로직 기능 구현
sdrs6921 Nov 4, 2021
1b9f35b
refactor : 이름 공백 제거 기능 추가
sdrs6921 Nov 4, 2021
30e257d
style : 코드 통일성 추가
sdrs6921 Nov 4, 2021
135c27b
style : final 키워드 추가
sdrs6921 Nov 9, 2021
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
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ apply plugin: 'eclipse'

group = 'camp.nextstep'
version = '1.0.0'
sourceCompatibility = '1.8'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:5.7.2"
testImplementation "org.assertj:assertj-core:3.19.0"
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
testImplementation 'org.assertj:assertj-core:3.21.0'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

버전 변경한 이유가 무엇인가요?

}

test {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/racing/RacingCarApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racing;

import racing.controller.GameController;
import racing.strategy.MovementStrategy;
import racing.strategy.RandomMovementStrategyStrategy;
import racing.view.InputConsoleView;
import racing.view.InputView;
import racing.view.OutputConsoleView;
import racing.view.OutputView;

import java.util.Scanner;

public class RacingCarApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
InputView inputView = new InputConsoleView(scanner);
OutputView outputView = new OutputConsoleView();
MovementStrategy movementStrategy = new RandomMovementStrategyStrategy();
GameController gameController = new GameController(inputView, outputView, movementStrategy);
gameController.run();
scanner.close();
}
}
62 changes: 62 additions & 0 deletions src/main/java/racing/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package racing.controller;

import racing.domain.Car;
import racing.domain.Cars;
import racing.domain.Names;
import racing.domain.Round;
import racing.domain.vo.NumberOfAttempt;
import racing.dto.InputCarNameDto;
import racing.dto.InputNumberOfAttemptDto;
import racing.dto.OutputRoundResultDto;
import racing.dto.OutputWinnerDto;
import racing.strategy.MovementStrategy;
import racing.view.InputView;
import racing.view.OutputView;

import java.util.List;

public class GameController {
private final InputView inputView;
private final OutputView outputView;
private final MovementStrategy movementStrategy;

public GameController(final InputView inputView, final OutputView outputView, final MovementStrategy movementStrategy) {
this.inputView = inputView;
this.outputView = outputView;
this.movementStrategy = movementStrategy;
}

public void run() {
Round round = initRound();
outputView.printRoundResultMessage();
round = race(round);
printWinner(round);
}

private Round race(final Round initialRound) {
Round round = initialRound;

while (round.isPlaying()) {
round = round.play(movementStrategy);
Cars nextRoundCars = round.cars();
OutputRoundResultDto outputRoundResultDto = new OutputRoundResultDto(nextRoundCars);
outputView.printRoundResult(outputRoundResultDto);
}

return round;
}

private Round initRound() {
InputCarNameDto inputCarNameDto = inputView.inputCarNames();
InputNumberOfAttemptDto inputNumberOfAttemptDto = inputView.inputNumberOfAttempt();
Names names = inputCarNameDto.toNames();
NumberOfAttempt numberOfAttempt = inputNumberOfAttemptDto.toNumberOfAttempt();
return new Round(names, numberOfAttempt);
}

private void printWinner(final Round round) {
List<Car> winners = round.winners();
OutputWinnerDto outputWinnerDto = new OutputWinnerDto(winners);
outputView.printWinner(outputWinnerDto);
}
}
62 changes: 62 additions & 0 deletions src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package racing.domain;

import racing.domain.vo.Name;
import racing.domain.vo.Position;
import racing.strategy.MovementStrategy;

import java.util.Objects;

public class Car {

private static final int INITIAL_LOCATION = 0;
private static final int DISTANCE_MOVED_AT_ONCE = 1;

private final Name name;
private final Position position;

public Car(final Name name, final Position position) {
this.name = name;
this.position = position;
}

public Car(final String name, final int position) {
this(new Name(name), new Position(position));
}

public Car(final String name) {
this(name, INITIAL_LOCATION);
}

public Car(final Name name) {
this(name, new Position(INITIAL_LOCATION));
}

public String name() {
return name.value();
}

public int position() {
return position.value();
}

public Car move(MovementStrategy movementStrategy) {
if (movementStrategy.canMove()) {
return new Car(name, position.increaseBy(DISTANCE_MOVED_AT_ONCE));
}

return this;
}

@Override
public boolean equals(final Object other) {
if (this == other) return true;
if (!(other instanceof Car)) return false;
final Car car = (Car) other;
return name.equals(car.name);
}

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

import racing.strategy.MovementStrategy;

import java.util.List;
import java.util.stream.Collectors;

public class Cars {

private final List<Car> elements;

public Cars(final List<Car> elements) {
this.elements = elements;
}

public Cars(final Names carNames) {
this(convertFrom(carNames));
}

private static List<Car> convertFrom(final Names carNames) {
return carNames.elements()
.stream()
.map(Car::new)
.collect(Collectors.toUnmodifiableList());

}

public List<Car> elements() {
return elements;
}

public Cars driveAll(final MovementStrategy movementStrategy) {
List<Car> droveCar = elements.stream()
.map(car -> car.move(movementStrategy))
.collect(Collectors.toUnmodifiableList());
return new Cars(droveCar);
}

public List<Car> findMaxPositionCars() {
int max = findMaxPosition();
return elements.stream()
.filter(car -> car.position() == max)
.collect(Collectors.toUnmodifiableList());
}

private int findMaxPosition() {
return elements.stream()
.map(Car::position)
.max(Integer::compareTo)
.orElseThrow(() -> new IllegalArgumentException("최대값을 찾을 수 없습니다"));
}
}
18 changes: 18 additions & 0 deletions src/main/java/racing/domain/Names.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package racing.domain;

import racing.domain.vo.Name;

import java.util.List;

public class Names {

private final List<Name> elements;

public Names(final List<Name> elements) {
this.elements = elements;
}

public List<Name> elements() {
return elements;
}
}
39 changes: 39 additions & 0 deletions src/main/java/racing/domain/Round.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package racing.domain;

import racing.domain.vo.NumberOfAttempt;
import racing.strategy.MovementStrategy;

import java.util.List;

public class Round {

private final Cars cars;
private final NumberOfAttempt numberOfAttempt;

public Round(final Cars cars, final NumberOfAttempt numberOfAttempt) {
this.cars = cars;
this.numberOfAttempt = numberOfAttempt;
}

public Round(final Names names, final NumberOfAttempt numberOfAttempt) {
this(new Cars(names), numberOfAttempt);
}

public Round play(final MovementStrategy movementStrategy) {
NumberOfAttempt numberOfAttemptAfterTrying = numberOfAttempt.decrease();
Cars carsAfterDriveAll = this.cars.driveAll(movementStrategy);
return new Round(carsAfterDriveAll, numberOfAttemptAfterTrying);
}

public Cars cars() {
return cars;
}

public List<Car> winners() {
return cars.findMaxPositionCars();
}

public boolean isPlaying() {
return !numberOfAttempt.isOver();
}
}
39 changes: 39 additions & 0 deletions src/main/java/racing/domain/vo/Name.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package racing.domain.vo;

import java.util.Objects;

public class Name {

private static final int MIN_NAME_LENGTH = 1;
private static final int MAX_NAME_LENGTH = 5;

private final String value;

public Name(final String value) {
validate(value);
this.value = value;
}

private void validate(final String value) {
if (value.length() < MIN_NAME_LENGTH || value.length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("이름이 길이는 1이상 5이하여야 합니다");
}
}

public String value() {
return value;
}

@Override
public boolean equals(final Object other) {
if (this == other) return true;
if (!(other instanceof Name)) return false;
final Name name = (Name) other;
return value.equals(name.value);
}

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

import java.util.Objects;

public class NumberOfAttempt {

private static final int ZERO = 0;
private static final int SUBTRACTED_VALUE = 1;

private final int value;

public NumberOfAttempt(final int value) {
if (value < ZERO) {
throw new IllegalArgumentException("시도 횟수는 0 이상의 수 입니다");
}

this.value = value;
}

public int value() {
return value;
}

public NumberOfAttempt decrease() {
return new NumberOfAttempt(value - SUBTRACTED_VALUE);
}

public boolean isOver() {
return value == ZERO;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof NumberOfAttempt)) return false;
final NumberOfAttempt that = (NumberOfAttempt) o;
return value == that.value;
}

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

import java.util.Objects;

public class Position {

private final int value;

public Position(final int value) {
this.value = value;
}

public int value() {
return value;
}

public Position increaseBy(final int distanceMovedAtOnce) {
return new Position(value + distanceMovedAtOnce);
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof Position)) return false;
final Position position = (Position) o;
return value == position.value;
}

@Override
public int hashCode() {
return Objects.hash(value);
}
}
Loading