-
Notifications
You must be signed in to change notification settings - Fork 55
[윤해인_BackEnd] 1주차 과제 제출합니다. #64
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
92d27bb
63cbcca
0378efe
10ef05f
0b831d6
bd64f69
8f1c5fb
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 |
|---|---|---|
|
|
@@ -22,4 +22,5 @@ java { | |
|
|
||
| test { | ||
| useJUnitPlatform() | ||
| jvmArgs '-Xshare:off' // JVM 아규먼트 설정 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 구현해야하는 기능 목록 | ||
|
|
||
| 1. 자동차 이름 입력 (쉼표로 구분) | ||
| 1-1. 이름 나누기 | ||
| 1-2. 각 이름 검증 (5글자 이하) | ||
| 2. 시도할 횟수 입력 | ||
| 2-1. 횟수 검증 (10회 이하) | ||
|
|
||
| 3. 각 라운드 진행 | ||
| 3-1. 각 개체 별 랜덤 넘버 생성 | ||
| 3-2. 라운드 진행 | ||
| 3-3. 각 라운드 별 결과 작성 (이름 : 진행 상황) | ||
|
|
||
| 4. 최종 우승자 출력 | ||
| 4-1. 각 객체 별 최종 위치 확인 | ||
| 4-2. 우승자 출력 (2명 이상일 수 있음) | ||
|
|
||
| 유의사항 | ||
| - 사용자 입력값은 camp.nextstep.edu.missionutils.Console의 readLine() 활용 | ||
| - Random 값 추출은 camp.nextstep.edu.missionutils.Randoms의 pickNumberInRange() 활용 | ||
| - indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 | ||
| - 함수(또는 메서드)가 한 가지 일만 하도록 최대한 작게 구현 | ||
| - JUnit 5와 AssertJ를 이용하여 본인이 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인할 것! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,67 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| //1. 이름 입력 받기 | ||
| System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분합니다.)"); | ||
| String participations = Console.readLine(); | ||
|
|
||
| //1-1. 이름 나누기 | ||
| String[] carNames = participations.split(","); //String을 콤마를 기준으로 나누어 리스트로 변환 | ||
|
|
||
| //객체 배열 생성 | ||
| Car[] cars = new Car[carNames.length]; | ||
|
|
||
| for (int i = 0; i < carNames.length; i++) { | ||
| cars[i] = new Car(carNames[i]); | ||
| } | ||
| //공백 제거 체크용 | ||
| for (Car car : cars) { System.out.println("참가자 : " + car.getName()); } | ||
|
|
||
| // 2. 시도 횟수 입력 | ||
| System.out.println("시도할 회수는 몇 회입니까?"); | ||
| String tryCounts = Console.readLine(); //readLine은 String으로 받아야 함. (int 불가x) | ||
|
|
||
| int Counts = Application.validateTryCount(tryCounts); | ||
| //System.out.println("[게임 실행 결과]"); | ||
|
|
||
| //3. 게임 실행 | ||
| Game game = new Game(); | ||
| for(int i = 0; i < Counts; i++){ | ||
| System.out.println(""); | ||
| // System.out.println( "["+ (i+1) +"번째 시도]"); | ||
|
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. 사소하긴 한데, 주석 형태가 떨어져있거나, //1. || // 1. 처럼 일치하지 않아서 가독성이 떨어져 한 번 정해보면 좋을 거 같아요.
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. 신경써서 통일해보겠습니다😊 |
||
| //각 라운드 경주 진행 | ||
| game.runRound(cars); | ||
|
|
||
| //3-1. 라운드 별 결과 출력 | ||
| game.printRound(cars); | ||
| } | ||
| //4. 게임 끝! 결과 출력 | ||
| //4-1. | ||
| game.checkWinner(cars); | ||
| //4-2. | ||
| game.printWinner(cars); | ||
| } | ||
|
|
||
| //2-1. 시도 횟수 검증(임의 규칙 생성) | ||
| public static int validateTryCount(String tryCounts){ | ||
| // 게임 횟수는 1번 이상일 것 | ||
| int num ; | ||
| try { | ||
| num = Integer.parseInt(tryCounts); | ||
| } catch(NumberFormatException e) { | ||
| throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다."); | ||
| } | ||
| if (num < 1){ | ||
|
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. 요거 이렇게 되면 1회 이상이 아니게 되지 않나요👀!!
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. 앗 1 미만은 입력하지 못하게 하려던건데 if (num == 0)이 나을까요?? |
||
| throw new IllegalArgumentException("게임 횟수는 최소 1번이어야 합니다."); | ||
| } | ||
| // 게임 횟수는 10번 이하일 것 | ||
| if (num > 10){ | ||
| throw new IllegalArgumentException("게임 횟수는 10회 이하여야 합니다."); | ||
| } | ||
| return num ; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package racingcar; | ||
| public class Car { | ||
|
|
||
| private String name; | ||
| private int position = 0; //초기화 | ||
|
|
||
| // 생성자 | ||
| public Car(String name) { | ||
| this.name = validateName(name); | ||
| } | ||
|
|
||
| private String validateName(String name) { //필드는 private 권장? | ||
| // TODO: 1. null 체크 | ||
| if (name == null) throw new IllegalArgumentException("이름을 입력해주세요."); | ||
|
|
||
| // TODO: 2. 이름이 비었는지 체크(공백만으로 이루어졌는지) | ||
| String s = name.strip(); | ||
| if (s.isEmpty()) throw new IllegalArgumentException("이름은 공백만으로 이루어질 수 없습니다."); | ||
|
|
||
| // TODO: 길이 > 5 체크 | ||
| if (s.length() > 5) throw new IllegalArgumentException("이름은 5글자 이하여야 합니다"); | ||
| return s; | ||
| } | ||
|
|
||
| public String getName() { return name; } | ||
| public int getPosition() { return position; } | ||
|
|
||
| public StringBuilder printPosition() { //String으로는 문자열 추가가 어려움. buffer또는 builder를 사용해야함. | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("-".repeat(Math.max(0, position))); //for문 대신 string.repeat로 변경 | ||
| return sb; | ||
| } | ||
| // public String printPosition() { //String으로는 문자열 추가가 어려움. buffer또는 builder를 사용해야함. | ||
| // return "-".repeat(position); | ||
| // } | ||
|
|
||
| //4 이상이면 전진 | ||
| void moveIf(int num) { if (num >= 4) { position++; } } | ||
|
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. 길이 5나, 숫자 4 같은 경우는 상단에 상수로 빼둬도 좋을 거 같아요.
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,64 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Game { //게임 진행 클래스 | ||
| //3-1. 랜덤 넘버 생성 | ||
| public int pickRandomNumber(){ | ||
| return Randoms.pickNumberInRange(0,9); | ||
| } | ||
|
|
||
| //3-2. 라운드 진행 | ||
| public void runRound(Car[] cars){ | ||
| //모든 차에 대해 랜덤 수를 뽑고, 전진 여부를 판단하기 | ||
| for (Car car : cars) { | ||
| int n = pickRandomNumber(); | ||
| car.moveIf(n); | ||
| } | ||
| } | ||
|
|
||
| //3-3. 라운드 결과 출력(라운드마다 실행!) | ||
| public void printRound(Car[] cars){ | ||
| for (Car car : cars) { | ||
| // 단순히 getPosition으로 숫자만 출력하는게 아님! 현재 위치를 '-' 이용해서 출력하기 | ||
| System.out.println(car.getName() + " : " + car.printPosition()); | ||
| } | ||
| } | ||
| //4. 게임 끝! | ||
|
|
||
| //4-1. 마지막 위치 확인 | ||
| // - 객체 배열 순회하면서 최종 위치 + 우승자 갱신 | ||
| public StringBuilder checkWinner(Car[] cars){ | ||
| StringBuilder winners = new StringBuilder(); //winner는 2명 이상일 수 있으므로 stringbuilder로 생성 | ||
| int maxPosition = 0; | ||
| //<1> 1번 순회하기 (max값과 같으면 string에 추가, 더 크면 초기화) | ||
| for (Car car : cars) { | ||
| //[1] max보다 크면 StringBuilder 초기화 후 우승자 후보 정보 입력 | ||
| if (car.getPosition() > maxPosition) { | ||
| //StringBuilder 초기화 | ||
| winners.delete(0, winners.length()); | ||
| winners.append(car.getName()); | ||
| maxPosition = car.getPosition(); | ||
| } | ||
| //[2] max와 동일하면 초기화는 하지 않고 우승자 후보 추가 입력 | ||
| else if (car.getPosition() == maxPosition) { | ||
| if (winners.length() > 0) { winners.append(", ");} | ||
| winners.append(car.getName()); | ||
| } | ||
| } | ||
| // //<2> 2번 순회하기 (maxPosition 찾고, max와 동일한 position을 가진 carName 수집) | ||
| // for (Car car : cars) { | ||
| // if (car.getPosition() >= maxPosition) { maxPosition = car.getPosition(); } | ||
| // } | ||
| // for (Car car : cars) { | ||
| // if (car.getPosition() == maxPosition) { winners.append(car.getName()).append(", "); } | ||
| // } | ||
| return winners; | ||
| } | ||
| public void printWinner(Car[] cars){ | ||
| // System.out.println("<최종 우승자> :"+ checkWinner(cars)); | ||
| System.out.println("최종 우승자 : " + checkWinner(cars)); | ||
|
|
||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package racingcar; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest; | ||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
|
|
||
| class CarTest { | ||
| //1. 자동차 이름 입력 (쉼표로 구분) | ||
| @Test | ||
| void 자동차의_이름은_5글자_넘으면_예외발생() { | ||
| // 이름이 6자 이상인 자동차를 만들면 예외 발생 | ||
| assertThatThrownBy(() -> new Car("abcdef")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void 이름이_비었으면_예외발생() { | ||
| assertThatThrownBy(() -> new Car(" ")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void 이름이_null이면_예외발생() { | ||
| assertThatThrownBy(() -> new Car(null)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| // //2번 - 시도할 횟수 입력 | ||
| // @Test | ||
| // void 게임횟수는_1미만이면_예외발생() { | ||
| // assertThatThrownBy(() -> Application.validateTryCount("0") | ||
| // .isInstanceOf(IllegalArgumentException.class)); | ||
| // } | ||
| //3. 전진 규칙 | ||
| @Test | ||
| void 자동차는_숫자가_4이상일때_전진() { //테스트는 경계값을 기준으로 설정 | ||
| Car car = new Car("test1"); | ||
| car.moveIf(4); | ||
| assertThat(car.getPosition()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void 자동차는_숫자가_3이하면_정지() { | ||
| Car car = new Car("test1"); | ||
| car.moveIf(3); // 3 이하 → 멈춤 | ||
| assertThat(car.getPosition()).isEqualTo(0); // 자동차의 위치가 0인지 검증 | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package racingcar; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TryCountValidateTest { | ||
| //2번 - 시도할 횟수 입력 테스트 | ||
| @Test | ||
| void 게임횟수가_0이하면_예외처리(){ | ||
| assertThatThrownBy(() -> Application.validateTryCount("0")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void 게임횟수가_10초과면_예외처리(){ | ||
| assertThatThrownBy(() -> Application.validateTryCount("11")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| } |
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.
carNames.length를 받는 변수를 하나 만들면 편하게 사용할 수 있을 거 같아요
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.
내장 메소드를 계속 호출하는 것보단 그게 낫겠네요!!! 수정하겠습니다!