-
Notifications
You must be signed in to change notification settings - Fork 49
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
이가연 레이싱카 미션 1단계, 2단계 제출합니다 #59
base: gy102912
Are you sure you want to change the base?
Changes from 5 commits
cfb6707
271b314
39f7cd3
a06f16a
6c116c2
f6fc534
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,25 @@ | ||
public class RacingCar { | ||
private String name; | ||
|
||
public RacingCar(String name) { | ||
setName(name); | ||
} | ||
|
||
public int moveByRandom(int random){ | ||
if (random >= 4){ | ||
return 1; //go | ||
} | ||
return 0; //stop | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
if (name == null || name.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Racing car name cannot be empty"); | ||
} | ||
this.name = name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class RacingContest { | ||
private static final Random random = new Random(); | ||
private final RacingCar[] RACING_CARS; | ||
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. 어떤건 array를 쓰고 어떤건 List를 썼군요 가연님만의 기준이 있으신가요? 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. 전체 갯수를 이미 알고 있는 경우에는 array를 사용하고 레이싱 경기 우승자와 같이 미리 전체 갯수를 모를 땐 list를 사용했습니다. array, list 중에 무엇이 적절한 지 선택하기 위한 더 나은 기준이 있을까요? 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 int rounds; | ||
|
||
public RacingContest(RacingCar[] racingCars, int racingRounds) { | ||
RACING_CARS = racingCars; | ||
rounds = racingRounds; | ||
} | ||
|
||
public int[] start(){ | ||
int[] distances = new int[RACING_CARS.length]; | ||
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. distance를 메서드 내부의 변수로 관리하셨군요. 만약 RacingCar 내부에 필드로 갖게된다면 어떨까요? 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. 생각하지 못했었는데 RacingCar 클래스의 필드로 사용하는 것이 테스트할 때 더 편할 것 같습니다! |
||
|
||
for (int r = 0; r < rounds; r++) { | ||
for (int c = 0; c < RACING_CARS.length; c++) { | ||
distances[c] += RACING_CARS[c].moveByRandom(random.nextInt(10)); | ||
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. 한줄에 너무 많은게 담겨 있는 거 같아요. c번째 car가 움직이는 것을 랜덤값으로 뽑아서 움직인다. 하나의 줄에 여러개의 메서드가 포함되는 것은 가독성을 떨어뜨릴 수 있을 것 같아요. 어떻게 풀어볼 수 있을까요? |
||
} | ||
} | ||
|
||
return distances; | ||
} | ||
|
||
public ArrayList<String> ranking(int[] distances){ | ||
int maxDist = Arrays.stream(distances).max().getAsInt(); // winner decision | ||
ArrayList<String> winners = new ArrayList<>(); | ||
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. ArrayList와 List는 어떤 차이가 있을까요? |
||
|
||
for (int i = 0; i < distances.length; i++) { | ||
if (distances[i] == maxDist) { | ||
winners.add(RACING_CARS[i].getName()); | ||
} | ||
} | ||
|
||
return winners; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestRacingCar { | ||
|
||
private RacingCar racingCar; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
racingCar = new RacingCar("myRacingCar"); | ||
} | ||
|
||
@Test | ||
@DisplayName("레이싱카 전진 확인") | ||
public void testGo(){ | ||
//given | ||
int random = 9; | ||
|
||
//when | ||
int actual = racingCar.moveByRandom(random); | ||
int expected = 1; | ||
|
||
//then | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
@Test | ||
@DisplayName("레이싱카 정지 확인") | ||
public void testStop(){ | ||
//given | ||
int random = 0; | ||
|
||
//when | ||
int actual = racingCar.moveByRandom(random); | ||
int expected = 0; | ||
|
||
//then | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
@DisplayName("레이싱카의 이름이 유효하지 않을 시 에러 발생") | ||
public void testValidName(){ | ||
//given | ||
|
||
//when | ||
|
||
//then | ||
assertThatThrownBy(() -> { | ||
racingCar.setName(null); | ||
}).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestRacingContest { | ||
private static int round; | ||
private static RacingContest contest; | ||
private static final String[] names = new String[]{"neo", "brie", "brown"}; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
RacingCar[] participants = new RacingCar[]{ | ||
new RacingCar(names[0]), | ||
new RacingCar(names[1]), | ||
new RacingCar(names[2]) | ||
}; | ||
round = 10; | ||
contest = new RacingContest(participants, round); | ||
} | ||
|
||
@Test | ||
@DisplayName("경기 결과 모든 차의 주행거리가 0 이상이고 주어진 횟수 round 이하인지 확인") | ||
public void testStart() { | ||
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. start의 기능에 대한 테스트가 조금 모자라게 느껴지는 것 같아요. 조금 더 명확하고 효과적인 테스트는 어떻게 작성할 수 있을까요?? |
||
//given | ||
int round = contest.getRounds(); | ||
|
||
//when | ||
int[] distances = contest.start(); | ||
int maxDist = Arrays.stream(distances).max().getAsInt(); | ||
int minDist = Arrays.stream(distances).min().getAsInt(); | ||
|
||
//then | ||
assertThat(maxDist).isBetween(0, round + 1); | ||
assertThat(minDist).isBetween(0, round + 1); | ||
} | ||
@Test | ||
@DisplayName("경기 결과를 바탕으로 우승자를 가려낼 수 있는지 확인") | ||
public void testRanking() { | ||
//given | ||
int[] distances = new int[]{0, round, round}; | ||
String[] winners = new String[]{names[1], names[2]}; | ||
|
||
//when | ||
ArrayList<String> actual = contest.ranking(distances); | ||
ArrayList<String> expected = new ArrayList<>(Arrays.asList(winners)); | ||
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. 이런식으로 두번 초기화할 필요는 없을 것 같아요. |
||
|
||
//then | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
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. 요렇게 쓰는 것은 순서에 영향을 받을 위험이 있습니다. 어떻게 해결할 수 있을까요?? (만약, winners가 new String[]{names[2], names[1]}) 인 경우 테스트 미통과 처리 |
||
} | ||
} |
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.
setName이란 메서드가 RacingCar 내부 외에서 호출되는 일이 있나요?