Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/racingcar/Application.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 프로그램 진입점은 테스트를 위한 코드가 아닌데 Test를 붙인 이유가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 관련해서 이것저것 알아보다 추가하고 방치한 것 같네요.

public static void main(String[] args) {
// TODO: 프로그램 구현
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input=Console.readLine();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가독성을 생각해보면 좋을거 같아요
자바 코딩 컨벤션에 대해 찾아보세요~

RacingCar racingCar;
int n;
try{
racingCar=new RacingCar(input);
System.out.println("시도할 회수는 몇회인가요?");
input=Console.readLine();
n= getTurn(input);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input을 가져오는것도 getTurn의 일부로 볼 수 있지 않을까요?

}catch(IllegalArgumentException e) {
System.out.println("잘못된 값이 입력되어서 종료합니다.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IllegalArgumentException이 발생하면 알아서 종료될건데 catch해서 다시 print 해주고 종료하는 이유가 있나요?

print보단 커스텀 exception을 만들어서 종료해보는건 어떨까요

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;
}
}
30 changes: 30 additions & 0 deletions src/main/java/racingcar/Car.java
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(" ","");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공백을 없애준 이유가 있나요?

이름의 앞과 맨 마지막에 잘못 들어간 공백만 지워주는건 어떤가요?

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();
}
}
51 changes: 51 additions & 0 deletions src/main/java/racingcar/RacingCar.java
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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RacingCar로 네이밍한 이유가 있나요?
RacingCar보다는 RacingGame 같은게 더 알맞지 않을까요? 아니면 RacingCars로 여러대를 포함하고 있음을 알려주는게 좋을 것 같다고 생각해요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 변수명 뭘로 할까 고민하다 생각 안 나서 그냥 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(", ");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuilder에 대해 알아보면 좋을거 같아요~

}
}
return winnerList;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 changes: 5 additions & 4 deletions src/test/java/racingcar/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러 예외 상황이 있었을건데 최대한 많은 부분에 대해서 테스트 해보면 좋을것 같아요.

);
}

Expand Down