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

Apply feedback sukeun #4

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,29 @@ git checkout main // 기본 브랜치가 main인 경우
git checkout -b 브랜치이름
ex) git checkout -b apply-feedback
```

[] 자동차 이름,위치 정보를 가지는 객체 생성 - Car
[] 자동차 이름 , 에따라 주어지면 split 해서 Car 객체를 가지는 Cars 그룹 생성 - Cars
[] Validation체크
[]이름 5자 체크
[]null체크
[]isempty체크
[]정상적으로 들어왔는지 체크
[](,)에 따라 객체 생성 안될시 오류) - Cars
[] 자동차 움직임 구현 0~1 - Car , move
[] 자동차 그룹으로 움직임 구현 0~1 - Cars , move
[] 횟수에 따른 움직임 구현
[] 횟수가 음수 일시 오류 발생

[] 위치 정보에 따른 승자 뽑아내기

----- 출력
[] 자동차 이름 입력하세요
[] 도할 회수 몇회
[] 실행 결과
[] 횟수에 따른 이동 결과
[] 최종 우승 자동차
[] 1명, 0명 , 2명이상
----- 입력
[ ] 자동차 이름 입력
[ ] 횟수 입력
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'eclipse'

group = 'camp.nextstep'
version = '1.0.0'
sourceCompatibility = '1.8'
sourceCompatibility = "11"

repositories {
mavenCentral()
Expand All @@ -17,3 +17,4 @@ dependencies {
test {
useJUnitPlatform()
}
targetCompatibility = JavaVersion.VERSION_11
45 changes: 45 additions & 0 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Arrays;

public class StringAddCalculator {
public static int splitAndSum(String o) {
if (isNullOrEmpty(o)) return 0;
if(isCustom(o)) return customSplit(o);
if(isOneNumber(o)) return Integer.parseInt(o);
NegativeCheck(SplitNumber(o, ",|:"));
return Sum(SplitNumber(o, ",|:"));
}

private static String[] SplitNumber(String o, String s) {
return o.split(s);
}

private static boolean isNullOrEmpty(String o) {
return o == null || o.isEmpty();
}

private static boolean isCustom(String o) {
return o.contains("\n");
}

private static boolean isOneNumber(String o) {
return o.length() == 1;
}

private static void NegativeCheck(String[] splitNumber) {
if (isNegative(splitNumber)) throw new RuntimeException();
}

private static boolean isNegative(String[] splitNumber) {
return Arrays.stream(splitNumber).mapToInt(a -> Integer.parseInt(a)).anyMatch(t -> t < 0);
}

private static int customSplit(String o) {
String[] custom = SplitNumber(o.substring(2), "\n");
NegativeCheck(SplitNumber(custom[1], custom[0]));
return Sum(SplitNumber(custom[1], custom[0]));
}

private static int Sum(String[] splitNumber) {
return Arrays.stream(splitNumber).mapToInt(a -> Integer.parseInt(a)).sum();
}
}
7 changes: 7 additions & 0 deletions src/main/java/racing/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package racing;

public class Application {
public static void main(String[] args) {
RacingCar.run();
}
}
31 changes: 31 additions & 0 deletions src/main/java/racing/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package racing;

import javax.xml.stream.Location;

public class Car {
private LocationCar location;
private final CarName name;

public Car(String name) {
Validation.CheckName(name);
this.name = new CarName(name);
this.location = new LocationCar(0);
}

public String getName() {
return name.toString();
}

public int getLocation() {
return location.distance();
}

public void move(int v) {
location = location.move(v);
}

@Override
public String toString() {
return name + ": " + location +'\n';
}
}
14 changes: 14 additions & 0 deletions src/main/java/racing/CarName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package racing;

public class CarName {
private final String carName;

public CarName(String name) {
this.carName = name;
}

@Override
public String toString() {
return carName;
}
}
35 changes: 35 additions & 0 deletions src/main/java/racing/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racing;

import java.util.List;
import java.util.stream.Collectors;

public class Cars {
private final List<Car> carList;

public Cars(List<Car> carList) {
this.carList = carList;
}

public List<Car> getCar() {
return carList;
}

public void move(List<Car> carList) {
carList.stream().forEach(car -> car.move((int) Math.round(Math.random())));
}

public int Maxdistance() {
return carList.stream().mapToInt(car -> car.getLocation()).max().getAsInt();
}

public List<String> WinName(int distance) {
return carList.stream().
filter(car -> isSameDistance(distance, car)).
map(name -> name.getName()).
collect(Collectors.toList());
}

private boolean isSameDistance(int distance, Car car) {
return car.getLocation() == distance;
}
}
28 changes: 28 additions & 0 deletions src/main/java/racing/LocationCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racing;

public class LocationCar {


private final int location;

public LocationCar(int location) {
this.location = location;
}

public int distance() {
return this.location;
}

public LocationCar move(int v) {
return new LocationCar(this.location + v);
}

@Override
public String toString() {
String s = "";
for (int i = 0; i <location ; i++) {
s += "-";
}
return s;
}
}
58 changes: 58 additions & 0 deletions src/main/java/racing/RacingCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package racing;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RacingCar {

public static void run() {

try {
List<Car> carList = NewCar(InputView.start());
Cars cars = new Cars(carList);
OutPutView.result(cars,InputView.count());
} catch (IOException e) {
e.printStackTrace();
run();
} catch (IllegalArgumentException illegalArgumentException) {
System.out.println(illegalArgumentException.getMessage());
run();
}
}

public static List<Car> NewCar(String[] car) {
return Arrays.stream(car).map(a -> new Car(a)).collect(Collectors.toList());
}

private static class InputView {

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static String[] start() throws IOException {
System.out.println("경주할 자동차 이름을 입력하세요 ");
return (br.readLine()).split(",");
}

public static int count() throws IOException {
System.out.println("시도할 횟수는 몇회 인가요?");
return Validation.checkNegative(Integer.parseInt(br.readLine()));
}
}


private static class OutPutView {
static StringBuilder st = new StringBuilder();
public static void result(Cars cars, int count) {
System.out.println("실행 결과");
for (int i = 0; i < count; i++) {
cars.move(cars.getCar());
cars.getCar().stream().forEach(car -> st.append(car.toString()));
st.append("\n");
}
cars.WinName(cars.Maxdistance()).stream().forEach(car->st.append(car).append(","));
st.replace(st.length() - 1, st.length(), "가 최종 우승했습니다.");
System.out.println(st);
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/racing/Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package racing;

public class Validation {

public static final int NAME_SIZE = 6;

public static void CheckName(String name) {
ValidationName(name == null, "잘못된 이름 입니다 다시 입력 해 주세요");
ValidationName(name.isEmpty() || name.isBlank(), "잘못된 이름 입니다 다시 입력 해 주세요");
ValidationName(name.length() >= NAME_SIZE, "이름이 6글자 이상입니다. : "+name);
}


private static void ValidationName(boolean b, String s) {
if (b) throw new IllegalArgumentException(s);
}

public static int checkNegative(int parseInt) {
if(parseInt < 0) throw new IllegalArgumentException("음수는 입력할 수 없습니다.");
return parseInt;
}
}
45 changes: 45 additions & 0 deletions src/test/java/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import org.junit.jupiter.api.Test;

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

public class StringAddCalculatorTest {
@Test
public void splitAndSum_null_또는_빈문자() {
int result = StringAddCalculator.splitAndSum(null);
assertThat(result).isEqualTo(0);

result = StringAddCalculator.splitAndSum("");
assertThat(result).isEqualTo(0);
}

@Test
public void splitAndSum_숫자하나() throws Exception {
int result = StringAddCalculator.splitAndSum("1");
assertThat(result).isEqualTo(1);
}

@Test
public void splitAndSum_쉼표구분자() throws Exception {
int result = StringAddCalculator.splitAndSum("1,2");
assertThat(result).isEqualTo(3);
}

@Test
public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception {
int result = StringAddCalculator.splitAndSum("1,2:3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_custom_구분자() throws Exception {
int result = StringAddCalculator.splitAndSum("//;\n1;2;3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_negative() throws Exception {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3"))
.isInstanceOf(RuntimeException.class);
}
}
36 changes: 36 additions & 0 deletions src/test/java/racing/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racing;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Random;

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

public class CarTest {
@Test
void 자동차_객체_생성_확인() {
assertThat((new Car("abc")).getName().toString()).isEqualTo("abc");
}

@Test
void 자동차_움직임_구현() {
Car car = new Car("test1");
car.move(1);
assertThat(car.getLocation()).isLessThanOrEqualTo(1);
car.move(0);
assertThat(car.getLocation()).isLessThanOrEqualTo(1);
}

@Test
void 자동차_5번_움직임_구현() {
Car car = new Car("test1");
for (int i = 0; i <= 5; i++) {
car.move(1);
}
System.out.println(car.getLocation());
assertThat(car.getLocation()).isGreaterThanOrEqualTo(1);
assertThat(car.getLocation()).isLessThanOrEqualTo(6);
}

}
Loading