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

Step4 #5832

Open
wants to merge 8 commits into
base: sweet-info
Choose a base branch
from
Open

Step4 #5832

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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

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

import racing.entity.Car;
import racing.service.Cars;
import racing.view.InputView;
import racing.view.ResultView;

import java.util.List;

public class CarRacingController {

public static void main(String[] args) {
InputView inputView = new InputView();
List<Car> cars = inputView.inputCar();
int rounds = inputView.inputNumber();

Cars racingService = new Cars(cars);
ResultView resultView = new ResultView();

for (int i = 0; i < rounds; i++) {
racingService.moveCars();
resultView.printResult(racingService.getCars());
}
resultView.findWinners(racingService.findWinners());
}
}


40 changes: 40 additions & 0 deletions src/main/java/racing/entity/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package racing.entity;

import java.util.Random;

public class Car {

private final String name;
protected int position;
private final Random random = new Random();

private static final int MOVE_THRESHOLD = 4;
private static final int RANDOM_RANGE = 10;

public Car(String name) {
this.name = name;
this.position = 0;
}

public String getName() {
return name;
}

public int getPosition() {
return position;
}

public void move() {
if (canMove()) {
position += 1;
}
}

protected boolean canMove() {
int randomValue = random.nextInt(RANDOM_RANGE);
return randomValue >= MOVE_THRESHOLD;
}
}



36 changes: 36 additions & 0 deletions src/main/java/racing/service/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racing.service;

import racing.entity.Car;
import java.util.List;
import java.util.stream.Collectors;

public class Cars {

private final List<Car> cars;

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

public void moveCars() {
for (Car car : cars) {
car.move();
}
}

public List<Car> getCars() {
return cars;
}

public List<Car> findWinners() {
int maxPosition = cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(0);

return cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.collect(Collectors.toList());
}
}

27 changes: 27 additions & 0 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package racing.view;

import racing.entity.Car;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class InputView {

private final Scanner scanner = new Scanner(System.in);

public List<Car> inputCar() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String[] names = scanner.nextLine().split(",");
List<Car> cars = new ArrayList<>();
for (String name : names) {
cars.add(new Car(name.trim()));
}
return cars;
}

public int inputNumber() {
System.out.println("시도할 횟수를 입력하세요 : ");
return scanner.nextInt();
}
}

34 changes: 34 additions & 0 deletions src/main/java/racing/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racing.view;

import racing.entity.Car;

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

public class ResultView {
public void printResult(List<Car> cars) {
StringBuilder result = new StringBuilder();
cars.forEach(car -> result.append(buildCarResult(car)).append("\n"));
System.out.println(result.toString());
}

private String buildCarResult(Car car) {
return car.getName() + ": " + carPosition(car.getPosition());
}

private String carPosition(int position) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < position; i++) {
stringBuilder.append("-");
}
return stringBuilder.toString();
}

public void findWinners(List<Car> winners) {
String winnerNames = winners.stream()
.map(Car::getName)
.collect(Collectors.joining(", "));
System.out.println("우승자는 " + winnerNames + " 입니다.");
}
}

41 changes: 41 additions & 0 deletions src/test/java/racing/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racing;

import org.junit.jupiter.api.Test;
import racing.entity.Car;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CarTest {

@Test
void carMoves() {
Car car = new Car("Car");
car.move();
assertEquals(1, car.getPosition());
}

@Test
void carDoesNotMove() {
Car car = new Car("CarzCar") {
@Override
public void move() {
if (!canMove()) {
position = 0;
}
}
};
car.move();
assertEquals(0, car.getPosition());
}

@Test
void carNameIsSetCorrectly() {
Car car = new Car("Car");
assertEquals("Car", car.getName());
}

@Test
void carInitialPositionIsZero() {
Car car = new Car("Car");
assertEquals(0, car.getPosition());
}
}
35 changes: 35 additions & 0 deletions src/test/java/racing/CarsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racing;

import org.junit.jupiter.api.Test;
import racing.entity.Car;
import racing.service.Cars;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CarsTest {

@Test
void testMoveCars() {
Car car1 = new Car("Car1");
Car car2 = new Car("Car2");
Cars racingService = new Cars(Arrays.asList(car1, car2));

int initialPositionCar1 = car1.getPosition();
int initialPositionCar2 = car2.getPosition();

racingService.moveCars();

assertEquals(calculateExpectedPosition(initialPositionCar1, car1.getPosition()), car1.getPosition());
assertEquals(calculateExpectedPosition(initialPositionCar2, car2.getPosition()), car2.getPosition());
}


private int calculateExpectedPosition(int initialPosition, int currentPosition) {
if (currentPosition > initialPosition) {
return initialPosition + 1;
}
return initialPosition;
}

}