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

feat(step3): 자동차 경주 #5332

Open
wants to merge 2 commits into
base: chyjeon
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
22 changes: 13 additions & 9 deletions src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

public class Calculator {
private static final String ADDITOR = ",|:";
private static final String NEW_OPERATOR = "//(.)\n(.*)";

public static int calculate(String text){
if(isBlank(text)){
if (isBlank(text)){
return 0;
}

Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text);
if(m.find()){
Matcher m = Pattern.compile(NEW_OPERATOR).matcher(text);
if (m.find()){
return sumWithCustomizingAdditor(m);
}

Expand All @@ -27,9 +28,12 @@ private static String[] split(String text){
return text.split(ADDITOR);
}

private static final int NEW_ADDITOR = 1;
private static final int SPLIT_TEXT = 2;

private static int sumWithCustomizingAdditor(Matcher m){
String customDelimiter = m.group(1);
String[] tokens = m.group(2).split(customDelimiter);
String customDelimiter = m.group(NEW_ADDITOR);
String[] tokens = m.group(SPLIT_TEXT).split(customDelimiter);

return sum(toInts(tokens));
}
Expand All @@ -38,15 +42,15 @@ private static int sumWithCustomizingAdditor(Matcher m){
private static int sum(int[] numbers) {
int result = 0;

for(int number : numbers){
for (int number : numbers){
result += number;
}
return result;
}

private static int[] toInts(String[] values){
int[] numbers = new int[values.length];
for(int i=0; i<values.length ; i++){
for (int i=0; i<values.length ; i++){
numbers[i] = checkPositive(values[i]);;
}
return numbers;
Expand All @@ -55,8 +59,8 @@ private static int[] toInts(String[] values){

private static int checkPositive(String value){
int result = Integer.parseInt(value);
if(result < 0){
throw new RuntimeException();
if (result < 0){
throw new IllegalArgumentException();
}
return result;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/racingcar/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package racingcar;

public class Controller {
public void racingGame(){
InputView inputView = new InputView();

int numOfCar = inputView.numberOfCar();
int numOfTry = inputView.ChanceOfTry();
}
}
Empty file.
22 changes: 22 additions & 0 deletions src/main/java/racingcar/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package racingcar;

import java.util.Scanner;

public class InputView {

private int numberOfCar;
private int chanceOfTry;

Scanner scanner = new Scanner(System.in);

public int numberOfCar(){
System.out.println("자동차 대수는 몇 대 인가요?\n");
return scanner.nextInt();
}

public int ChanceOfTry(){
System.out.println("시도할 회수는 몇 회 인가요?\n");
return scanner.nextInt();
}

}
8 changes: 8 additions & 0 deletions src/main/java/racingcar/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racingcar;

public class OutputView {
public void racingResult(String text){
System.out.println("실행 결과");
System.out.println(text);
}
}
10 changes: 10 additions & 0 deletions src/main/java/racingcar/RacingCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package racingcar;

public class RacingCar {

public static String racingCar(int num){
if (num == 0)
return "";
return "num";
}
}
Empty file.
Empty file.
Empty file.
Binary file added src/test/.DS_Store
Binary file not shown.
Binary file added src/test/java/.DS_Store
Binary file not shown.
10 changes: 7 additions & 3 deletions src/test/java/calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import static calculator.Calculator.calculate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

@DisplayName("음수나 숫자가 아니면 에러")
@ParameterizedTest
@ValueSource(strings = {"-1", "1#2#3"})
void exception_test(String elements) {
Assertions.assertThrows(RuntimeException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
calculate("-1");
});
}
Expand Down Expand Up @@ -48,7 +50,9 @@ void single_text() {
@Test
@DisplayName("Null 값 빈문자")
void null_value() {
assertThat(calculate(null)).isEqualTo(0);
assertThat(calculate("")).isEqualTo(0);
assertAll(
()->assertEquals(calculate(null),0),
()->assertEquals(calculate(""),0)
);
}
}
18 changes: 18 additions & 0 deletions src/test/java/racingcar/RacingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package racingcar;

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


import static org.assertj.core.api.Assertions.assertThat;
import static racingcar.RacingCar.racingCar;

public class RacingTest {


@Test
@DisplayName("자동차 대수 0")
void null_value() {
assertThat(racingCar(0)).isEqualTo("");
}
}
Copy link

Choose a reason for hiding this comment

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

테스트가 많이 부족합니다 🙇
만약에 테스트 작성이 어렵다면, 객체들의 역할과 책임이 분명하지 않거나, 비즈니스 로직과 UI로직이 잘 분리가 안되서 그럴 수 있을것 같아요 😄
이 부분 좀 더 개선 고민해주시면 좋을 것 같습니다 🙇