-
Notifications
You must be signed in to change notification settings - Fork 55
[박태진_BackEnd] 1주차 과제 제출합니다. #47
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 all commits
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,29 @@ | ||
| ## ✅ 핵심 기능 목록 (체크리스트) | ||
|
|
||
| - [ ] 자동차 이름 입력 검증 (`feat: 자동차 이름 입력 검증 추가`) | ||
| - 쉼표 구분 파싱 | ||
| - 1~5자 길이 체크 | ||
|
|
||
| - [ ] 시도 횟수 입력 검증 (`feat: 시도 횟수 유효성 체크 구현`) | ||
| - 숫자 형식 검증 | ||
| - 양수 여부 확인 | ||
|
|
||
| - [ ] 자동차 이동 로직 (`feat: 자동차 이동 조건 구현`) | ||
| - 0-9 랜덤 값 생성 | ||
| - 4 이상 시 위치 증가 | ||
|
|
||
| - [ ] 경주 진행 관리 (`feat: 라운드별 경주 실행 기능`) | ||
| - 지정 횟수만큼 반복 | ||
| - 매 라운드 위치 업데이트 | ||
|
|
||
| - [ ] 결과 출력 형식 (`feat: 실행 결과 출력 포맷팅`) | ||
| - `이름 : ----` 형태 표시 | ||
| - 라운드 구분선 추가 | ||
|
|
||
| - [ ] 우승자 판별 (`feat: 최종 우승자 계산 로직`) | ||
| - 최대 위치 계산 | ||
| - 공동 승자 처리 | ||
|
|
||
| - [ ] 예외 처리 통합 (`feat: 입력 예외 핸들링 추가`) | ||
| - `IllegalArgumentException` throw | ||
| - 오류 메시지 출력 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,42 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| List<Car> cars = createCars(); | ||
| int attempts = getAttempts(); | ||
| Racing racing = new Racing(cars, attempts); | ||
| racing.start(); | ||
| printWinners(racing.getWinners()); // try-catch 제거 | ||
|
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. 처음엔 try-catch로 예외 처리를 했었는데, 테스트 코드 실행 후 오류가 발생하여 보았더니... 예외를 직접 던져야 한다는걸 알게되어서...😭 try-catch는 지우고 주석으로 잠시 기록해둔 후, 커밋할 때 잊어버려 지우지 못한 것 같습니다... |
||
| } | ||
|
|
||
| private static List<Car> createCars() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String[] names = Console.readLine().split(","); | ||
| return Arrays.stream(names) | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
|
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. 'collect(Collectors.toList())'와 그냥 'toList()'는 무슨 차이가 있을까요?
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. toList()는 수정 불가능한 리스트를 반환하고, collect(Collectors.toList())는 수정 가능한 리스트를 반환합니다! |
||
| } | ||
|
|
||
| private static int getAttempts() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| String input = Console.readLine(); | ||
| try { | ||
| int attempts = Integer.parseInt(input); | ||
| if (attempts <= 0) { | ||
| throw new IllegalArgumentException("[ERROR] 시도 횟수는 1 이상이어야 합니다"); | ||
| } | ||
| return attempts; | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다"); | ||
| } | ||
| } | ||
|
|
||
| private static void printWinners(List<String> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position; | ||
|
|
||
| public Car(String name) { | ||
| validateName(name); // 생성자에서 이름 검증 | ||
| this.name = name; | ||
| this.position = 0; | ||
| } | ||
|
|
||
| private void validateName(String name) { | ||
| if (name == null || name.isEmpty() || name.length() > 5) { | ||
| throw new IllegalArgumentException("[ERROR] 자동차 이름은 1~5자만 가능합니다"); // 예외 메시지 일치 | ||
| } | ||
| } | ||
|
|
||
| public void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
| position++; | ||
| } | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Racing { | ||
|
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 final List<Car> cars; | ||
| private final int attempts; | ||
|
|
||
| public Racing(List<Car> cars, int attempts) { | ||
| this.cars = cars; | ||
| this.attempts = attempts; | ||
| } | ||
|
|
||
| public void start() { | ||
| System.out.println("\n실행 결과"); | ||
| for (int i = 0; i < attempts; i++) { | ||
| raceRound(); | ||
| printPositions(); | ||
| } | ||
| } | ||
|
|
||
| private void raceRound() { | ||
| for (Car car : cars) { | ||
| car.move(); | ||
| } | ||
| } | ||
|
|
||
| private void printPositions() { | ||
| for (Car car : cars) { | ||
| System.out.printf("%s : %s%n", car.getName(), "-".repeat(car.getPosition())); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| int maxPosition = getMaxPosition(); | ||
| return cars.stream() | ||
| .filter(car -> car.getPosition() == maxPosition) | ||
| .map(Car::getName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private int getMaxPosition() { | ||
| return cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElse(0); | ||
| } | ||
| } | ||
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.
체크리스트 좋네요. 완성되었으니 체크 표시 하면 좋을 것 같습니다.