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

Step3 #5723

Open
wants to merge 5 commits into
base: seulpi
Choose a base branch
from
Open

Step3 #5723

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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
---
## 기능 구현
* 사용자 입력 (몇 대의 자동차로 몇번을 이동할것인지 정의)
* 입력값에 대한 유효성 검증
* 전진 or 멈춤
* 조건 : 0-9 사이 랜덤값을 통해 4 이상일 경우 전진
* 자동차 상태 화면에 출력
32 changes: 32 additions & 0 deletions src/main/java/study/racing/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package study.racing;

public class Car {

private int carCount;
private int tryCount;

public Car() {

}

public Car(int carCount, int tryCount) {
this.carCount = carCount;
this.tryCount = tryCount;
}

public int getCarCount() {
return carCount;
}

public void setCarCount(int carCount) {
this.carCount = carCount;
}

public int getTryCount() {
return tryCount;
}

public void setTryCount(int tryCount) {
this.tryCount = tryCount;
}
}
32 changes: 32 additions & 0 deletions src/main/java/study/racing/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package study.racing;

import study.validation.InputValidation;

import java.util.Scanner;

public class InputView {
Copy link

Choose a reason for hiding this comment

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

해당 클래스를 보면 별도의 상태를 가지지 않는 클래스로 보여요. 유틸리티 혹은 싱글톤 패턴을 적용해보면 어떨까요?

Copy link
Author

@seulpi seulpi Oct 3, 2024

Choose a reason for hiding this comment

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

@catsbi 코멘트 주신 부분이 이해가 안되서 다시 여쭤봅니다..!
별도의 상태를 가지지 않는 클래스란 말이 활용이 안되고 있는 클래스란 말이신걸까요?

Copy link

Choose a reason for hiding this comment

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

좀 더 직접적으로 설명을 드리면,
상태를 가지지 않고 있다는 말은 별도의 인스턴스 변수를 가지지 않는 클래스다. 라고 생각하시면 됩니다!
이런 객체들은 보통 재사용성이 높은 객체들인데 매 번 새롭게 인스턴스를 생성해서 사용할 필요가 없죠.
그렇기에 싱글톤 패턴을 적용해 재사용성을 높힐 수 있습니다.


public Car input() {
Scanner scanner = new Scanner(System.in);
Car car = new Car();

while (true) {
Copy link

Choose a reason for hiding this comment

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

특정한 중단포인트가 없는 무한루프는 권장드리지 않습니다. StackOverFlow 에러를 발생기키기 쉬운 코드이기도 하구요.

특정 중단 변수를 지정하거나 하는게 나을 것 같습니다.

또한 해당 반복문은 매 번 Scanner를 새로 호출하고 있는데, 그럴 필요가 있는 작업일까요? 재사용성이 없는 객체가 아닌걸로 보입니다!


System.out.println(RacingMessage.CAR_COUNT.msg());
String carCount = scanner.nextLine();

System.out.println(RacingMessage.TRY_COUNT.msg());
String tryCount = scanner.nextLine();

boolean isInputMatchForCar = InputValidation.racingInputValidation(carCount);
boolean isInputMatchForTry = InputValidation.racingInputValidation(tryCount);

if(isInputMatchForCar && isInputMatchForTry) {
car.setCarCount(Integer.parseInt(carCount));
car.setTryCount(Integer.parseInt(tryCount));
break;
}
}
return car;
}
}
29 changes: 29 additions & 0 deletions src/main/java/study/racing/RacingCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package study.racing;

import study.utils.NumberUtils;
import java.util.Map;

public class RacingCar {

public static int goAndStop() {

int randomNumber = NumberUtils.randomNumberUnder10();
int forWord = 0;
if(randomNumber >=4 ) {
forWord++;
}

return forWord;
}

public static Map<Integer, Integer> addCountForDash(int car, int moveCount, Map<Integer, Integer> map) {

if(!map.containsKey(car)) {
map.put(car, 0);

}
map.put(car, map.get(car) + moveCount);

return map;
}
}
18 changes: 18 additions & 0 deletions src/main/java/study/racing/RacingMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package study.racing;

public enum RacingMessage {
CAR_COUNT("자동차 대수는 몇 대 인가요?")
, TRY_COUNT("시도할 회수는 몇 회 인가요?")
, TRY_AGAIN("다시 입력해주세요.");

private final String msg;

RacingMessage(String msg) {
this.msg = msg;
}

public String msg() {
return msg;
}

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultView {

final static String FORWARD_DASH = "-";

public static void resultView() {
InputView inputView = new InputView();
Car car = inputView.input();

Map<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < car.getTryCount(); i++) {
for(int j = 0; j < car.getCarCount(); j++) {
int moveCount = RacingCar.goAndStop();
map = RacingCar.addCountForDash(j, moveCount, map);
printView(map);
}
System.out.println();
}
}

private static void printView(Map<Integer, Integer> map) {
for(int k = 0; k < map.size(); k++) {
if(map.get(k) > 0) {
System.out.print(FORWARD_DASH);
}
}
System.out.println();
}

}
11 changes: 11 additions & 0 deletions src/main/java/study/utils/NumberUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package study.utils;

import java.util.Random;

public class NumberUtils {

public static int randomNumberUnder10() {
Random random = new Random();
return random.nextInt(10);
}
}
26 changes: 26 additions & 0 deletions src/main/java/study/validation/InputValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package study.validation;

import study.racing.RacingMessage;

public class InputValidation {

public static boolean racingInputValidation(String inputCount) {

boolean isInputMatchType = true;

if(inputCount.isBlank()) {
return isInputMatchType;
}

try {
if(Integer.parseInt(inputCount) < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.out.println(RacingMessage.TRY_AGAIN.msg());
isInputMatchType = false;
}

return isInputMatchType;
}
}
33 changes: 33 additions & 0 deletions src/test/java/stydy/racing/RacingCarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package stydy.racing;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import study.racing.RacingCar;
import study.racing.ResultView;
import study.utils.NumberUtils;

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

public class RacingCarTest {

@DisplayName("랜덤값 < 10")
@RepeatedTest(10)
public void repeatedTest() {
int randomNumber = NumberUtils.randomNumberUnder10();
assertThat(randomNumber).isBetween(0, 9);
}

@Test
@DisplayName("전진 멈춤 테스트 : 전진 -> 1 , 멈춤 -> 0")
public void goAndStop() {
int forwardNumber = RacingCar.goAndStop();
assertThat(forwardNumber).isBetween(0,1);
}

@Test
@DisplayName("자동차 경주가 잘 출력되는지 확인")
public void racing() {
ResultView.resultView();
}
}