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

[최형규] 프리코스 미션 제출합니다. #5548

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@
* 코드 리뷰 피드백에 대한 개선 작업을 하고 다시 PUSH한다.
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.


## 기능
* 1. n대 자동차 이름 입력
* 2. 입력값 체크(5자 넘어가면 에러 메세지 출력)
* 3. 쉼표 기준으로 스플릿하여 이름 저장
* 4. 몇 번 시도할지 입력(0 또는 양수가 아닌 경우 에러 메세지 출력): m번
* 5. f함수: 0~9사이 생성 랜덤 함수
* 6. f함수 m번 실행 후 값 저장
* 7. 각 함수 실행 결과 출력
* 8. 우승자 선출
## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
File renamed without changes.
19 changes: 19 additions & 0 deletions src/main/java/racingcar/RacingCarApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racingcar;

import racingcar.controller.RacingCarGame;
import racingcar.service.cars.CarsService;
import racingcar.service.cars.impl.CarsServiceImpl;
import racingcar.service.race.RaceService;
import racingcar.service.race.impl.RaceServiceImpl;
import racingcar.view.InputView;

public class RacingCarApplication {
public static void main(String[] args){
CarsService carsService = new CarsServiceImpl(new InputView());
RaceService raceService = new RaceServiceImpl(new InputView());
RacingCarGame racingCarGame = new RacingCarGame(carsService, raceService);
racingCarGame.run();

}

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

import racingcar.domain.Car;
import racingcar.service.cars.CarsService;
import racingcar.service.race.RaceService;

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

public class RacingCarGame {

private final CarsService carsService;
private final RaceService raceService;

public RacingCarGame(CarsService carsService, RaceService raceService) {
this.carsService = carsService;
this.raceService = raceService;
}

public void run() {

List<String> carsName = carsService.getCarsName();
List<Car> cars = carsName.stream()
.map(Car::new)
.collect(Collectors.toList());
int raceTryCount = raceService.getRaceTryCount();
raceService.raceStart(cars, raceTryCount);
raceService.determineWinner(cars);

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

public class Car {
private int position;

public int getPosition() { return position; }

public String getName() { return name; }

private final String name;
public Car(String name) {
this(0, name);
}
public Car(int position, String name) {
this.position = position;
this.name = name;
}
public void move(int moveValue){
position += moveValue;
}


@Override
public String toString() {
return "Car{" +
"position=" + position +
", name='" + name + '\'' +
'}';
}
}
7 changes: 7 additions & 0 deletions src/main/java/racingcar/service/cars/CarsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package racingcar.service.cars;

import java.util.List;

public interface CarsService {
public List<String> getCarsName();
}
47 changes: 47 additions & 0 deletions src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package racingcar.service.cars.impl;

import racingcar.service.cars.CarsService;
import racingcar.view.InputView;

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

public class CarsServiceImpl implements CarsService {
private final InputView inputView;
private static final int LIMIT_NAME_LENGTH = 5;

public CarsServiceImpl(InputView inputView) {
this.inputView = inputView;
}

public List<String> getCarsName(){
List<String> carsNameList;
while(true){
try{
String carsName = inputView.inputString();
carsNameList = splitCarsNmae(carsName);
checkCarsName(carsNameList);
break;
}catch (IllegalArgumentException e){
System.out.println("[ERROR]" + e);
}
}
return carsNameList;
}

private List<String> splitCarsNmae(String carsName){
if(carsName.isEmpty()) throw new IllegalArgumentException("이름을 입력하세요");

return Arrays.stream(carsName.split(","))
.collect(Collectors.toList());
}
private void checkCarsName(List<String> carsNameList){
if (carsNameList.stream().anyMatch(name -> name.length() > LIMIT_NAME_LENGTH)) {
throw new IllegalArgumentException("이름은 5자를 넘길 수 없습니다.");
}
}


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

import racingcar.domain.Car;

import java.util.List;

public interface RaceService {
public int getRaceTryCount();
public void raceStart(List<Car> cars, int tryCount);
public void determineWinner(List<Car> cars);
}
72 changes: 72 additions & 0 deletions src/main/java/racingcar/service/race/impl/RaceServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package racingcar.service.race.impl;

import racingcar.domain.Car;
import racingcar.service.race.RaceService;
import racingcar.view.InputView;

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

public class RaceServiceImpl implements RaceService {
private final InputView inputView;
private static final int RANDOM_BOUNDARY = 10;
private static final int MOVE_CONDITION = 4;
private static final int GO = 1;
private static final int STOP = 0;
public RaceServiceImpl(InputView inputView) {
this.inputView = inputView;
}
private int randomValue(){
Random random = new Random();
return random.nextInt(RANDOM_BOUNDARY);
}
private int moveNumber(int randomValue){
if(randomValue >= MOVE_CONDITION ) return GO;
return STOP;
}
public void printCar(Car car){
System.out.println(car.getName() + " : " + car.getPosition());
}
public int getRaceTryCount(){
int tryCount;
while(true){
try{
tryCount = inputView.inputInt();
if(tryCount <= 0) throw new IllegalArgumentException("비정상적인 숫자입니다.");
return tryCount;
}catch (IllegalArgumentException e){
System.out.println("[ERROR]" + e);
}
}
}
public void raceStart(List<Car> cars, int tryCount){
for(int i = 0; i < tryCount; ++i){
for(Car car: cars){
int randomValue = this.randomValue();
car.move(moveNumber(randomValue));
printCar(car);
}
System.out.println();
}
}

public void determineWinner(List<Car> cars){

int maxPosition = cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(Integer.MIN_VALUE);

String winners = cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.map(Car::getName)
.collect(Collectors.joining(", "));

System.out.println("최종 우승자 : " + winners);
}




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

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

public class StringParser {
private static final String SPLIT_PATTERN = ",";

private StringParser() {}

public static List<String> parse(String text) {
return parse(text, SPLIT_PATTERN);
}

public static List<String> parse(String text, String pattern) {
Objects.requireNonNull(pattern, SPLIT_PATTERN);
return Stream.of(text.split(pattern))
.collect(Collectors.toList());
}
}
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 java.util.Scanner;

public class InputView {
public String inputString() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
Scanner scanner = new Scanner(System.in);
return scanner.next();
}
public int inputInt() {
System.out.println("시도할 회수는 몇회인가요?");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}


}