diff --git a/README.md b/README.md index c550c4c2a09..9af77111b34 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 28ff446a215..ffed3a254e9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/src/main/java/.gitkeep b/src/main/java/racingcar/.gitkeep similarity index 100% rename from src/main/java/.gitkeep rename to src/main/java/racingcar/.gitkeep diff --git a/src/main/java/racingcar/RacingCarApplication.java b/src/main/java/racingcar/RacingCarApplication.java new file mode 100644 index 00000000000..016ec06809c --- /dev/null +++ b/src/main/java/racingcar/RacingCarApplication.java @@ -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(); + + } + +} diff --git a/src/main/java/racingcar/controller/RacingCarGame.java b/src/main/java/racingcar/controller/RacingCarGame.java new file mode 100644 index 00000000000..550c631a81d --- /dev/null +++ b/src/main/java/racingcar/controller/RacingCarGame.java @@ -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 carsName = carsService.getCarsName(); + List cars = carsName.stream() + .map(Car::new) + .collect(Collectors.toList()); + int raceTryCount = raceService.getRaceTryCount(); + raceService.raceStart(cars, raceTryCount); + raceService.determineWinner(cars); + + } +} diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java new file mode 100644 index 00000000000..d5872098b66 --- /dev/null +++ b/src/main/java/racingcar/domain/Car.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/racingcar/service/cars/CarsService.java b/src/main/java/racingcar/service/cars/CarsService.java new file mode 100644 index 00000000000..23a8451f69e --- /dev/null +++ b/src/main/java/racingcar/service/cars/CarsService.java @@ -0,0 +1,7 @@ +package racingcar.service.cars; + +import java.util.List; + +public interface CarsService { + public List getCarsName(); +} diff --git a/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java new file mode 100644 index 00000000000..4525b96fcdd --- /dev/null +++ b/src/main/java/racingcar/service/cars/impl/CarsServiceImpl.java @@ -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 getCarsName(){ + List 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 splitCarsNmae(String carsName){ + if(carsName.isEmpty()) throw new IllegalArgumentException("이름을 입력하세요"); + + return Arrays.stream(carsName.split(",")) + .collect(Collectors.toList()); + } + private void checkCarsName(List carsNameList){ + if (carsNameList.stream().anyMatch(name -> name.length() > LIMIT_NAME_LENGTH)) { + throw new IllegalArgumentException("이름은 5자를 넘길 수 없습니다."); + } + } + + +} diff --git a/src/main/java/racingcar/service/race/RaceService.java b/src/main/java/racingcar/service/race/RaceService.java new file mode 100644 index 00000000000..52d6444eac6 --- /dev/null +++ b/src/main/java/racingcar/service/race/RaceService.java @@ -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 cars, int tryCount); + public void determineWinner(List cars); +} diff --git a/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java new file mode 100644 index 00000000000..89850aee5a6 --- /dev/null +++ b/src/main/java/racingcar/service/race/impl/RaceServiceImpl.java @@ -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 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 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); + } + + + + +} diff --git a/src/main/java/racingcar/util/StringParser.java b/src/main/java/racingcar/util/StringParser.java new file mode 100644 index 00000000000..9bd3c1bdfce --- /dev/null +++ b/src/main/java/racingcar/util/StringParser.java @@ -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 parse(String text) { + return parse(text, SPLIT_PATTERN); + } + + public static List parse(String text, String pattern) { + Objects.requireNonNull(pattern, SPLIT_PATTERN); + return Stream.of(text.split(pattern)) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java new file mode 100644 index 00000000000..ff05abbb2f8 --- /dev/null +++ b/src/main/java/racingcar/view/InputView.java @@ -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(); + } + + +}