-
Notifications
You must be signed in to change notification settings - Fork 79
이가연 레이싱카 미션 1단계, 2단계 제출합니다 #59
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
Open
GY102912
wants to merge
6
commits into
next-step:gy102912
Choose a base branch
from
GY102912:step1
base: gy102912
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cfb6707
feat: racing car
GY102912 271b314
test: racing car
GY102912 39f7cd3
refactor: moveByRandom go 또는 stop 문자열 반환하도록 변경
GY102912 a06f16a
feat: racing contest
GY102912 6c116c2
style: 필요없는 주석 삭제
GY102912 f6fc534
feat: racing contest with input/output view
GY102912 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import domain.RacingContest; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws Exception { | ||
final var playerNames = InputView.getPlayerNames(); | ||
final var rounds = InputView.getRounds(); | ||
final var racingContest = new RacingContest(playerNames, rounds); | ||
|
||
ResultView.printContest(racingContest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
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 | ||
} | ||
|
||
private void setName(String name) { | ||
if (name == null || name.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Racing car name cannot be empty"); | ||
} | ||
this.name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class RacingContest { | ||
private static final Random random = new Random(); | ||
private final List<RacingCar> racingCars; | ||
private final Map<String, Integer> distances; | ||
private final int rounds; | ||
|
||
public RacingContest(List<String> playerNames, int rounds) { | ||
this.racingCars = playerNames.stream().map(RacingCar::new).toList(); | ||
this.distances = new HashMap<>(); | ||
for (RacingCar car : racingCars){ | ||
distances.putIfAbsent(car.getName(), 0); | ||
} | ||
this.rounds = rounds; | ||
} | ||
|
||
public void goRound(){ | ||
for (RacingCar car : racingCars) { | ||
String name = car.getName(); | ||
int move = car.moveByRandom(random.nextInt(10)); | ||
distances.compute(name, (k, v) -> v + move); | ||
} | ||
} | ||
|
||
public List<String> getWinners() { | ||
List<String> winners = new ArrayList<>(); | ||
int max = distances.values().stream().max(Integer::compareTo).get(); | ||
distances.entrySet().stream() | ||
.filter(e -> e.getValue() >= max) | ||
.forEach(e -> winners.add(e.getKey())); | ||
|
||
return winners; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package view; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private static final Scanner sc = new Scanner(System.in); | ||
|
||
public static List<String> getPlayerNames() throws Exception { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
var playerNames = Arrays.asList(sc.nextLine().split(",")); | ||
if (playerNames.isEmpty()) { | ||
throw new Exception("자동차 이름이 입력되지 않았습니다."); | ||
} | ||
if (playerNames.size() < 2) { | ||
throw new Exception("2개 이상의 자동차 이름이 입력되지 않았습니다."); | ||
} | ||
return playerNames; | ||
} | ||
|
||
public static int getRounds() throws Exception { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
var rounds = Integer.parseInt(sc.nextLine()); | ||
if (rounds <= 0) { | ||
throw new IOException(); | ||
} | ||
return rounds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package view; | ||
|
||
import domain.RacingContest; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ResultView { | ||
|
||
private static void printRound(Map<String, Integer> distances) { | ||
if (distances.isEmpty()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (Map.Entry<String, Integer> player : distances.entrySet()) { | ||
sb.append(player.getKey()).append(" : "); | ||
sb.append("-".repeat(player.getValue())).append("\n"); | ||
} | ||
|
||
System.out.println(sb); | ||
} | ||
|
||
public static void printWinners(List<String> winners) { | ||
if (winners.isEmpty()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
System.out.print(String.join(", ", winners)); | ||
System.out.println("가 최종 우승했습니다."); | ||
} | ||
|
||
public static void printContest(RacingContest contest) { | ||
if (contest == null) { | ||
throw new IllegalArgumentException(); | ||
} | ||
int rounds = contest.getRounds(); | ||
|
||
System.out.println("\n실행 결과"); | ||
for (int r = 0; r < rounds; r++){ | ||
contest.goRound(); | ||
printRound(contest.getDistances()); | ||
} | ||
printWinners(contest.getWinners()); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import domain.RacingCar; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
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 | ||
String name = null; | ||
|
||
//when | ||
|
||
//then | ||
assertThatThrownBy(() -> { | ||
new RacingCar(name); | ||
}).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import domain.RacingCar; | ||
import domain.RacingContest; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestRacingContest { | ||
|
||
private static RacingContest contest; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
int rounds = 5; | ||
List<String> playerNames = Arrays.asList("neo", "brie", "brown"); | ||
contest = new RacingContest(playerNames, rounds); | ||
} | ||
|
||
@Test | ||
@DisplayName("경기 결과 모든 차의 주행거리가 0 이상이고 주어진 횟수 round 이하인지 확인") | ||
public void testStart() { | ||
//given | ||
int round = contest.getRounds(); | ||
var distances = contest.getDistances(); | ||
|
||
//when | ||
int maxDist = distances.values().stream().max(Integer::compareTo).get(); | ||
int minDist = distances.values().stream().min(Integer::compareTo).get(); | ||
|
||
//then | ||
assertThat(maxDist).isBetween(0, round + 1); | ||
assertThat(minDist).isBetween(0, round + 1); | ||
} | ||
@Test | ||
@DisplayName("경기 결과를 바탕으로 우승자를 가려낼 수 있는지 확인") | ||
public void testRanking() { | ||
//given | ||
contest.goRound(); | ||
var distances = contest.getDistances(); | ||
var winners = contest.getWinners(); | ||
|
||
//when | ||
var actual = distances.get(winners.get(0)); | ||
var expected = distances.values().stream().max(Integer::compareTo).get(); | ||
|
||
//then | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
start의 기능에 대한 테스트가 조금 모자라게 느껴지는 것 같아요.
조금 더 명확하고 효과적인 테스트는 어떻게 작성할 수 있을까요??