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

[자동차 경주] 이서준 미션 제출합니다. #82

Open
wants to merge 2 commits into
base: seojoonleee
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
45 changes: 45 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.*;

public class Car {
private String name;
private int location;
private static final Random rand = new Random();

public Car(String name) {
this.name = name;
this.location = 0;
validateName(name);
}

private void validateName(String name) {
if (name.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하여야 함");
}
}


public int move() {
int randomValue = rand.nextInt(10);

if (randomValue >= 4) {
return location++;
}
return location;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getLocation() {
return location;
}

public void setLocation(int location) {
this.location = location;
}
}
60 changes: 60 additions & 0 deletions src/main/java/CarRacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class CarRacingGame {
private final List<Car> cars;
private final int rounds;

public CarRacingGame(List<String> carNames, int rounds) {
this.cars = createCars(carNames);
this.rounds = rounds;
}

private List<Car> createCars(List<String> carNames) {
// 자동차 이름으로 car 객채 생성
return carNames.stream()
.map(Car::new)
.collect(Collectors.toList()); // car 객체들을 리스트로 변환
}

public void race() {
for (int i = 0; i < rounds; i++) {
cars.forEach(Car::move);
printRace();
}
}

// 라운드가 진행될 때마다 자동차들의 위치 출력
private void printRace() {
for (Car car : cars) {
System.out.println(car.getName() + " : " + "-".repeat(car.getLocation()));
}
System.out.println();
}

// 이동 거리가 가장 긴 자동차 리스트를 반환
public List<Car> getWinners() {
int maxLocation = findMaxLocation();
return findWinners(maxLocation);
}

// 그 최대 이동 거리를 구하는 메소드
private int findMaxLocation() {
return cars.stream()
.mapToInt(Car::getLocation)
.max()
.orElse(0);
}

// 앞에서 구한 최대 이동 거리를 통해 우승 자동차 리스트를 뽑는 메소드
private List<Car> findWinners(int maxLocation) {
return cars.stream()
.filter(car -> car.getLocation() == maxLocation)
.collect(Collectors.toList());
}

public List<Car> getCars() {
return cars;
}
}
43 changes: 43 additions & 0 deletions src/main/java/CarRacingGameApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Scanner;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

public class CarRacingGameApp {
public static void main(String[] args) {
List<String> carNames = getCarNames();
int rounds = getRounds();

CarRacingGame game = new CarRacingGame(carNames, rounds);
game.race();

printWinners(game.getWinners());
}

// 자동차 이름 추출
private static List<String> getCarNames() {
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

// 쉼표를 기준으로 이름 추출
return Arrays.stream(input.split(","))
.map(String::trim)
.collect(Collectors.toList());
}

// 라운드 횟수 추출
private static int getRounds() {
System.out.println("시도할 횟수는 몇 회인가요?");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

// CarRacingGame 클래스로부터 받아온 우승자 명단을 출력
private static void printWinners(List<Car> winnerList) {
String winnerNames = winnerList.stream()
.map(Car::getName)
.collect(Collectors.joining(", "));
System.out.println(winnerNames + "가 최종 우승했습니다.");
}
}
35 changes: 35 additions & 0 deletions src/test/java/CarRacingGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.Arrays;

public class CarRacingGameTest {

@Test
void 자동차_경주_이동_테스트() {
Car car = new Car("포니");
int initialLocation = car.getLocation();

for (int i = 0; i < 100; i++) {
car.move();
}

// 현재 위치값이 초기값 이상이면 테스트 통과
assertTrue(car.getLocation() >= initialLocation);
}

@Test
void 우승자_테스트() {
List<String> carNames = Arrays.asList("쿠페", "다시아", "버기", "피코버스", "오토바이");
CarRacingGame game = new CarRacingGame(carNames, 5);
game.race();

List<Car> winners = game.getWinners();
int maxLocation = winners.get(0).getLocation(); // 우승자의 위치 저장

// 우승자들의 위치가 같은 지 확인
for (Car winner : winners) {
assertEquals(maxLocation, winner.getLocation());
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class CarTest {

@Test
@DisplayName("자동차 움직임 기능 테스트")
public void moveTest() {
Car car = new Car("부릉부릉");
int location = car.getLocation();

// 초기 위치 테스트
Assertions.assertThat(location).isEqualTo(0);

// 10번 랜덤 돌리기
for (int i = 0; i < 10; i++) {
car.move();
}

// 자동차가 멈췄다면 위치가 변하지 않을 수 있음을 확인
Assertions.assertThat(car.getLocation()).isBetween(location, location + 10);
}

@Test
@DisplayName("작명 기능 테스트")
public void nameTest() {
Car car = new Car("꼬마자동차");

// 기본 이름 테스트
Assertions.assertThat(car.getName()).isEqualTo("꼬마자동차");

// 이름 변경 테스트
car.setName("UAZ");
Assertions.assertThat(car.getName()).isEqualTo("UAZ");
}
}