-
Notifications
You must be signed in to change notification settings - Fork 55
[이관우_BackEnd] 1주차 과제 제출합니다. #63
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
base: main
Are you sure you want to change the base?
Changes from 18 commits
69d278c
9229ec4
685fe5a
40da36d
61d357e
7bbb90e
f64079a
a7ff546
45ea126
37fa75d
3225b97
8b31444
50e6030
279705a
a2c3577
8d25826
d728091
7da49d6
84f9b6f
7313a94
7fba792
d7231b8
cb66dfc
014e73c
f1e0633
2ac8837
4a8e1f0
a96b0a3
b6e5763
903d754
12502f4
dcede87
b93d0f6
593e6cc
0b2d351
69b3c01
dc2bbdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ## 구현해야 하는 기능 목록 | ||
|
|
||
| ### 자동차 | ||
| - [x] 클래스 기본 틀 작성(변수, 생성자 등) | ||
| - [x] 랜덤 수(0~9) 반환 메소드 | ||
| - [x] 자동차를 한 칸 전진하는 메소드 | ||
|
|
||
| ### 입력 | ||
| - [x] 자동차 이름 & 입력 횟수 입력 | ||
| - [x] 입력받은 데이터 전달 | ||
|
|
||
| ### 출력 | ||
| - [x] 라운드 진행 시마다 경기 진행 상황 출력 | ||
| - [x] 경주 종료 후 최종 결과 및 우승자 출력 | ||
|
|
||
| ### 예외 처리 | ||
| - [x] 자동차 이름 관련 | ||
| - [x] 시도 횟수 관련 | ||
|
|
||
|
|
||
| ### 고민 사항 | ||
| - 자동차 정보를 입력받는 DTO를 어떻게 설계하는 것이 좋을까? | ||
| - 입력 텍스트 그대로(poni,woni,jun)를 DTO에 넣기 | ||
| - 이름을 분리하여 List<String>에 넣기 | ||
| - 이름을 분리한 것을 바탕으로 Car 객체들로 변환하고, List<Cars>에 넣기 | ||
| - Util 패키지 관련 | ||
| - 객체들을 new로 생성해서 사용하는 것이 좋은가? | ||
| - 아니면 객체를 별도로 생성하지 않고 객체 내부 메소드들을 모두 static 메소드로 만들어서 쓰는 것이 나은가? | ||
| - 시도 횟수를 입력할 때, 숫자가 아닌 다른 형태의 값을 입력했을 경우 | ||
| - CheckTryCount 라는 별도의 클래스에서 이를 검증할 방법은 없을까?(현재 코드는 sc.nextInt() 하는 시점에 try-catch를 통해 예외처리 하고 있음) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| package racingcar; | ||
|
|
||
| import racingcar.controller.RacingController; | ||
| import racingcar.controller.dto.RaceInfo; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| RacingController racingController = new RacingController(); | ||
|
|
||
| RaceInfo raceInfo = racingController.getInfosBeforeRaceStart(); | ||
| racingController.raceStart(raceInfo); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.controller.dto.RaceInfo; | ||
| import racingcar.util.CheckCarName; | ||
| import racingcar.util.CheckTryCount; | ||
| import racingcar.util.RaceManager; | ||
| import racingcar.util.StringUtils; | ||
| import racingcar.model.Car; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| public class RacingController { | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final RaceManager raceManager; | ||
|
|
||
|
|
||
|
||
| public RacingController() { | ||
| this.inputView = new InputView(); | ||
|
||
| this.outputView = new OutputView(); | ||
| this.raceManager = new RaceManager(); | ||
| } | ||
|
||
|
|
||
| public RaceInfo getInfosBeforeRaceStart() { | ||
| outputView.getCars(); | ||
| String racerStr = inputView.getStringInput(); | ||
| List<Car> cars = StringUtils.makeCarsUsingString(racerStr); | ||
| for(var c : cars) CheckCarName.checkName(c.getName()); | ||
|
||
|
|
||
| outputView.getTryCount(); | ||
| int tryCnt = inputView.getNumInput(); | ||
| CheckTryCount.isPositive(tryCnt); | ||
|
|
||
| return new RaceInfo(cars, tryCnt); | ||
| } | ||
|
|
||
| public void raceStart(RaceInfo raceInfo) { | ||
| List<Car> cars = raceInfo.cars(); | ||
|
|
||
| outputView.printRaceStart(); | ||
|
|
||
| for(int i = 0; i < raceInfo.gameCount(); ++i) { | ||
| raceManager.movingCars(cars); | ||
| outputView.printRaceStatus(cars); | ||
| } | ||
|
|
||
| finishRace(cars); | ||
| } | ||
|
|
||
| private void finishRace(List<Car> cars) { | ||
| List<String> winners = raceManager.findWinners(cars); | ||
| outputView.printRaceFinalStatus(winners); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package racingcar.controller.dto; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
|
||
| public record RaceInfo( | ||
| List<Car> cars, | ||
| int gameCount | ||
| ) { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package racingcar.model; | ||
|
|
||
| public class Car { | ||
| private static final int CAN_MOVE_STANDARD = 4; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 private는, 자동차가 움직이는 기준 값을 외부에 공개할 필요가 없기 때문에 private를 사용하였고, |
||
|
|
||
| int location; | ||
| String name; | ||
|
||
|
|
||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| this.location = 0; | ||
| } | ||
|
|
||
| public void move() { | ||
| if(isAbleToMove()) ++location; | ||
| } | ||
|
|
||
| private boolean isAbleToMove() { | ||
| return (int)(Math.random() * 10) >= CAN_MOVE_STANDARD; | ||
| } | ||
|
|
||
| public int getLocation() { | ||
| return location; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package racingcar.util; | ||
|
|
||
| public class CheckCarName { | ||
|
||
| private static final int NAME_MAX_LENGTH = 5; | ||
|
|
||
| public static void isNull(String name) { | ||
| if(name == null) | ||
| throw new IllegalArgumentException("이름에는 null 값이 들어갈 수 없습니다."); | ||
| } | ||
|
|
||
| public static void isBlank(String name) { | ||
| if(name.isBlank()) | ||
| throw new IllegalArgumentException("이름은 공백일 수 없습니다."); | ||
| } | ||
|
|
||
| public static void isMoreThanMaxLength(String name) { | ||
| if(name.length() > NAME_MAX_LENGTH) | ||
| throw new IllegalArgumentException("이름은 5자를 넘을 수 없습니다."); | ||
| } | ||
|
|
||
| public static void checkName(String name) { | ||
| isNull(name); | ||
| isBlank(name); | ||
| isMoreThanMaxLength(name); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package racingcar.util; | ||
|
|
||
| public class CheckTryCount { | ||
| public static void isPositive(int tryCnt) { | ||
| if(tryCnt < 0) | ||
| throw new IllegalArgumentException("시도 횟수는 자연수여야 합니다."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar.util; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class RaceManager { | ||
| public void movingCars(List<Car> cars) { | ||
| for(var c : cars) c.move(); | ||
| } | ||
|
|
||
| public List<String> findWinners(List<Car> cars) { | ||
| int maxLocation = 0; | ||
|
|
||
| for(var c : cars) { | ||
| if(maxLocation < c.getLocation()) maxLocation = c.getLocation(); | ||
| } | ||
|
|
||
| List<String> winners = new ArrayList<>(); | ||
| for(var c : cars) { | ||
| if(maxLocation == c.getLocation()) | ||
| winners.add(c.getName()); | ||
| } | ||
|
|
||
| return winners; | ||
| } | ||
|
Comment on lines
13
to
19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 부분도 마찬가지지만 반복문대신 스트림을 사용해보는건 어떠세요?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 부분은 확실히 스트림으로 해야 직관적이고 가독성도 좋을 것 같습니다 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package racingcar.util; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class StringUtils { | ||
| public static String[] splitByComma(String str) { | ||
| return str.split(","); | ||
| } | ||
|
|
||
| public static String NumToSticks(int count) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 메소드 명이 파스칼 케이스로 적용되어 있어서 카멜 케이스로 변경해주세요! |
||
| return "-".repeat(count); | ||
| } | ||
|
|
||
| public static List<Car> makeCarsUsingString(String str) { | ||
| String[] strs = StringUtils.splitByComma(str); | ||
| List<Car> cars = new ArrayList<>(); | ||
|
|
||
| for (var c : strs) { | ||
| cars.add(new Car(c)); | ||
| } | ||
|
|
||
| return cars; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package racingcar.view; | ||
|
|
||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private final Scanner sc = new Scanner(System.in); | ||
|
|
||
| public String getStringInput() { | ||
| return sc.nextLine(); | ||
| } | ||
|
|
||
| public int getNumInput() { | ||
| try { | ||
| return sc.nextInt(); | ||
| } | ||
| catch (InputMismatchException e) { | ||
| throw new IllegalArgumentException("숫자 외 다른 형태의 값을 입력할 수 없습니다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package racingcar.view; | ||
|
|
||
| import racingcar.util.StringUtils; | ||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
| public void getCars() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메소드 이름은 자동차 리스트를 가져오는? 의미로 볼 수 있을 것 같아요.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다 출력에 관련된 내용이니 get 보다는 print 가 어울릴 것 같습니다 수정하겠습니다! |
||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n"); | ||
| } | ||
|
|
||
| public void getTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?\n"); | ||
| } | ||
|
|
||
| public void printRaceStart() { | ||
| System.out.println("\n실행 결과"); | ||
| } | ||
|
|
||
| public void printRaceStatus(List<Car> cars) { | ||
| for(var c : cars) { | ||
| System.out.println(c.getName() + " : " + StringUtils.NumToSticks(c.getLocation())); | ||
| } | ||
| System.out.print('\n'); | ||
| } | ||
|
|
||
| public void printRaceFinalStatus(List<String> winners) { | ||
| System.out.print("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기에 불필요한 공백이 있는 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
11번째 줄 공백을 말하신 건가요? 수정하겠습니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
13, 14번째 줄을 얘기한거에요!
보통 두 줄 사이의 간격은 한 칸으로 유지해주세요!