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

5주차 미션 구현 #8

Open
wants to merge 2 commits into
base: oliviarla
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
28 changes: 11 additions & 17 deletions src/main/java/racingcar/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,32 @@
import java.util.Random;

public class Car {
private static final int START_LOCATION=0;
private final String name;
private int location;
private static final int MAX_BOUND=10;
private final Name name;
private final Location location;

public Car(String name) throws RuntimeException {
if(name.length()>5){
throw new RuntimeException("자동차 이름은 5글자 이내여야 합니다.");
}
if(name.trim().isEmpty()){
throw new RuntimeException("자동차 이름은 공백일 수 없습니다.");
}
public Car(Name name) {
this.name = name;
this.location=START_LOCATION;
this.location=new Location();
}

public int randomNumGenerator(){
protected int randomNumGenerator(){
Random random = new Random();
return random.nextInt(10);
return random.nextInt(MAX_BOUND);
}

public void forward(){
int randomNum = randomNumGenerator();
if(randomNum>4){
this.location++;
if(randomNum>=4){
this.location.forward();
}
}

public int getLocation() {
public Location getLocation() {
return this.location;
}

public String getName() {
public Name getName() {
return this.name;
}
}
35 changes: 21 additions & 14 deletions src/main/java/racingcar/domain/Cars.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Collections.max;

public class Cars {
private final List<Car> carList = new ArrayList<>();

public void addCar(Car car){
this.carList.add(car);
public Cars(String[] strings) {
for (String s : strings) {
Name name = new Name(s);
carList.add(new Car(name));
}
}

public void forwardAll(){
public void forwardAll() {
carList.forEach(Car::forward);
}

public int maxLocation(){
return max(carList.stream().map(Car::getLocation).collect(Collectors.toList()));
public Location maxLocation() {
Location location = new Location();
for (Car c : carList) {
if (c.getLocation().isGreaterThan(location)) {
location = c.getLocation();
}
}
return location;
}

public String getMaxNames(){
public String getMaxNames() {
List<String> result = new ArrayList<>();
for(Car c: carList){
if(c.getLocation()==maxLocation()){
result.add(c.getName());
for (Car c : carList) {
if (c.getLocation().equals(maxLocation())) {
result.add(c.getName().getName());
}
}
if(!result.isEmpty()){
if (!result.isEmpty()) {
return String.join(",", result);
}
throw new RuntimeException("최대로 이동한 자동차를 찾을 수 없습니다.");
throw new RuntimeException("최댓값을 가진 이름을 반환할 수 없습니다");
}

public List<Car> getCarList(){
public List<Car> getCarList() {
return carList.stream().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}
}
43 changes: 43 additions & 0 deletions src/main/java/racingcar/domain/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package racingcar.domain;

import java.util.Objects;

public class Location{
private int location;

Choose a reason for hiding this comment

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

Location은 원시 값을 포장한 값 객체처럼 보이는데, 인자를 final로 만들어 immutable 하게 만들면 어떨까요? ☺️


public Location() {
this.location=0;
}
public Location(int location) {
this.location = location;
}

public void forward(){
this.location++;
}

public String makeBar(){
StringBuilder result = new StringBuilder();
for(int i=0;i<location;i++){
result.append("-");
}
return result.toString();
}

public boolean isGreaterThan(Location o) {
return this.location > o.location;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location location1 = (Location) o;
return location == location1.location;
}

@Override
public int hashCode() {
return Objects.hash(location);
}
}
36 changes: 36 additions & 0 deletions src/main/java/racingcar/domain/Name.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racingcar.domain;

import java.util.Objects;

public class Name {
private final String name;
private static final int MAX_LENGTH = 5;

public Name(String name) throws IllegalArgumentException {
if (name==null || name.trim().isEmpty()) {
throw new IllegalArgumentException("자동차 이름은 공백일 수 없습니다.");
}

if (name.length() >= MAX_LENGTH) {
throw new IllegalArgumentException("자동차 이름은 5글자 이내여야 합니다.");
}
this.name = name;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Name name1 = (Name) o;
return Objects.equals(name, name1.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}
6 changes: 1 addition & 5 deletions src/main/java/racingcar/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ public Cars makeCars() {
String input = scanner.next();

String[] strings = input.split(",");
Cars cars = new Cars();
for (String s : strings) {
cars.addCar(new Car(s));
}
return cars;
return new Cars(strings);
}

public int getTries(){
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/racingcar/view/OutputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
import racingcar.domain.Cars;

public class OutputView {
public String locationToLine(int location){
StringBuilder result = new StringBuilder();
for(int i=0;i<location;i++){
result.append("-");
}
return result.toString();
}

public void outputCars(Cars cars){
for(Car c: cars.getCarList()){
System.out.print(c.getName()+" : "+locationToLine(c.getLocation())+"\n");
System.out.print(c.getName().getName()+" : "+c.getLocation().makeBar()+"\n");
}
System.out.println();
}
Expand Down
35 changes: 20 additions & 15 deletions src/test/java/racingcar/domain/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,35 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class CarTest {
Car car;
@BeforeEach
public void setUp(){
String name = "jeep";
car = new Car(name);
}

@Test
@DisplayName("5글자 초과시 Exception 발생 테스트")
public void nameTest(){
String name = "sonata";
assertThatThrownBy(()-> new Car(name)).isInstanceOf(RuntimeException.class);
assertThatThrownBy(()-> new Car(new Name("sonata"))).isInstanceOf(IllegalArgumentException.class);
}

@Test
@DisplayName("랜덤 숫자가 0~9 사이인지 테스트")
public void randomNumTest(){
assertThat(car.randomNumGenerator()).matches(integer -> integer>=0 && integer<=9);
@DisplayName("전진이 제대로 수행되는지 테스트")
public void forwardTest(){
Car car = new Car(new Name("jeep")){
@Override
protected int randomNumGenerator() {
return 3;
}
};
car.forward();
assertThat(car.getLocation()).isEqualTo(new Location(0));
}

@Test
@DisplayName("전진이 제대로 수행되는지 테스트")
public void forwardTest(){
@DisplayName("전진이 제대로 수행되는지 테스트2")
public void forwardTest2(){
Car car = new Car(new Name("jeep")){
@Override
protected int randomNumGenerator() {
return 4;
}
};
car.forward();
assertThat(car.getLocation()).isIn(0,1);
assertThat(car.getLocation()).isEqualTo(new Location(1));
}
}
13 changes: 1 addition & 12 deletions src/test/java/racingcar/domain/CarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,13 @@ public class CarsTest {
Cars cars;
@BeforeEach
public void setUp(){
cars = new Cars();
cars.addCar(new Car("ray"));
cars.addCar(new Car("audi"));
cars.addCar(new Car("benz"));
cars.addCar(new Car("tico"));
cars = new Cars(new String[]{"ray", "audi", "benz", "tico"});
cars.forwardAll();
}

@Test
@DisplayName("max 값이 정상적으로 도출되는지 확인")
public void maxTest(){
assertThat(cars.maxLocation()).isEqualTo(1);
}

@Test
@DisplayName("우승 자동차의 이름 출력되는지 확인")
public void getNameTest(){
assertThat(cars.getMaxNames()).isNotNull();
}

}
28 changes: 28 additions & 0 deletions src/test/java/racingcar/domain/LocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racingcar.domain;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class LocationTest {
Location l1 = new Location();
Location l2 = new Location();

@Test
void forwardTest1(){
l1.forward();
l1.forward();
assertThat(l1).isEqualTo(new Location(2));
}

@Test
void forwardTest2(){
assertThat(l2).isEqualTo(new Location(0));
}

@Test
void compareTest(){
assertThat(new Location(2).isGreaterThan(new Location(0))).isTrue();
assertThat(new Location(1).isGreaterThan(new Location(2))).isFalse();
}
}
24 changes: 24 additions & 0 deletions src/test/java/racingcar/domain/NameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package racingcar.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class NameTest {
@Test
@DisplayName("Name 5글자 이상일 경우 예외 발생 테스트")
void checkValid1(){
assertThatThrownBy(() -> new Name("giant")).isInstanceOf(IllegalArgumentException.class);
}
@Test
@DisplayName("Name 비어있을 경우 예외 발생 테스트")
void checkValid2(){
assertThatThrownBy(() -> new Name("")).isInstanceOf(IllegalArgumentException.class);
}
@Test
@DisplayName("Name 비어있을 경우 예외 발생 테스트2")
void checkValid3(){
assertThatThrownBy(() -> new Name(" ")).isInstanceOf(IllegalArgumentException.class);
}
}