-
Notifications
You must be signed in to change notification settings - Fork 55
[김민석_Backend] 2주차 과제 제출합니다. #50
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 1 commit
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 |
|---|---|---|
| @@ -1,7 +1,40 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class Application { | ||
| @DisplayName("실행") | ||
| @Test | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input=Console.readLine(); | ||
|
||
| RacingCar racingCar; | ||
| int n; | ||
| try{ | ||
| racingCar=new RacingCar(input); | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| input=Console.readLine(); | ||
| n= getTurn(input); | ||
|
||
| }catch(IllegalArgumentException e) { | ||
| System.out.println("잘못된 값이 입력되어서 종료합니다."); | ||
|
||
| return; | ||
| } | ||
| racingCar.runRace(n); | ||
| racingCar.printResult(); | ||
| return; | ||
| } | ||
| public static int getTurn(String input) { | ||
| int n; | ||
| try{ | ||
| n = Integer.parseInt(input); | ||
| }catch(NumberFormatException e) { | ||
| System.out.println("숫자가 아닌 다른 값이 입력되었습니다."); | ||
| throw new IllegalArgumentException("에러 : 잘못된 횟수 입력"); | ||
| } | ||
| return n; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| String name; | ||
| int distance; | ||
|
|
||
| public Car(String name) { | ||
| name=name.replaceAll(" ",""); | ||
|
||
| if(name.length()>5||name.length()<1) { | ||
| System.out.println("자동차 이름은 1글자 이상, 5글자 이하여야 합니다."); | ||
| throw new IllegalArgumentException("에러 : 자동차 이름 글자수 초과"); | ||
| } | ||
| this.name=name; | ||
| this.distance=0; | ||
| } | ||
| public void run() { | ||
| if(Randoms.pickNumberInRange(0,9)>=4){ | ||
| this.distance++; | ||
| } | ||
| } | ||
| public void print() { | ||
| System.out.print(this.name+" : "); | ||
| for(int i=0;i<distance;i++) { | ||
| System.out.print("-"); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package racingcar; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| public class RacingCar { | ||
|
||
| Car[] car; | ||
|
|
||
| public RacingCar(String input) { | ||
| String carName[] = input.split(","); | ||
| this.car=new Car[carName.length]; | ||
|
|
||
| for(int i=0;i< carName.length;i++){ | ||
| this.car[i]=new Car(carName[i]); | ||
| //assertThat(car[i].name.length()).isBetween(1,5); | ||
| // 차 이름 글자수 확인 | ||
| } | ||
| } | ||
| public void runRace(int n) { | ||
| for(int i=0;i<n;i++){ | ||
| this.runCars(); | ||
| } | ||
| } | ||
| public void runCars(){ | ||
| for(int i=0;i<this.car.length;i++) { | ||
| car[i].run(); | ||
| car[i].print(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| public void printResult() { | ||
| System.out.print("최종 우승자 : "); | ||
| System.out.println(this.findWinner()); | ||
| System.out.println("경기가 종료되었습니다."); | ||
| } | ||
| public String findWinner() { | ||
| int winnerDistance=0; | ||
| for(int i=0;i<car.length;i++) { | ||
| if(winnerDistance<car[i].distance) { | ||
| winnerDistance=car[i].distance; | ||
| } | ||
| } | ||
| String winnerList=""; | ||
| for(int i=0;i<car.length;i++) { | ||
| if (winnerDistance == car[i].distance) { | ||
| winnerList=winnerList.concat(car[i].name); | ||
| winnerList=winnerList.concat(", "); | ||
|
||
| } | ||
| } | ||
| return winnerList; | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,11 @@ | |
| import camp.nextstep.edu.missionutils.test.NsTest; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| 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; | ||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| class ApplicationTest extends NsTest { | ||
| private static final int MOVING_FORWARD = 4; | ||
|
|
@@ -26,8 +27,8 @@ class ApplicationTest extends NsTest { | |
| @Test | ||
| void 이름에_대한_예외_처리() { | ||
| assertSimpleTest(() -> | ||
| assertThatThrownBy(() -> runException("pobi,javaji", "1")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| assertThatCode(() -> runException("pobi,javaji", "1")) | ||
| .doesNotThrowAnyException() | ||
|
||
| ); | ||
| } | ||
|
|
||
|
|
||
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.
이 프로그램 진입점은 테스트를 위한 코드가 아닌데 Test를 붙인 이유가 있을까요?
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.
테스트 관련해서 이것저것 알아보다 추가하고 방치한 것 같네요.