From ed7ba12b3913c2cf33eb2c52d61f215ebd96b7a6 Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:06:08 +0900 Subject: [PATCH 01/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=201=20-=20split=20=ED=96=88=EC=9D=84=20=EB=95=8C=20co?= =?UTF-8?q?ntains()=EB=A5=BC=20=ED=99=9C=EC=9A=A9=ED=95=B4=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=20=EA=B0=92=EC=9D=B4=20=EB=A7=9E=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=ED=95=98=EB=8A=94=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/empty.txt | 0 src/test/java/study/StringTest.java | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) delete mode 100644 src/test/java/empty.txt create mode 100644 src/test/java/study/StringTest.java diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java new file mode 100644 index 00000000..2d693085 --- /dev/null +++ b/src/test/java/study/StringTest.java @@ -0,0 +1,16 @@ +package study; + +import org.junit.jupiter.api.Test; +import org.assertj.core.api.Assertions; + +public class StringTest { + @Test + void split() { + String value = "1,2"; + String[] result = value.split(","); + Assertions.assertThat(result).contains("1"); + Assertions.assertThat(result).contains("2"); + String[] temp = {"1", "2"}; + Assertions.assertThat(result).containsExactly(temp); + } +} From b7f56bf9a5bd374ba05363f2b142c18c7276e3a0 Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:07:14 +0900 Subject: [PATCH 02/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=202=20-=20substring()=EC=9D=84=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=B4=20=EC=98=AC=EB=B0=94=EB=A5=B8=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=EC=9D=B4=20=EB=B0=98=ED=99=98=EB=90=98=EB=8A=94?= =?UTF-8?q?=EC=A7=80=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/StringTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 2d693085..abf3b3ca 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -13,4 +13,11 @@ void split() { String[] temp = {"1", "2"}; Assertions.assertThat(result).containsExactly(temp); } + + @Test + void substring() { + String value = "(1,2)"; + String result = value.substring(1, value.length() - 1); + Assertions.assertThat(result).contains("1,2"); + } } From 4646843ca489415647612e923adc25deb18d4744 Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:12:40 +0900 Subject: [PATCH 03/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=203=20-=20charAt()=20=EB=A9=94=EC=86=8C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=ED=99=9C=EC=9A=A9=ED=95=B4=20=EB=B2=94=EC=9C=84?= =?UTF-8?q?=EB=A5=BC=20=EB=84=98=EC=96=B4=EA=B0=80=EB=8A=94=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/StringTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index abf3b3ca..1bc3624f 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,5 +1,6 @@ package study; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.assertj.core.api.Assertions; @@ -20,4 +21,13 @@ void substring() { String result = value.substring(1, value.length() - 1); Assertions.assertThat(result).contains("1,2"); } + + @Test + @DisplayName("charAt method out of bounds test") + void charAtTest() { + String value = "abc"; + Assertions.assertThatThrownBy(() -> { + value.charAt(10); + }).isInstanceOf(StringIndexOutOfBoundsException.class).hasMessageContaining("String index out of range"); + } } From 3994dee828806d2f928db1a0af61ad9ac3746639 Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:15:00 +0900 Subject: [PATCH 04/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=201=20-=20size()=20=EB=A9=94=EC=86=8C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=ED=99=9C=EC=9A=A9=ED=95=B4=20Set=EC=9D=98=20?= =?UTF-8?q?=ED=81=AC=EA=B8=B0=EB=A5=BC=20=ED=99=95=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/test/java/study/SetTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 00000000..e1ce2518 --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,27 @@ +package study; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +public class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + void sizeTest() { + int size = numbers.size(); + Assertions.assertThat(size).isEqualTo(4); + } +} From db637c05e19bbdffec4bd4a97431a18305a7188d Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:16:38 +0900 Subject: [PATCH 05/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=202=20-=20JUnit=EC=9D=98=20ParameterizedTest=EB=A5=BC?= =?UTF-8?q?=20=EC=9D=B4=EC=9A=A9=ED=95=B4=20=EC=A4=91=EB=B3=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index e1ce2518..d1879ee7 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -3,10 +3,14 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.HashSet; import java.util.Set; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class SetTest { private Set numbers; @@ -24,4 +28,10 @@ void sizeTest() { int size = numbers.size(); Assertions.assertThat(size).isEqualTo(4); } + + @ParameterizedTest + @ValueSource(ints = {1, 1, 2, 3}) + void containTest(int number) { + assertTrue(numbers.contains(number)); + } } From 1da46a8dd7552d18c5831844fb40a0f671d9df40 Mon Sep 17 00:00:00 2001 From: giantim Date: Wed, 5 Feb 2020 15:17:26 +0900 Subject: [PATCH 06/17] =?UTF-8?q?add=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=203=20-=20@CsvSource=EB=A5=BC=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=B4=20=EC=9E=85=EB=A0=A5=20=EA=B0=92=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20=EA=B2=B0=EA=B3=BC=20=EA=B0=92=EC=9D=B4=20=EB=8B=A4?= =?UTF-8?q?=EB=A5=B8=20=EA=B2=BD=EC=9A=B0=EC=9D=98=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index d1879ee7..5414d654 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import java.util.HashSet; @@ -34,4 +35,10 @@ void sizeTest() { void containTest(int number) { assertTrue(numbers.contains(number)); } + + @ParameterizedTest + @CsvSource(value = {"ravie-RAVIE", "Lavine-LAVINE", "orange-ORANKE", "dog-DoG"}, delimiter = '-') + void equalUpperCase(String input, String upperCase){ + Assertions.assertThat(upperCase).isEqualTo(input.toUpperCase()); + } } From 43d67f664463d343125677c31613253e15430253 Mon Sep 17 00:00:00 2001 From: giantim Date: Fri, 7 Feb 2020 14:00:08 +0900 Subject: [PATCH 07/17] =?UTF-8?q?add=20:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 28 ++++++++ src/main/java/calculator/Input.java | 32 +++++++++ src/main/java/calculator/Main.java | 13 ++++ src/main/java/calculator/Output.java | 7 ++ src/main/java/calculator/Validator.java | 59 ++++++++++++++++ src/test/java/calculator/CalculatorTest.java | 74 ++++++++++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 src/main/java/calculator/Calculator.java create mode 100644 src/main/java/calculator/Input.java create mode 100644 src/main/java/calculator/Main.java create mode 100644 src/main/java/calculator/Output.java create mode 100644 src/main/java/calculator/Validator.java create mode 100644 src/test/java/calculator/CalculatorTest.java diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 00000000..9d8e92b3 --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,28 @@ +package calculator; + +public class Calculator { + public double calculate(String[] splitedInput) { + double left = Double.valueOf(splitedInput[0]); + for (int i = 1; i < splitedInput.length; i = i + 2) { + left = calculateByOperator(left, Double.valueOf(splitedInput[i + 1]), splitedInput[i]); + } + return left; + } + + private double calculateByOperator(double left, double right, String operator) { + double result = 0d; + if (operator.equals("+")) { + result = left + right; + } + if (operator.equals("-")) { + result = left - right; + } + if (operator.equals("*")) { + result = left * right; + } + if (operator.equals("/")) { + result = left / right; + } + return result; + } +} diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java new file mode 100644 index 00000000..b2df3ca9 --- /dev/null +++ b/src/main/java/calculator/Input.java @@ -0,0 +1,32 @@ +package calculator; + +import java.util.Scanner; + +public class Input { + private Validator validator = new Validator(); + private Scanner scanner = new Scanner(System.in); + + private String inputString() { + System.out.print("계산식을 입력하시오: "); + String inputLine = scanner.nextLine(); + try { + validator.isValidInput(inputLine); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return inputString(); + } + return inputLine; + } + + public String[] splitString() { + String input = inputString(); + String[] splitInput = input.split(" "); + try { + validator.isValidSplitedInput(splitInput); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return splitString(); + } + return splitInput; + } +} diff --git a/src/main/java/calculator/Main.java b/src/main/java/calculator/Main.java new file mode 100644 index 00000000..d0715fff --- /dev/null +++ b/src/main/java/calculator/Main.java @@ -0,0 +1,13 @@ +package calculator; + +public class Main { + public static void main(String[] args) { + Input input = new Input(); + Calculator calculator = new Calculator(); + Output output = new Output(); + + String[] splitString = input.splitString(); + double result = calculator.calculate(splitString); + output.printResult(result); + } +} diff --git a/src/main/java/calculator/Output.java b/src/main/java/calculator/Output.java new file mode 100644 index 00000000..578721a9 --- /dev/null +++ b/src/main/java/calculator/Output.java @@ -0,0 +1,7 @@ +package calculator; + +public class Output { + public void printResult(double result) { + System.out.println("결과 값은 : " + result); + } +} diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java new file mode 100644 index 00000000..5695ab38 --- /dev/null +++ b/src/main/java/calculator/Validator.java @@ -0,0 +1,59 @@ +package calculator; + +import java.util.ArrayList; + +public class Validator { + private static final ArrayList operators = new ArrayList<>(); + + public Validator() { + operators.add("+"); + operators.add("-"); + operators.add("*"); + operators.add("/"); + } + + public void isValidInput(String input) { + if (isNull(input) || isBlank(input) || isEmpty(input)) { + throw new IllegalArgumentException("Null or Blank or Empty exception."); + } + } + + private boolean isNull(String input) { + return input == null; + } + + private boolean isBlank(String input) { + return input.equals(" "); + } + + private boolean isEmpty(String input) { + return input.equals(""); + } + + public void isValidSplitedInput(String[] splitedInput) { + checkIsDoubleNumber(splitedInput[0]); + for (int i = 1; i < splitedInput.length; i = i + 2) { + String operator = splitedInput[i]; + String number = splitedInput[i + 1]; + if (operator.equals("/") && number.equals("0")) { + throw new IllegalArgumentException("Cannot divide zero."); + } + checkIsOperator(operator); + checkIsDoubleNumber(number); + } + } + + private void checkIsDoubleNumber(String s) { + try { + Double.valueOf(s); + } catch (Exception e) { + throw new IllegalArgumentException("Can not convert to double."); + } + } + + private void checkIsOperator(String s) { + if (!operators.contains(s)) { + throw new IllegalArgumentException("Operator check exception."); + } + } +} diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java new file mode 100644 index 00000000..00a50a34 --- /dev/null +++ b/src/test/java/calculator/CalculatorTest.java @@ -0,0 +1,74 @@ +package calculator; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +public class CalculatorTest { + private Calculator calculator; + private Validator validator; + + @BeforeEach + public void setUp() { + calculator = new Calculator(); + validator = new Validator(); + } + + static Stream notValidInputStrings() { + return Stream.of(" ", null, ""); + } + + static Stream notValidSplitedStrings() { + return Stream.of("q + w + e", "1+2+3", "+ + 2", "1 abcd 2", "2 * 65 / 0", "1 + 2 + 3"); + } + + @ParameterizedTest + @MethodSource("notValidInputStrings") + public void isValidInputTest(String notValidInputString) { + Assertions.assertThatThrownBy(() -> { + validator.isValidInput(notValidInputString); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Null or Blank or Empty exception."); + } + + @ParameterizedTest + @MethodSource("notValidSplitedStrings") + public void isValidSplitedInputTest(String notValidSplitedStrings) { + String[] splitData = notValidSplitedStrings.split(" "); + Assertions.assertThatThrownBy(() -> { + validator.isValidSplitedInput(splitData); + }).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void addTest() { + String[] numericalExpression = {"1", "+", "2"}; + Double result = calculator.calculate(numericalExpression); + Assertions.assertThat(result).isEqualTo(3); + } + + @Test + public void subtractTest() { + String[] numericalExpression = {"1", "-", "2"}; + Double result = calculator.calculate(numericalExpression); + Assertions.assertThat(result).isEqualTo(-1); + } + + @Test + public void multipleTest() { + String[] numericalExpression = {"1", "*", "2"}; + Double result = calculator.calculate(numericalExpression); + Assertions.assertThat(result).isEqualTo(2); + } + + @Test + public void divideTest() { + String[] numericalExpression = {"1", "/", "2"}; + Double result = calculator.calculate(numericalExpression); + Assertions.assertThat(result).isEqualTo(0.5d); + } +} From 75191ca14129c35617db5bb518f6db911d3fc6cf Mon Sep 17 00:00:00 2001 From: giantim Date: Fri, 7 Feb 2020 14:02:07 +0900 Subject: [PATCH 08/17] =?UTF-8?q?add=20:=20README=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d742d97..c6bb9932 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,39 @@ # java-calculator 문자열 계산기 미션 저장소 -## 우아한테크코스 코드리뷰 -* [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) \ No newline at end of file +# java-calculator +문자열 계산기 미션 저장소 + +## 기능적 요구사항 +- 문자열을 입력 받아 공백 단위로 split 하는 함수를 만든다 + - 입력이 정상적인지 확인 + - 입력이 없는지 + - 숫자와 연산자 외의 다른 문자가 있는지 + - split 된 결과가 정상적인지 확인 + - 숫자와 연산자의 순서가 맞는지 + +- 사칙연산을 하는 함수를 만든다 + - 나눗셈의 경우 0으로 나누는 경우에 대한 예외 처리 + - 입력 값이 자료형의 범위를 넘어 가는 경우에 대한 예외 처리 + +- split 된 결과 값이 순서대로 연산이 되는 함수를 만든다 + +- 연산 결과를 출력하는 함수를 만든다 + +- 단위별로 테스트 하는 함수를 만든다 + - split 테스트를 진행한다 + - 입력으로 null, 공백, 유효하지 않은 double 범위의 수, 숫자와 연산자 외의 문자열 + - split 할 수 없는 문자열, 숫자와 연산자 순서가 바뀐 문자열, 정상 계산식 + - 사칙연산 테스트 + - 사칙연산의 결과 값이 실제 값과 맞는지 테스트 + - 0으로 나누었을 때, double 범위를 넘어가는 결과 값 + + +## 비기능적 요구사항 +- indent 2까지 허용한다. +- 메소드는 15줄 까지 허용한다. +- switch문 사용하지 않는다. +- 하드코딩하지 않는다. +- 변수와 메소드 명명은 컨벤션에 따른다. +- 자바 코드 컨벤션을 지키면서 프로그래밍 한다. +- 함수의 인자 수를 3개까지만 허용한다. \ No newline at end of file From f5b3b85f97bebb744e343b6fc90f8a7f31594458 Mon Sep 17 00:00:00 2001 From: giantim <39546083+giantim@users.noreply.github.com> Date: Fri, 7 Feb 2020 15:02:27 +0900 Subject: [PATCH 09/17] Update README.md --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index c6bb9932..946f95cf 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # java-calculator 문자열 계산기 미션 저장소 -# java-calculator -문자열 계산기 미션 저장소 - ## 기능적 요구사항 - 문자열을 입력 받아 공백 단위로 split 하는 함수를 만든다 - 입력이 정상적인지 확인 @@ -36,4 +33,4 @@ - 하드코딩하지 않는다. - 변수와 메소드 명명은 컨벤션에 따른다. - 자바 코드 컨벤션을 지키면서 프로그래밍 한다. -- 함수의 인자 수를 3개까지만 허용한다. \ No newline at end of file +- 함수의 인자 수를 3개까지만 허용한다. From 19caade35ce105269064c107e135fbdc399f0879 Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 19:57:59 +0900 Subject: [PATCH 10/17] =?UTF-8?q?refact=20:=20operator=EB=A5=BC=20enum=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EC=96=B4=EC=84=9C=20=EA=B4=80=EB=A6=AC=20/=20Validator=20->=20?= =?UTF-8?q?ValidityInspector=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Input.java | 6 +++--- src/main/java/calculator/Operator.java | 15 +++++++++++++++ .../{Validator.java => ValidityInspector.java} | 16 +++++++++------- src/test/java/calculator/CalculatorTest.java | 8 ++++---- 4 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 src/main/java/calculator/Operator.java rename src/main/java/calculator/{Validator.java => ValidityInspector.java} (79%) diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java index b2df3ca9..b9eb6d58 100644 --- a/src/main/java/calculator/Input.java +++ b/src/main/java/calculator/Input.java @@ -3,14 +3,14 @@ import java.util.Scanner; public class Input { - private Validator validator = new Validator(); + private ValidityInspector validityInspector = new ValidityInspector(); private Scanner scanner = new Scanner(System.in); private String inputString() { System.out.print("계산식을 입력하시오: "); String inputLine = scanner.nextLine(); try { - validator.isValidInput(inputLine); + validityInspector.isValidInput(inputLine); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); return inputString(); @@ -22,7 +22,7 @@ public String[] splitString() { String input = inputString(); String[] splitInput = input.split(" "); try { - validator.isValidSplitedInput(splitInput); + validityInspector.isValidSplitedInput(splitInput); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); return splitString(); diff --git a/src/main/java/calculator/Operator.java b/src/main/java/calculator/Operator.java new file mode 100644 index 00000000..0457f181 --- /dev/null +++ b/src/main/java/calculator/Operator.java @@ -0,0 +1,15 @@ +package calculator; + +public enum Operator { + Plus("+"), Minus("-"), Multiplication("*"), Division("/"); + + private String symbol; + + Operator(String symbol) { + this.symbol = symbol; + } + + public String getValue() { + return this.symbol; + } +} diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/ValidityInspector.java similarity index 79% rename from src/main/java/calculator/Validator.java rename to src/main/java/calculator/ValidityInspector.java index 5695ab38..e14cc278 100644 --- a/src/main/java/calculator/Validator.java +++ b/src/main/java/calculator/ValidityInspector.java @@ -1,15 +1,17 @@ package calculator; import java.util.ArrayList; +import java.util.List; -public class Validator { - private static final ArrayList operators = new ArrayList<>(); +public class ValidityInspector { + private List operators; - public Validator() { - operators.add("+"); - operators.add("-"); - operators.add("*"); - operators.add("/"); + ValidityInspector() { + operators = new ArrayList<>(); + operators.add(Operator.Plus.getValue()); + operators.add(Operator.Minus.getValue()); + operators.add(Operator.Multiplication.getValue()); + operators.add(Operator.Division.getValue()); } public void isValidInput(String input) { diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index 00a50a34..b9ff6a28 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -10,12 +10,12 @@ public class CalculatorTest { private Calculator calculator; - private Validator validator; + private ValidityInspector validityInspector; @BeforeEach public void setUp() { calculator = new Calculator(); - validator = new Validator(); + validityInspector = new ValidityInspector(); } static Stream notValidInputStrings() { @@ -30,7 +30,7 @@ static Stream notValidSplitedStrings() { @MethodSource("notValidInputStrings") public void isValidInputTest(String notValidInputString) { Assertions.assertThatThrownBy(() -> { - validator.isValidInput(notValidInputString); + validityInspector.isValidInput(notValidInputString); }).isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Null or Blank or Empty exception."); } @@ -40,7 +40,7 @@ public void isValidInputTest(String notValidInputString) { public void isValidSplitedInputTest(String notValidSplitedStrings) { String[] splitData = notValidSplitedStrings.split(" "); Assertions.assertThatThrownBy(() -> { - validator.isValidSplitedInput(splitData); + validityInspector.isValidSplitedInput(splitData); }).isInstanceOf(IllegalArgumentException.class); } From 006eded08eccb3ee2c8c273f2bff98d2a869cd1e Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 20:04:04 +0900 Subject: [PATCH 11/17] =?UTF-8?q?refact=20:=20if=20=EB=AC=B8=EC=9D=98=20?= =?UTF-8?q?=EB=82=98=EC=97=B4=20->=20if=20~=20else=20if=20=EB=AC=B8?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20/=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=EB=AC=B8=EC=9D=98=20=EB=8B=A8=EA=B3=84=EB=B3=84?= =?UTF-8?q?=EB=A1=9C=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 9d8e92b3..1d0dd0be 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -10,19 +10,15 @@ public double calculate(String[] splitedInput) { } private double calculateByOperator(double left, double right, String operator) { - double result = 0d; - if (operator.equals("+")) { - result = left + right; + if (Operator.Plus.getValue().equals(operator)) { + return left + right; + } else if (Operator.Minus.getValue().equals(operator)) { + return left - right; + } else if (Operator.Multiplication.getValue().equals(operator)) { + return left * right; + } else if (Operator.Division.getValue().equals(operator)) { + return left / right; } - if (operator.equals("-")) { - result = left - right; - } - if (operator.equals("*")) { - result = left * right; - } - if (operator.equals("/")) { - result = left / right; - } - return result; + return 0d; } } From 470960aac474a314778b0a9324b7423cf0bdb479 Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 20:14:04 +0900 Subject: [PATCH 12/17] =?UTF-8?q?refact=20:=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=98=20=EA=B3=B5=EB=B0=B1=20?= =?UTF-8?q?=EB=98=90=EB=8A=94=20null=20=EA=B2=80=EC=82=AC=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=A5=BC=20=ED=95=98=EB=82=98=EB=A1=9C=20?= =?UTF-8?q?=ED=86=B5=EC=9D=BC=20/=20=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0=20/=20checkIsDoubleNumber=20->=20checkCor?= =?UTF-8?q?rectDoubleNumber,=20checkIsOperator=20->=20checkCorrectOperator?= =?UTF-8?q?=20=EB=A1=9C=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/calculator/ValidityInspector.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/calculator/ValidityInspector.java b/src/main/java/calculator/ValidityInspector.java index e14cc278..3f27be61 100644 --- a/src/main/java/calculator/ValidityInspector.java +++ b/src/main/java/calculator/ValidityInspector.java @@ -15,37 +15,34 @@ public class ValidityInspector { } public void isValidInput(String input) { - if (isNull(input) || isBlank(input) || isEmpty(input)) { + if (isBlank(input) || isNullOrEmpty(input)) { throw new IllegalArgumentException("Null or Blank or Empty exception."); } } - private boolean isNull(String input) { - return input == null; + private boolean isNullOrEmpty(String input) { + return input.equals(""); } private boolean isBlank(String input) { return input.equals(" "); } - private boolean isEmpty(String input) { - return input.equals(""); - } - public void isValidSplitedInput(String[] splitedInput) { - checkIsDoubleNumber(splitedInput[0]); + String firstClause = splitedInput[0]; + checkCorrectDoubleNumber(firstClause); for (int i = 1; i < splitedInput.length; i = i + 2) { String operator = splitedInput[i]; - String number = splitedInput[i + 1]; - if (operator.equals("/") && number.equals("0")) { + String secondClause = splitedInput[i + 1]; + if (Operator.Division.getValue().equals(operator) && "0".equals(secondClause)) { throw new IllegalArgumentException("Cannot divide zero."); } - checkIsOperator(operator); - checkIsDoubleNumber(number); + checkCorrectOperator(operator); + checkCorrectDoubleNumber(secondClause); } } - private void checkIsDoubleNumber(String s) { + private void checkCorrectDoubleNumber(String s) { try { Double.valueOf(s); } catch (Exception e) { @@ -53,7 +50,7 @@ private void checkIsDoubleNumber(String s) { } } - private void checkIsOperator(String s) { + private void checkCorrectOperator(String s) { if (!operators.contains(s)) { throw new IllegalArgumentException("Operator check exception."); } From b3fbab4348e4ca50281e77decb5f1c47a13d07f0 Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 20:19:27 +0900 Subject: [PATCH 13/17] =?UTF-8?q?modify=20:=20gitignore=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 76611b07..8e0b9acb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,10 @@ !gradle/wrapper/gradle-wrapper.jar /out/ /target/ +/src/main/java/nickname.txt +/src/main/java/study +/src/test/java/study/CabenetTest.java + ### STS ### .apt_generated From 1985f2b787c505e196c177f16e5e74953512aa3c Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 21:02:46 +0900 Subject: [PATCH 14/17] =?UTF-8?q?refact=20:=20call=20stack=20=EC=9D=B4=20?= =?UTF-8?q?=EC=8C=93=EC=9D=B4=EB=8A=94=20=EA=B2=83=EC=9D=84=20=EB=B0=A9?= =?UTF-8?q?=EC=A7=80=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=B4=20=EC=9E=98?= =?UTF-8?q?=EB=AA=BB=EB=90=9C=20=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EB=90=A0=20=EC=8B=9C=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=EA=B8=B0=EC=9D=98=20=EA=B8=B0=EB=8A=A5=EC=9D=B4=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=EB=90=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=20/?= =?UTF-8?q?=20Validator=20=EC=97=90=EC=84=9C=20is=20=EB=A1=9C=20=EC=8B=9C?= =?UTF-8?q?=EC=9E=91=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD=20/=20=EA=B3=B5=EB=B0=B1?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=EB=A7=8C=20=EC=9D=B4=EB=A3=A8=EC=96=B4?= =?UTF-8?q?=EC=A7=84=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EC=B6=9C=EB=A0=A5=20/=20Input?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=EC=9D=98=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EC=9D=84=20=EB=AA=85=ED=99=95=ED=9E=88=20=EC=84=A4=EB=AA=85?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=9D=B4=EB=A6=84=EA=B3=BC=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Input.java | 32 ------------------- src/main/java/calculator/Main.java | 12 ++++--- .../java/calculator/UserInputScanner.java | 22 +++++++++++++ .../java/calculator/ValidityInspector.java | 23 ++++++++----- 4 files changed, 45 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/calculator/Input.java create mode 100644 src/main/java/calculator/UserInputScanner.java diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java deleted file mode 100644 index b9eb6d58..00000000 --- a/src/main/java/calculator/Input.java +++ /dev/null @@ -1,32 +0,0 @@ -package calculator; - -import java.util.Scanner; - -public class Input { - private ValidityInspector validityInspector = new ValidityInspector(); - private Scanner scanner = new Scanner(System.in); - - private String inputString() { - System.out.print("계산식을 입력하시오: "); - String inputLine = scanner.nextLine(); - try { - validityInspector.isValidInput(inputLine); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - return inputString(); - } - return inputLine; - } - - public String[] splitString() { - String input = inputString(); - String[] splitInput = input.split(" "); - try { - validityInspector.isValidSplitedInput(splitInput); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - return splitString(); - } - return splitInput; - } -} diff --git a/src/main/java/calculator/Main.java b/src/main/java/calculator/Main.java index d0715fff..0c46a59c 100644 --- a/src/main/java/calculator/Main.java +++ b/src/main/java/calculator/Main.java @@ -2,12 +2,16 @@ public class Main { public static void main(String[] args) { - Input input = new Input(); + UserInputScanner userInputScanner = new UserInputScanner(); Calculator calculator = new Calculator(); Output output = new Output(); - String[] splitString = input.splitString(); - double result = calculator.calculate(splitString); - output.printResult(result); + try { + String[] splitString = userInputScanner.splitUserInputString(); + double result = calculator.calculate(splitString); + output.printResult(result); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } } } diff --git a/src/main/java/calculator/UserInputScanner.java b/src/main/java/calculator/UserInputScanner.java new file mode 100644 index 00000000..ded8d47f --- /dev/null +++ b/src/main/java/calculator/UserInputScanner.java @@ -0,0 +1,22 @@ +package calculator; + +import java.util.Scanner; + +public class UserInputScanner { + private ValidityInspector validityInspector = new ValidityInspector(); + private Scanner scanner = new Scanner(System.in); + + private String inputStringToUser() { + System.out.print("계산식을 입력하시오: "); + String inputLine = scanner.nextLine(); + validityInspector.checkUserInputIsBlankOrEmpty(inputLine); + return inputLine; + } + + public String[] splitUserInputString() { + String input = inputStringToUser(); + String[] splitInput = input.split(" "); + validityInspector.checkCanConvertUserInputToNumberAndOperator(splitInput); + return splitInput; + } +} diff --git a/src/main/java/calculator/ValidityInspector.java b/src/main/java/calculator/ValidityInspector.java index 3f27be61..8a64efa9 100644 --- a/src/main/java/calculator/ValidityInspector.java +++ b/src/main/java/calculator/ValidityInspector.java @@ -14,13 +14,13 @@ public class ValidityInspector { operators.add(Operator.Division.getValue()); } - public void isValidInput(String input) { - if (isBlank(input) || isNullOrEmpty(input)) { - throw new IllegalArgumentException("Null or Blank or Empty exception."); + public void checkUserInputIsBlankOrEmpty(String input) { + if (isBlank(input) || isEmpty(input)) { + throw new IllegalArgumentException("공백 또는 빈 문자열을 입력하셨습니다."); } } - private boolean isNullOrEmpty(String input) { + private boolean isEmpty(String input) { return input.equals(""); } @@ -28,14 +28,15 @@ private boolean isBlank(String input) { return input.equals(" "); } - public void isValidSplitedInput(String[] splitedInput) { + public void checkCanConvertUserInputToNumberAndOperator(String[] splitedInput) { + checkSplitedInputEmpty(splitedInput); String firstClause = splitedInput[0]; checkCorrectDoubleNumber(firstClause); for (int i = 1; i < splitedInput.length; i = i + 2) { String operator = splitedInput[i]; String secondClause = splitedInput[i + 1]; if (Operator.Division.getValue().equals(operator) && "0".equals(secondClause)) { - throw new IllegalArgumentException("Cannot divide zero."); + throw new IllegalArgumentException("0으로 나누는 식을 입력하셨습니다."); } checkCorrectOperator(operator); checkCorrectDoubleNumber(secondClause); @@ -46,13 +47,19 @@ private void checkCorrectDoubleNumber(String s) { try { Double.valueOf(s); } catch (Exception e) { - throw new IllegalArgumentException("Can not convert to double."); + throw new IllegalArgumentException("잘못된 수를 입력하였습니다."); } } private void checkCorrectOperator(String s) { if (!operators.contains(s)) { - throw new IllegalArgumentException("Operator check exception."); + throw new IllegalArgumentException("잘못된 연산자를 입력하였습니다."); + } + } + + private void checkSplitedInputEmpty(String[] splitedInput) { + if (splitedInput.length == 0) { + throw new IllegalArgumentException("공백을 입력하였습니다."); } } } From 061557d5e0bf4e276cbd18867837e5390a57e9b2 Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 21:08:06 +0900 Subject: [PATCH 15/17] =?UTF-8?q?refact=20:=20=EC=97=AC=EB=9F=AC=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20=EA=B3=B5?= =?UTF-8?q?=ED=86=B5=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8A=94=200,=20=EA=B3=B5=EB=B0=B1,=20=EB=B9=88=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=84=20=EC=83=81=EC=88=98=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Constant.java | 8 ++++++++ src/main/java/calculator/UserInputScanner.java | 2 +- src/main/java/calculator/ValidityInspector.java | 6 +++--- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 src/main/java/calculator/Constant.java diff --git a/src/main/java/calculator/Constant.java b/src/main/java/calculator/Constant.java new file mode 100644 index 00000000..f42e493c --- /dev/null +++ b/src/main/java/calculator/Constant.java @@ -0,0 +1,8 @@ +package calculator; + +public class Constant { + public final static int ZERO = 0; + + public final static String BLANK = " "; + public final static String EMPTY_STRING = ""; +} diff --git a/src/main/java/calculator/UserInputScanner.java b/src/main/java/calculator/UserInputScanner.java index ded8d47f..b55ec451 100644 --- a/src/main/java/calculator/UserInputScanner.java +++ b/src/main/java/calculator/UserInputScanner.java @@ -15,7 +15,7 @@ private String inputStringToUser() { public String[] splitUserInputString() { String input = inputStringToUser(); - String[] splitInput = input.split(" "); + String[] splitInput = input.split(Constant.BLANK); validityInspector.checkCanConvertUserInputToNumberAndOperator(splitInput); return splitInput; } diff --git a/src/main/java/calculator/ValidityInspector.java b/src/main/java/calculator/ValidityInspector.java index 8a64efa9..9d6936da 100644 --- a/src/main/java/calculator/ValidityInspector.java +++ b/src/main/java/calculator/ValidityInspector.java @@ -21,11 +21,11 @@ public void checkUserInputIsBlankOrEmpty(String input) { } private boolean isEmpty(String input) { - return input.equals(""); + return input.equals(Constant.EMPTY_STRING); } private boolean isBlank(String input) { - return input.equals(" "); + return input.equals(Constant.BLANK); } public void checkCanConvertUserInputToNumberAndOperator(String[] splitedInput) { @@ -35,7 +35,7 @@ public void checkCanConvertUserInputToNumberAndOperator(String[] splitedInput) { for (int i = 1; i < splitedInput.length; i = i + 2) { String operator = splitedInput[i]; String secondClause = splitedInput[i + 1]; - if (Operator.Division.getValue().equals(operator) && "0".equals(secondClause)) { + if (Operator.Division.getValue().equals(operator) && Integer.toString(Constant.ZERO).equals(secondClause)) { throw new IllegalArgumentException("0으로 나누는 식을 입력하셨습니다."); } checkCorrectOperator(operator); From 9d2cac89a556c20809cfe80bc25155c11aa2a348 Mon Sep 17 00:00:00 2001 From: giantim Date: Sat, 8 Feb 2020 21:15:18 +0900 Subject: [PATCH 16/17] =?UTF-8?q?refact=20:=20=EB=B3=80=EA=B2=BD=ED=95=9C?= =?UTF-8?q?=20=EB=A9=94=EC=86=8C=EB=93=9C=EC=97=90=20=EB=A7=9E=EA=B2=8C=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20/=20MethodSource=20->=20ValueSource=20=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/calculator/CalculatorTest.java | 28 +++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index b9ff6a28..cd3f1090 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -4,9 +4,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.stream.Stream; +import org.junit.jupiter.params.provider.ValueSource; public class CalculatorTest { private Calculator calculator; @@ -18,29 +16,21 @@ public void setUp() { validityInspector = new ValidityInspector(); } - static Stream notValidInputStrings() { - return Stream.of(" ", null, ""); - } - - static Stream notValidSplitedStrings() { - return Stream.of("q + w + e", "1+2+3", "+ + 2", "1 abcd 2", "2 * 65 / 0", "1 + 2 + 3"); - } - @ParameterizedTest - @MethodSource("notValidInputStrings") - public void isValidInputTest(String notValidInputString) { + @ValueSource(strings = {" ", ""}) + public void checkUserInputIsBlankOrEmptyTest(String input) { Assertions.assertThatThrownBy(() -> { - validityInspector.isValidInput(notValidInputString); + validityInspector.checkUserInputIsBlankOrEmpty(input); }).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Null or Blank or Empty exception."); + .hasMessageContaining("공백 또는 빈 문자열을 입력하셨습니다."); } @ParameterizedTest - @MethodSource("notValidSplitedStrings") - public void isValidSplitedInputTest(String notValidSplitedStrings) { - String[] splitData = notValidSplitedStrings.split(" "); + @ValueSource(strings = {" ", "q + w + e", "1+2+3", "+ + 2", "1 abcd 2", "2 * 65 / 0"}) + public void checkCanConvertUserInputToNumberAndOperatorTest(String input) { + String[] splitData = input.split(Constant.BLANK); Assertions.assertThatThrownBy(() -> { - validityInspector.isValidSplitedInput(splitData); + validityInspector.checkCanConvertUserInputToNumberAndOperator(splitData); }).isInstanceOf(IllegalArgumentException.class); } From 4f46d3030a2e3bd1414629a7212d5a2061e93a0c Mon Sep 17 00:00:00 2001 From: giantim Date: Mon, 10 Feb 2020 00:29:26 +0900 Subject: [PATCH 17/17] =?UTF-8?q?refact=20:=20Operator=20=EB=A5=BC=20?= =?UTF-8?q?=EC=BB=A8=EB=B2=A4=EC=85=98=EC=97=90=20=EB=A7=9E=EA=B2=8C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95=20/=20Operator.find()?= =?UTF-8?q?=20=EC=99=80=20Operator.calculate()=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EB=A7=8C=EB=93=A4=EC=96=B4=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=ED=95=9C=20=EC=97=B0=EC=82=B0=EC=9E=90=EC=99=80=20?= =?UTF-8?q?=EC=A2=8C=ED=95=AD,=20=EC=9A=B0=ED=95=AD=EC=9D=84=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=ED=95=98=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 17 ++------- src/main/java/calculator/Main.java | 2 +- src/main/java/calculator/Operator.java | 38 +++++++++++++++++-- .../java/calculator/ValidityInspector.java | 37 ++++++++---------- src/test/java/calculator/CalculatorTest.java | 2 +- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 1d0dd0be..bc7cf61a 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -4,21 +4,10 @@ public class Calculator { public double calculate(String[] splitedInput) { double left = Double.valueOf(splitedInput[0]); for (int i = 1; i < splitedInput.length; i = i + 2) { - left = calculateByOperator(left, Double.valueOf(splitedInput[i + 1]), splitedInput[i]); + String operator = splitedInput[i]; + double right = Double.valueOf(splitedInput[i + 1]); + left = Operator.find(operator).calculate(left, right); } return left; } - - private double calculateByOperator(double left, double right, String operator) { - if (Operator.Plus.getValue().equals(operator)) { - return left + right; - } else if (Operator.Minus.getValue().equals(operator)) { - return left - right; - } else if (Operator.Multiplication.getValue().equals(operator)) { - return left * right; - } else if (Operator.Division.getValue().equals(operator)) { - return left / right; - } - return 0d; - } } diff --git a/src/main/java/calculator/Main.java b/src/main/java/calculator/Main.java index 0c46a59c..6885e51f 100644 --- a/src/main/java/calculator/Main.java +++ b/src/main/java/calculator/Main.java @@ -10,7 +10,7 @@ public static void main(String[] args) { String[] splitString = userInputScanner.splitUserInputString(); double result = calculator.calculate(splitString); output.printResult(result); - } catch (IllegalArgumentException e) { + } catch (Exception e) { System.out.println(e.getMessage()); } } diff --git a/src/main/java/calculator/Operator.java b/src/main/java/calculator/Operator.java index 0457f181..e9eb8099 100644 --- a/src/main/java/calculator/Operator.java +++ b/src/main/java/calculator/Operator.java @@ -1,15 +1,47 @@ package calculator; +import java.util.Arrays; +import java.util.function.BiFunction; + public enum Operator { - Plus("+"), Minus("-"), Multiplication("*"), Division("/"); + PLUS("+", + (left, right) -> (left + right)), + MINUS("-", + (left, right) -> (left - right)), + MULTIPLICATION("*", + (left, right) -> (left * right)), + DIVISION("/", + (left, right) -> (left / right)); private String symbol; + private BiFunction expression; - Operator(String symbol) { + Operator(String symbol, BiFunction expression) { this.symbol = symbol; + this.expression = expression; } - public String getValue() { + private String getSymbol() { return this.symbol; } + + public static Operator find(String operator) { + return Arrays.stream(Operator.values()) + .filter(op -> op.getSymbol().equals(operator)) + .findFirst() + .get(); + } + + public double calculate(double left, double right) { + return expression.apply(left, right); + } + + public static boolean isRightOperator(String operator) { + return Arrays.stream(Operator.values()) + .anyMatch(op -> op.getSymbol().equals(operator)); + } + + public static boolean isDivisionOperator(String operator) { + return "/".equals(operator); + } } diff --git a/src/main/java/calculator/ValidityInspector.java b/src/main/java/calculator/ValidityInspector.java index 9d6936da..f2442ccc 100644 --- a/src/main/java/calculator/ValidityInspector.java +++ b/src/main/java/calculator/ValidityInspector.java @@ -1,19 +1,6 @@ package calculator; -import java.util.ArrayList; -import java.util.List; - public class ValidityInspector { - private List operators; - - ValidityInspector() { - operators = new ArrayList<>(); - operators.add(Operator.Plus.getValue()); - operators.add(Operator.Minus.getValue()); - operators.add(Operator.Multiplication.getValue()); - operators.add(Operator.Division.getValue()); - } - public void checkUserInputIsBlankOrEmpty(String input) { if (isBlank(input) || isEmpty(input)) { throw new IllegalArgumentException("공백 또는 빈 문자열을 입력하셨습니다."); @@ -32,14 +19,16 @@ public void checkCanConvertUserInputToNumberAndOperator(String[] splitedInput) { checkSplitedInputEmpty(splitedInput); String firstClause = splitedInput[0]; checkCorrectDoubleNumber(firstClause); - for (int i = 1; i < splitedInput.length; i = i + 2) { - String operator = splitedInput[i]; - String secondClause = splitedInput[i + 1]; - if (Operator.Division.getValue().equals(operator) && Integer.toString(Constant.ZERO).equals(secondClause)) { - throw new IllegalArgumentException("0으로 나누는 식을 입력하셨습니다."); + try { + for (int i = 1; i < splitedInput.length; i = i + 2) { + String operator = splitedInput[i]; + String secondClause = splitedInput[i + 1]; + checkDivideByZero(operator, secondClause); + checkCorrectOperator(operator); + checkCorrectDoubleNumber(secondClause); } - checkCorrectOperator(operator); - checkCorrectDoubleNumber(secondClause); + } catch (ArrayIndexOutOfBoundsException e) { + throw new ArrayIndexOutOfBoundsException("잘못된 계산식을 입력하였습니다."); } } @@ -52,7 +41,7 @@ private void checkCorrectDoubleNumber(String s) { } private void checkCorrectOperator(String s) { - if (!operators.contains(s)) { + if (!Operator.isRightOperator(s)) { throw new IllegalArgumentException("잘못된 연산자를 입력하였습니다."); } } @@ -62,4 +51,10 @@ private void checkSplitedInputEmpty(String[] splitedInput) { throw new IllegalArgumentException("공백을 입력하였습니다."); } } + + private void checkDivideByZero(String operator, String secondClause) { + if (Operator.isDivisionOperator(operator) && Integer.toString(Constant.ZERO).equals(secondClause)) { + throw new IllegalArgumentException("0으로 나누는 식을 입력하셨습니다."); + } + } } diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index cd3f1090..c7599cb9 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -26,7 +26,7 @@ public void checkUserInputIsBlankOrEmptyTest(String input) { } @ParameterizedTest - @ValueSource(strings = {" ", "q + w + e", "1+2+3", "+ + 2", "1 abcd 2", "2 * 65 / 0"}) + @ValueSource(strings = {" ", "q + w + e", "1+2+3", "+ + 2", "1 abcd 2", "2 * 65 / 0", "2 + 3/", "/1 + 55", "1 + a3"}) public void checkCanConvertUserInputToNumberAndOperatorTest(String input) { String[] splitData = input.split(Constant.BLANK); Assertions.assertThatThrownBy(() -> {