Skip to content
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

이가연 Step3, 4 미션 제출합니다 #69

Open
wants to merge 6 commits into
base: gy102912
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified build.gradle
100644 → 100755
Empty file.
Empty file modified gradle/wrapper/gradle-wrapper.jar
100644 → 100755
Empty file.
Empty file modified gradle/wrapper/gradle-wrapper.properties
100644 → 100755
Empty file.
Empty file modified gradlew.bat
100644 → 100755
Empty file.
Empty file modified settings.gradle
100644 → 100755
Empty file.
Empty file modified src/main/java/.gitkeep
100644 → 100755
Empty file.
15 changes: 15 additions & 0 deletions src/main/java/cholog/Main.java
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);
}
}
26 changes: 26 additions & 0 deletions src/main/java/cholog/domain/RacingCar.java
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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/cholog/domain/RacingContest.java
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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/cholog/view/InputView.java
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;
}
}
44 changes: 44 additions & 0 deletions src/main/java/cholog/view/ResultView.java
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 modified src/test/java/.gitkeep
100644 → 100755
Empty file.
58 changes: 58 additions & 0 deletions src/test/java/cholog/TestRacingCar.java
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);
}
}
53 changes: 53 additions & 0 deletions src/test/java/cholog/TestRacingContest.java
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);
}
}