Skip to content
15 changes: 15 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
자동차 이름을 입력

입력받은 문장 쪼개기

예외처리

게임 횟수 입력

게임 진행 출력 및 랜덤 계산

우승자 찾기

우승자 출력

Test추가
Comment on lines +1 to +15

Choose a reason for hiding this comment

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

기능 정리하고 각 기능 단위로 커밋 해주셨군요! 좋습니다!!
커밋 메시지 관련해서 깃허브 커밋 메시지 컨벤션 키워드 더 공부해보시면 좋을 것 같아요.
코인 프로젝트의 커밋 메시지를 참고해보세요!

101 changes: 100 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,106 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static camp.nextstep.edu.missionutils.Console.readLine;
import static org.assertj.core.api.Assertions.assertThat;


public class Application {

@Test
void test() {
String input = "1,2,3,45";
String[] result = input.split(",");

assertThat(result).contains("45","3","2", "1");
assertThat(result).containsExactly("1","2","3", "45");
}
Comment on lines +15 to +22

Choose a reason for hiding this comment

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

이 코드는 테스트 코드 시험 목적으로 작성하신건가요? 테스트 코드는 보통 src/test/java 패키지 내부에서 작성해요. 테스트 코드를 별도의 패키지에서 작성하는 이유가 뭘까요?


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

for (String name : userInput.split(",")) {
carNames.add(name.trim());
}
Comment on lines +29 to +31

Choose a reason for hiding this comment

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

stream(), trim(), join() 등 자바에서 제공하는 api를 적극적으로 사용하진 부분이 좋습니다!


//예외처리---------------------------------------------------
try {
Comment on lines +32 to +34

Choose a reason for hiding this comment

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

주석을 통해 코드에 대한 설명을 덧붙여 주셨네요. 코드를 읽는 입장에서 이해하기 편했어요. 다만, 주석을 사용하지 않고 코드 자체로 가독성을 챙길 수 있는 여러가지 방법이 있을 것 같아요. 함수를 이용하는 방법은 어떨까요?

for (String Name : carNames) {
if (Name.length() > 5) {
throw new IllegalArgumentException();
}
Comment on lines +36 to +38

Choose a reason for hiding this comment

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

IllegalArgumentException 생성시 인자로 문자열을 넘겨줄 수 있어요. 예외의 원인을 담아서 예외 객체를 생성하면 어떤 장점이 있을까요?

}
} catch (IllegalArgumentException e) {
return;
}
Comment on lines +40 to +42

Choose a reason for hiding this comment

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

try-catch 문을 통해 예외 처리를 해주셨네요. catch 문 내부를 비워두셨는데, 이유가 있으신가요?


System.out.println("시도할 회수는 몇회인가요?");
String count1 = readLine();
int count = Integer.parseInt(count1);

System.out.println("실행 결과");

//경주준비-------------------------------------------------
List<Integer> score = new ArrayList<>();
String line;

for (String Name1 : carNames) {
score.add(0);
}

//경주시작-------------------------------------------------
int ssc = 0;
Comment on lines +58 to +59

Choose a reason for hiding this comment

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

변수의 역할이 잘 드러나도록 이름을 지어주시면 좋아요!

for (int i = 0; i < count * carNames.size(); i++) {

if (ssc == carNames.size()) {
ssc = 0;
}

//랜덤계산-------------------------------------------------
int randomInt = Randoms.pickNumberInRange(0, 9);
if (randomInt > 3) {
score.set(ssc, score.get(ssc) + 1);
Comment on lines +67 to +69

Choose a reason for hiding this comment

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

요구사항에서 주어진 값을 상수로 처리하는건 어떻게 생각하시나요?

}
ssc = ssc + 1;
}

//1등 찾기--------------------------------------------------------------
int rank1 = 1;
int rank2 = 0;
for (int k = 0; score.size() - 1 > k; k++) {
rank2 = Math.max(score.get(k), score.get(k + 1));
if (rank2>rank1){
rank1=rank2;
}
}
Comment on lines +75 to +82

Choose a reason for hiding this comment

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

가장 멀리 나간 자동차를 찾기 위한 로직 같아요. rank2 변수를 사용하지 않는 방법은 없을까요?


if (carNames.size()==1){
rank1=score.get(0);
}
Comment on lines +84 to +86

Choose a reason for hiding this comment

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

바로 위 반복문의 종료 조건 score.size() - 1 때문에 추가한 코드 같아요. 위 기능에서 한번에 처리할 수 있을 것 같습니다.


//출력-------------------------------------------------
for (int k = 0; score.size() > k; k++) {
line = "-".repeat(score.get(k));
System.out.println(carNames.get(k) + " : " + line);
}
System.out.println("");

// 1등 출력---------------------------------------------------
List<String> winner = new ArrayList<>();
for (int k = 0; score.size()> k; k++) {
if (rank1== score.get(k)){
winner.add(carNames.get(k));
}
}
System.out.print("최종 우승자 : " + String.join(" ,", winner));

}
}

12 changes: 6 additions & 6 deletions src/test/java/racingcar/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class ApplicationTest extends NsTest {
@Test
void 전진_정지() {
assertRandomNumberInRangeTest(
() -> {
run("pobi,woni", "1");
assertThat(output()).contains("pobi : -", "woni : ", "최종 우승자 : pobi");
},
MOVING_FORWARD, STOP
() -> {
run("pobi,woni", "1");
assertThat(output()).contains("pobi : -", "woni : ", "최종 우승자 : pobi");
},
MOVING_FORWARD, STOP
);
}

Expand All @@ -35,4 +35,4 @@ class ApplicationTest extends NsTest {
public void runMain() {
Application.main(new String[]{});
}
}
}