Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

3번째 과제 PR #29

Open
wants to merge 15 commits into
base: k0000k
Choose a base branch
from
37 changes: 0 additions & 37 deletions src/main/java/InputView.java

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/java/SimpleRacing.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/racinggame/AlwaysGo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racinggame;

public class AlwaysGo implements MoveCase {
@Override
public boolean isMovable() {
return true;
}
}
11 changes: 11 additions & 0 deletions src/main/java/racinggame/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package racinggame;

public class Controller {
public static void main(String[] args) {
InputView input = new InputView();
RacingStatus racingStatus = input.inputCar();
RunRacing runRacing = new RunRacing();
RandomGo randomGo = new RandomGo();
runRacing.runRace(racingStatus, randomGo);
}
}
15 changes: 15 additions & 0 deletions src/main/java/racinggame/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package racinggame;

import java.util.Scanner;

public class InputView {
public RacingStatus inputCar() {
Scanner scanner = new Scanner(System.in);
System.out.println("자동차 대수는 몇 대 인가요?");
int carCount = scanner.nextInt();
System.out.println("시도 할 횟수는 몇 회 인가요?");
int racingCount = scanner.nextInt();

return new RacingStatus(carCount, racingCount);
k0000k marked this conversation as resolved.
Show resolved Hide resolved
}
}
5 changes: 5 additions & 0 deletions src/main/java/racinggame/MoveCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racinggame;

interface MoveCase {
boolean isMovable();
}
32 changes: 32 additions & 0 deletions src/main/java/racinggame/RacingStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package racinggame;

import java.util.Arrays;

public class RacingStatus {
private final int carCount;
private final int racingCount;
private final String[] racingCars;
k0000k marked this conversation as resolved.
Show resolved Hide resolved

public RacingStatus(int carCount, int racingCount) {
this.carCount = carCount;
this.racingCount = racingCount;
racingCars = new String[carCount];
Arrays.fill(racingCars, "-");
k0000k marked this conversation as resolved.
Show resolved Hide resolved
}

public int getRacingCount() {
return racingCount;
}

public int getCarCount() {
return carCount;
}

public String[] getRacingCars() {
return racingCars;
}

public void go(int num) {
racingCars[num] += "-";
}
}
16 changes: 16 additions & 0 deletions src/main/java/racinggame/RandomGo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package racinggame;

import java.util.Random;

public class RandomGo implements MoveCase {
private final Random random;
k0000k marked this conversation as resolved.
Show resolved Hide resolved

RandomGo() {
this.random = new Random();
}

@Override
public boolean isMovable() {
return (random.nextInt(10) >= 4);
}
}
10 changes: 10 additions & 0 deletions src/main/java/racinggame/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package racinggame;

public class ResultView {
public void printRace(RacingStatus car) {
for (String string : car.getRacingCars()) {
System.out.println(string);
}
System.out.println();
}
}
20 changes: 20 additions & 0 deletions src/main/java/racinggame/RunRacing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package racinggame;

public class RunRacing {

public void runRace(RacingStatus car, MoveCase moveCase) {
ResultView resultView = new ResultView();
for (int i = 0; i < car.getRacingCount(); ++i) {
resultView.printRace(car);
decideStop(car, moveCase);
}
}

private void decideStop(RacingStatus car, MoveCase moveCase) {
for (int j = 0; j < car.getCarCount(); j++) {
if (moveCase.isMovable()) {
car.go(j);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package stringcalculator;

import java.util.function.BiFunction;

public enum Operator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package stringcalculator;

import stringcalculator.Operator;

public class StringCalculator {
private final String[] splitContents;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package stringcalculator;

import stringcalculator.StringCalculator;
import stringcalculator.StringInput;

public class StringCalculatorMain {
public static void main(String[] args) {
StringInput stringInput = new StringInput();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package stringcalculator;

import java.util.Scanner;

public class StringInput {
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/RunRacingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import racinggame.AlwaysGo;
import racinggame.RunRacing;
import racinggame.RacingStatus;

import static org.assertj.core.api.Assertions.assertThat;

class RunRacingTest {

private final RacingStatus testRacing1 = new RacingStatus(3, 5);
private final RacingStatus testRacing2 = new RacingStatus(5, 3);

@Test
@DisplayName("레이싱 시작 상태 테스트")
public void randomNumberTest() {
String[] testCarList1 = new String[]{"-", "-", "-"};
String[] testCarList2 = new String[]{"-", "-", "-", "-", "-"};

assertThat(testRacing1.getRacingCars()).isEqualTo(testCarList1);
assertThat(testRacing2.getRacingCars()).isEqualTo(testCarList2);
}

@Test
@DisplayName("이동 횟수 테스트")
public void moveTest() {

String[] testCarList1 = new String[]{"------", "------", "------"};
String[] testCarList2 = new String[]{"----", "----", "----", "----", "----"};

RunRacing race = new RunRacing();
AlwaysGo alwaysGo = new AlwaysGo();

race.runRace(testRacing1, alwaysGo);
race.runRace(testRacing2, alwaysGo);

assertThat(testRacing1.getRacingCars()).isEqualTo(testCarList1);
assertThat(testRacing2.getRacingCars()).isEqualTo(testCarList2);
}

}
1 change: 1 addition & 0 deletions src/test/java/StringCalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import stringcalculator.StringCalculator;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down