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

Step1 string addition calculator #2

Open
wants to merge 8 commits into
base: woomin-Jeon
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
21 changes: 21 additions & 0 deletions src/main/java/calculator/controller/StringCalculatorApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package calculator.controller;

import calculator.model.Calculator;
import calculator.model.StringReader;
import calculator.view.Input;
import calculator.view.Result;

public class StringCalculatorApp {
public static void main(String[] args) {
Result.showStartMessage();
String userInput = Input.getUserInput();

System.out.println(userInput);

StringReader stringReader = new StringReader();
int[] arr = stringReader.read(userInput);

Calculator calculator = new Calculator(arr);
Result.showResult(calculator.executeAddition());
}
}
24 changes: 24 additions & 0 deletions src/main/java/calculator/model/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package calculator.model;

import java.util.Arrays;

public class Calculator {
private final int result;

public Calculator(int[] numbers) {
int minusCount = Arrays.stream(numbers).filter(v -> v < 0).toArray().length;
if (minusCount > 0) {
throw new RuntimeException();
}

this.result = Arrays.stream(numbers).reduce(Integer::sum).orElse(0);
}

public static Calculator newCalculator(int[] numbers) {
return new Calculator(numbers);
}

public int executeAddition() {
return this.result;
}
}
27 changes: 27 additions & 0 deletions src/main/java/calculator/model/StringReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package calculator.model;

import java.util.Arrays;

public class StringReader {
public int[] read(String str) {
String[] arr = str.split("n");

return arr.length == 1
? withNoCustomSeparator(arr[0])
: withCustomSeparator(arr[0], arr[1]);
}

private int[] withNoCustomSeparator(String str) {
String[] numbers = str.split("[:,]");

return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}

private int[] withCustomSeparator(String separatorPart, String numberPart) {
String customSeparator = Character.toString(separatorPart.charAt(2));
String regex = "[:," + customSeparator + "]";
String[] numbers = numberPart.split(regex);

return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}
}
11 changes: 11 additions & 0 deletions src/main/java/calculator/view/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package calculator.view;

import java.util.Scanner;

public class Input {

public static String getUserInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
}
12 changes: 12 additions & 0 deletions src/main/java/calculator/view/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package calculator.view;

public class Result {

public static void showStartMessage() {
System.out.println("숫자들을 입력해주세요");
}

public static void showResult(int result) {
System.out.printf("합계: %d", result);
}
}
Empty file removed src/main/java/empty.txt
Empty file.
46 changes: 46 additions & 0 deletions src/test/java/calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package calculator;

import calculator.model.Calculator;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class CalculatorTest {
@Test
@DisplayName("with zero parameter")
public void nullParameter() {
Calculator calculator = Calculator.newCalculator(new int[] { 0 });

int answer = calculator.executeAddition();

assertThat(answer).isEqualTo(0);
}

@Test
@DisplayName("with only one number parameter")
public void oneParameter() {
Calculator calculator = Calculator.newCalculator(new int[] { 3 });

int answer = calculator.executeAddition();

assertThat(answer).isEqualTo(3);
}

@Test
@DisplayName("with array parameter")
public void arrayParameter() {
Calculator calculator = Calculator.newCalculator(new int[] { 1, 2, 3, 4 });

int answer = calculator.executeAddition();

assertThat(answer).isEqualTo(10);
}

@Test
@DisplayName("with minus parameter")
public void minusParameter() {
assertThatThrownBy(() -> Calculator.newCalculator(new int[] { -5 })).isInstanceOf(RuntimeException.class);
}
}
36 changes: 36 additions & 0 deletions src/test/java/calculator/StringReaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package calculator;

import calculator.model.StringReader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class StringReaderTest {

StringReader stringReader;

@BeforeEach
void init() {
stringReader = new StringReader();
}

@Test
@DisplayName("with multiple numbers parameter")
public void multipleNumbers() {
assertThat(stringReader.read("1,2,3,4")).isEqualTo(new int[] { 1, 2, 3, 4 });
}

@Test
@DisplayName("with only one number parameter")
public void onlyOneNumber() {
assertThat(stringReader.read("5")).isEqualTo(new int[] { 5 });
}

@Test
@DisplayName("with custom separator")
public void withSeparator() {
assertThat(stringReader.read("//;\\n1:2;3,4")).isEqualTo(new int[] { 1, 2, 3, 4 });
}
}
Empty file removed src/test/java/empty.txt
Empty file.