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 구현 #6069

Merged
merged 15 commits into from
Mar 25, 2025
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)

---

## "3단계 - 자동차 경주" 기능 목록 및 요구사항

### 기능 요구사항
- [ ] 자동차 대수를 입력받는다.
- [ ] 이동할 횟수를 입력받는다.
- [ ] 랜덤 값을 생성하여 전진 (값이 4 이상) 혹은 정지 (값이 3 이하) 할지 결정한다.
- [ ] 매 이동 시 마다 자동차 상태를 출력한다.
46 changes: 46 additions & 0 deletions src/main/java/controller/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controller;

import domain.Car;
import view.InputView;
import view.ResultView;

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

public class RacingGame {
private final List<Car> cars = new ArrayList<>();
private final int numberOfRounds;

public RacingGame(int numberOfCars, int numberOfRounds) {
this.numberOfRounds = numberOfRounds;
generateCars(numberOfCars);
}

private void generateCars(int numberOfCars) {
for (int i = 0; i < numberOfCars; i++) {
cars.add(new Car());
}
}

private void runOneRound() {
for (Car car : cars) {
car.run();
}
}

private void run() {
ResultView.printResult("실행 결과");
for (int i = 0; i < numberOfRounds; i++) {
runOneRound();
ResultView.printResult("");
}
}

public static void main(String[] args) {
int numberOfCars = InputView.getNumberOfCars();
int numberOfRounds = InputView.getNumberOfRounds();

RacingGame game = new RacingGame(numberOfCars, numberOfRounds);
game.run();
}
}
29 changes: 29 additions & 0 deletions src/main/java/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package domain;

import java.util.Random;
import view.ResultView;

public class Car {
private int position = 0;
private static final Random random = new Random();

private int getRandomValue() {
return random.nextInt(10);
}

private void move(int randomValue) {
if (randomValue >= 4) {
position++;
}
}

Choose a reason for hiding this comment

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

랜덤값으로 인하여, 테스트가 용이하지 않죠.
이런 경우 interface를 통해 확장성을 고려해볼수 있습니다.

https://tecoble.techcourse.co.kr/post/2020-05-17-appropriate_method_for_test_by_interface/

위 글을 참고해서 한번 적용해보시면 테스트 코드 작성이 어렵지 않지 않을까 생각이들어요~

Copy link
Author

@bo-10000 bo-10000 Mar 21, 2025

Choose a reason for hiding this comment

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

붙혀주신 링크 확인하였습니다! 해당 링크에 있는 방법대로 구현하기 전에 질문드리고 싶은것이 있는데요,

NumberGenerator 관련하여, MovableNumberGenerator의 경우 test만을 위해 새롭게 구현하는 class로 보여지는데, (제가 TDD에 대해 잘 몰라서) 이렇게 test만을 위해 class를 추가하는 것이 전체 코드의 복잡도를 오히려 올리는 것이 아닌지 하는 생각이 들어 의견 여쭤보고 싶습니다!

Choose a reason for hiding this comment

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

테스트 폴더에 넣는다면 응집도 관점에서 맞지 않을까요~?

Copy link
Author

Choose a reason for hiding this comment

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

interface 방식으로 refactor 하고, 테스트코드 추가하였습니다~!


private void viewCurrentPosition() {
ResultView.printCurrentPosition(position);
}

public void run() {
int randomValue = getRandomValue();
move(randomValue);
viewCurrentPosition();
}
}
20 changes: 20 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package view;

import java.util.Scanner;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);

public static int getNumberOfCars() {
return getInput("자동차 대수는 몇 대 인가요?");
}

public static int getNumberOfRounds() {
return getInput("시도할 회수는 몇 회 인가요?");
}

private static int getInput(String message) {
System.out.println(message);
return scanner.nextInt();
}
}
11 changes: 11 additions & 0 deletions src/main/java/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package view;

public class ResultView {
public static void printCurrentPosition(int position) {
printResult("-".repeat(position));
}

public static void printResult(String message) {
System.out.println(message);
}
}