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

Bowook #2409

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Bowook #2409

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
4 changes: 4 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package racingcar;

import racingcar.controller.RacingController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
RacingController racingController = new RacingController();
racingController.run();
}
}
33 changes: 33 additions & 0 deletions src/main/java/racingcar/controller/RacingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package racingcar.controller;

import racingcar.domain.Cars;
import racingcar.service.MoveForward;
import racingcar.view.InputView;
import racingcar.view.OutputView;

public class RacingController {

private final InputView inputView;
private final OutputView outputView;
private final MoveForward moveForward;
private final Cars cars;

public RacingController() {
this.inputView = new InputView();
this.outputView = new OutputView();
this.cars = new Cars(inputView.readRacingCarName());
this.moveForward = new MoveForward(inputView.readTrialCount());
}

public void run() {
outputView.executionResultMessage();
int count = 0;

while(count < moveForward.getTrialCount()) {
count++;
cars.moveCars();
outputView.printMoveResults(cars.getCars());
}
outputView.finalWinnerMessage(cars.winnerJudge());
}
}
29 changes: 29 additions & 0 deletions src/main/java/racingcar/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package racingcar.domain;

public class Car {
//무엇을 할 것인가, 핵심 기능 로직
private final String name;
private int position;

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

public void move() {
position++;
}

public int getPosition() {
return position;
}
public String getName() {
return name;
}


@Override
public String toString() {
return name + " : " + "-".repeat(position);
}
}
54 changes: 54 additions & 0 deletions src/main/java/racingcar/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package racingcar.domain;

import racingcar.service.MoveForward;

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

public class Cars {

private final List<Car> cars;

public Cars(List<String> carNames) {
Validate.validator(carNames);
this.cars = carNames.stream().map(Car::new).collect(Collectors.toList());
}

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

public boolean stopAndGo() {
int number = MoveForward.randomMoveForwards();
return number >= 4;
}

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

public String winnerJudge() {
StringBuilder sb = new StringBuilder();
int maxPosition = 0;
for(Car car : cars) {
if(car.getPosition() > maxPosition) {
maxPosition = car.getPosition();
}
}

for(Car car : cars) {
if(car.getPosition() == maxPosition) {
sb.append(car.getName()).append(", ");
}
}

if (!sb.isEmpty()) {
sb.setLength(sb.length() - 2);
}
return sb.toString();
}
}
15 changes: 15 additions & 0 deletions src/main/java/racingcar/domain/Validate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package racingcar.domain;

import java.util.List;

public class Validate {

public static List<String> validator(List<String> carNames) {
for(String carName : carNames) {
if(carName.length() > 5) {
throw new IllegalArgumentException();
}
}
return carNames;
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/service/MoveForward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar.service;

import camp.nextstep.edu.missionutils.Randoms;

public class MoveForward {
//어떻게 할 것인가, 상황별 로직

private final int trialCount;

public MoveForward(int trialCount) {
this.trialCount = trialCount;
}

public int getTrialCount() {
return trialCount;
}

public static int randomMoveForwards() {
return Randoms.pickNumberInRange(0,9);
}


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

import camp.nextstep.edu.missionutils.Console;

import java.util.List;

public class InputView {

public List<String> readRacingCarName() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
return List.of(Console.readLine().split(","));
}

public int readTrialCount() {
System.out.println("시도할 회수는 몇회인가요?");
return Integer.parseInt(Console.readLine());
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar.view;

import racingcar.domain.Car;

import java.util.List;

public class OutputView {

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

public void finalWinnerMessage(String cars) {
System.out.print("최종 우승자 : " + cars);
}

public void printMoveResults(List<Car> cars) {
for (Car car : cars) {
System.out.println(car);
}
System.out.println();
}
}