Skip to content

Commit

Permalink
feat(step3): 자동차 경주
Browse files Browse the repository at this point in the history
  • Loading branch information
chyjeon committed Mar 9, 2024
1 parent e1bb022 commit e9dfb5e
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/racingcar/IOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package racingcar;

import java.io.ByteArrayInputStream;

public abstract class IOTest {

protected void systemIn(byte[] inputBytes) {
System.setIn(new ByteArrayInputStream(inputBytes));
}
}
19 changes: 19 additions & 0 deletions src/main/java/racingcar/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racingcar;

import java.util.Scanner;

import static racingcar.RacingCar.toInts;

public class InputView {
public static int numberOfCar(){
System.out.println("자동차 대수는 몇 대 인가요?\n");
return toInts((new Scanner(System.in)).nextLine());

}

public static int numberOfRound(){
System.out.println("시도할 회수는 몇 회 인가요?\n");
return toInts((new Scanner(System.in)).nextLine());
}

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

public class OutputView {
public void racingResult(RacingCar racingCar){
System.out.println(racingCar.resultOfRacing());
}

public void resultTitle(){
System.out.println("실행 결과");
}

public void oneRoundEnd(){
System.out.println();
}
}
49 changes: 49 additions & 0 deletions src/main/java/racingcar/RacingCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package racingcar;

import java.util.Random;

public class RacingCar {

private int position =0;
private String HYPHEN = "-";
private int TEN = 10;

private static final int THRESHOLD = 4;

public RacingCar() {
}

public void moveRacingCar(int randomNum){
if (randomNum >= THRESHOLD){
this.position ++;
}
}

public int genRandomNum(){
return new Random().nextInt(TEN);
}

public String resultOfRacing(){
String result = "";
for (int i=0;i<this.position;i++){
result += HYPHEN;
}
return result;
}

public static int toInts(String values){
return checkPositive(values);
}

private static int checkPositive(String value){
int result = Integer.parseInt(value);
if (result <= 0){
throw new IllegalArgumentException();
}
return result;
}

public int getPositionOfCar(){
return this.position;
}
}
15 changes: 15 additions & 0 deletions src/main/java/racingcar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package racingcar;

public class RacingGame {
public static void main(String[] args) {
RacingRecord racingRecord = new RacingRecord();
InputView inputView = new InputView();
int numberOfCar = inputView.numberOfCar();
int numberOfRound = inputView.numberOfRound();

racingRecord.carRegister(numberOfCar);
racingRecord.runRace(numberOfRound);

}

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

import java.util.ArrayList;
import java.util.List;

public class RacingRecord {
private final List<RacingCar> records = new ArrayList<>();

public void carRegister(int carNum){
for (int i=0;i<carNum; i++) {
records.add(new RacingCar());
}
}

public void runOneRound(){

for (RacingCar racingCar: records){
racingCar.moveRacingCar(racingCar.genRandomNum());
OutputView outputView = new OutputView();
outputView.racingResult(racingCar);
}
}

public void runRace(int rounds){
OutputView outputView = new OutputView();
outputView.resultTitle();
for (int round =0; round<rounds; round++){
runOneRound();
outputView.oneRoundEnd();
}
}


}
10 changes: 10 additions & 0 deletions src/main/java/racingcar/check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* 자동차 대수와 시도 횟수는 다음의 경우 RuntimeException 예외가 발생해야 한다.
* 0이하의 정수
* 숫자가 아닌 값을 입력
* 초기 position은 0으로 설정한다.
* 자동차 대수 만큼 객체 생성?
* 랜덤값이 4-9인 경우에 전진한다.(position 1 증가)
* 랜덤값이 0-3인 경우 position은 변화하지 않는다.
* 결과 출력시 position 만큼 하이픈 "-" 출력


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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;


import static racingcar.InputView.numberOfCar;
import static org.assertj.core.api.Assertions.assertThat;
import static racingcar.InputView.numberOfRound;


public class RacingTest extends IOTest{

@DisplayName("초기 생성 위치는 0")
@Test
void new_car(){
RacingCar car = new RacingCar();
assertThat(car.getPositionOfCar()).isEqualTo(0);
}

@DisplayName("입력이 4이상일 경우 차량 이동")
@ParameterizedTest
@CsvSource(value = {"1:0", "2:0", "3:0", "4:1", "5:1"}, delimiter = ':')
void car_move(int randomNum, int expected){
RacingCar car = new RacingCar();
car.moveRacingCar(randomNum);
assertThat(car.getPositionOfCar()).isEqualTo(expected);
}


@DisplayName("자동차 대수 음수나 숫자가 아니면 에러")
@ParameterizedTest
@CsvSource(value = {"-1: -1", "#: #", "0:0"}, delimiter = ':')
void car_check(String numOfCar, String numOfRound) {
systemIn(numOfCar.getBytes());
Assertions.assertThrows(IllegalArgumentException.class, () -> {
numberOfCar();
});
}

@DisplayName("게임 횟수 음수나 숫자가 아니면 에러")
@ParameterizedTest
@CsvSource(value = {"-1: -1", "1#2#3: 1#2#3", "0:0"}, delimiter = ':')
void round_check(String numOfCar, String numOfRound) {
systemIn(numOfRound.getBytes());
Assertions.assertThrows(IllegalArgumentException.class, () -> {
numberOfRound();
});

}
}

0 comments on commit e9dfb5e

Please sign in to comment.