From cde694a489bc99b0541b9dac40cd9c1d77d3bd17 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Wed, 24 Feb 2021 20:56:28 +0900 Subject: [PATCH 1/7] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index e79684b..8e72353 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,33 @@ --- ## 힌트 + - 문자열을 입력 받은 후(scanner의 nextLine() 메소드 활용) 빈 공백 문자열을 기준으로 문자들을 분리해야 한다. + ```java String value = scanner.nextLine(); String[] values = value.split(" "); ``` + - 문자열을 숫자로 변경하는 방법 `int number = Integer.parseInt("문자열");` + +--- + +### 기능 + +- 예외 + - 연산자나 숫자가 두 번 이상 연속으로 나올 경우 + - 숫자와 연산자 이외의 문자 + - 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) +- 순서 + - 문자열 입력 받기 + - 예외 처리 + - 띄어쓰기로 분리해서 배열에 넣기 + - 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 + - 계산하기 + +### 예제 + +2 + 4 / 3 20 + 4932 * 1 1 + - 9 4 & 2 9/3 From b8146b8c622542ea189e487d7b5c525cd0857673 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Wed, 24 Feb 2021 21:42:41 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20Calculator=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/calculator/Calculator.java diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 0000000..27c96e8 --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,42 @@ +package calculator; + +import java.util.List; + +public class Calculator { + + public double calculate(List operands, List operators) { + double result = operands.get(0); + int numOfOperations = operators.size(); + int times = 0; + + while (times < numOfOperations) { + result = fourFundamentalArithmeticOperations(result, operators.get(times), operands.get(++times)); + } + + return result; + } + + public double fourFundamentalArithmeticOperations(double tempResult, char operator, double operand) { + double result; + + switch (operator) { + case '+': + result = tempResult + operand; + break; + case '-': + result = tempResult - operand; + break; + case '*': + result = tempResult * operand; + break; + case '/': + result = tempResult / operand; + break; + default: + result = 0; + break; + } + + return result; + } +} From 858f32d0ef84c66c4d28a3b23e30f9055f5fa886 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Thu, 25 Feb 2021 01:46:57 +0900 Subject: [PATCH 3/7] =?UTF-8?q?test:=20Calculator=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/calculator/CalculatorTest.java | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/calculator/CalculatorTest.java diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java new file mode 100644 index 0000000..b7c6866 --- /dev/null +++ b/src/test/java/calculator/CalculatorTest.java @@ -0,0 +1,44 @@ +package calculator; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +class CalculatorTest { + + private List operands = Arrays.asList(2, 4, 3, 7, 5); + private List operators = Arrays.asList('+', '-', '*', '/'); + + private Calculator calculator = new Calculator(); + + @Test + void calculate() { + double result; + + result = calculator.calculate(operands, operators); + + assertThat(result).isEqualTo(4.2); + } + + @Test + void 사칙연산() { + double addition, subtraction, multiplication, division, wrongOperatorResult; + + addition = calculator.fourFundamentalArithmeticOperations(operands.get(0), operators.get(0), operands.get(1)); + subtraction = calculator.fourFundamentalArithmeticOperations(operands.get(0), operators.get(1), + operands.get(1)); + multiplication = calculator.fourFundamentalArithmeticOperations(operands.get(0), operators.get(2), + operands.get(1)); + division = calculator.fourFundamentalArithmeticOperations(operands.get(0), operators.get(3), operands.get(1)); + wrongOperatorResult = calculator.fourFundamentalArithmeticOperations(operands.get(0), '?', operands.get(1)); + + assertThat(addition).isEqualTo(6.0); + assertThat(subtraction).isEqualTo(-2.0); + assertThat(multiplication).isEqualTo(8.0); + assertThat(division).isEqualTo(0.5); + assertThat(wrongOperatorResult).isEqualTo(0); + } +} \ No newline at end of file From 8949088a85576470754409a765abe0175447fb85 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Thu, 25 Feb 2021 04:03:30 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=95=84=20=ED=94=BC=EC=97=B0?= =?UTF-8?q?=EC=82=B0=EC=9E=90=EC=99=80=20=EC=97=B0=EC=82=B0=EC=9E=90?= =?UTF-8?q?=EB=A1=9C=20=EB=82=98=EB=88=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++------ src/main/java/calculator/Application.java | 19 +++++++++ src/main/java/calculator/Input.java | 52 +++++++++++++++++++++++ 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 src/main/java/calculator/Application.java create mode 100644 src/main/java/calculator/Input.java diff --git a/README.md b/README.md index 8e72353..43494bc 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,11 @@ --- ## 힌트 - - 문자열을 입력 받은 후(scanner의 nextLine() 메소드 활용) 빈 공백 문자열을 기준으로 문자들을 분리해야 한다. - ```java String value = scanner.nextLine(); String[] values = value.split(" "); ``` - - 문자열을 숫자로 변경하는 방법 `int number = Integer.parseInt("문자열");` @@ -26,18 +23,23 @@ String[] values = value.split(" "); --- ### 기능 - - 예외 - - 연산자나 숫자가 두 번 이상 연속으로 나올 경우 - - 숫자와 연산자 이외의 문자 - - 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) + - [ ] 연산자나 숫자가 두 번 이상 연속으로 나올 경우 + - [ ] 숫자와 연산자 이외의 문자 + - [ ] 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) - 순서 - - 문자열 입력 받기 - - 예외 처리 - - 띄어쓰기로 분리해서 배열에 넣기 - - 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 - - 계산하기 + - [x] 문자열 입력 받기 + - [ ] 예외 처리 + - [x] 띄어쓰기로 분리해서 배열에 넣기 + - [x] 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 + - [x] 계산하기 ### 예제 +- 2 + 4 / 3 +- 20 + 4932 * 2 +- 1 + - 9 +- 4 & 2 +- 9/3 + 2 + 4 / 3 20 + 4932 * 1 1 + - 9 4 & 2 9/3 diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java new file mode 100644 index 0000000..a49f498 --- /dev/null +++ b/src/main/java/calculator/Application.java @@ -0,0 +1,19 @@ +package calculator; + +import java.util.List; +import java.util.Scanner; + +public class Application { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String operation = scanner.nextLine(); + + Input input = new Input(operation); + List operands = input.getOperands(); + List operators = input.getOperators(); + + Calculator calculator = new Calculator(); + double result = calculator.calculate(operands, operators); + System.out.println("result = " + result); + } +} diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java new file mode 100644 index 0000000..3d7e612 --- /dev/null +++ b/src/main/java/calculator/Input.java @@ -0,0 +1,52 @@ +package calculator; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Input { + private final String operation; + private final String[] values; + private List operands; + private List operators; + + public Input(String operation) { + this.operation = operation; + values = operation.split(" "); + verifyOperation(); + splitOperation(); + } + + public void verifyOperation() { + } + + public void splitOperation() { + operands = Arrays.stream(values) + .filter(operand -> operand.matches("^[0-9]*$")) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + operators = Arrays.stream(values) + .filter(operator -> operator.matches("[+*/-]")) + .map(operator -> operator.charAt(0)) + .collect(Collectors.toList()); + + /*System.out.println("operand"); + for (int operand : operands) { + System.out.println(operand); + } + + System.out.println("operators"); + for (String operator : operators) { + System.out.println(operator); + }*/ + } + + public List getOperands() { + return operands; + } + + public List getOperators() { + return operators; + } +} From 30cb7c4f5bb4babdfe3adfc97ccf252ca7e18d45 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Thu, 25 Feb 2021 23:25:34 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20Operation=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 문자열이 숫자로 시작하지 않을 경우 - 연산자나 숫자가 두 번 이상 연속으로 나올 경우 --- README.md | 8 ++- src/main/java/calculator/Application.java | 8 +-- src/main/java/calculator/Input.java | 52 -------------- src/main/java/calculator/Operation.java | 76 +++++++++++++++++++++ src/test/java/calculator/OperationTest.java | 47 +++++++++++++ 5 files changed, 132 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/calculator/Input.java create mode 100644 src/main/java/calculator/Operation.java create mode 100644 src/test/java/calculator/OperationTest.java diff --git a/README.md b/README.md index 43494bc..2a5f0c4 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,15 @@ String[] values = value.split(" "); ### 기능 - 예외 - - [ ] 연산자나 숫자가 두 번 이상 연속으로 나올 경우 - - [ ] 숫자와 연산자 이외의 문자 + - [x] 숫자로 시작하지 않을 경우 - [ ] 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) + - 음수가 있으면? ex> 2 + -1 + - [ ] 숫자와 연산자 이외의 문자 + - [x] 연산자나 숫자가 두 번 이상 연속으로 나올 경우 - 순서 - [x] 문자열 입력 받기 - - [ ] 예외 처리 - [x] 띄어쓰기로 분리해서 배열에 넣기 + - [ ] 예외 처리 - [x] 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 - [x] 계산하기 diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index a49f498..7565efd 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -6,11 +6,11 @@ public class Application { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - String operation = scanner.nextLine(); + String input = scanner.nextLine(); - Input input = new Input(operation); - List operands = input.getOperands(); - List operators = input.getOperators(); + Operation operation = new Operation(input); + List operands = operation.getOperands(); + List operators = operation.getOperators(); Calculator calculator = new Calculator(); double result = calculator.calculate(operands, operators); diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java deleted file mode 100644 index 3d7e612..0000000 --- a/src/main/java/calculator/Input.java +++ /dev/null @@ -1,52 +0,0 @@ -package calculator; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public class Input { - private final String operation; - private final String[] values; - private List operands; - private List operators; - - public Input(String operation) { - this.operation = operation; - values = operation.split(" "); - verifyOperation(); - splitOperation(); - } - - public void verifyOperation() { - } - - public void splitOperation() { - operands = Arrays.stream(values) - .filter(operand -> operand.matches("^[0-9]*$")) - .map(Integer::parseInt) - .collect(Collectors.toList()); - - operators = Arrays.stream(values) - .filter(operator -> operator.matches("[+*/-]")) - .map(operator -> operator.charAt(0)) - .collect(Collectors.toList()); - - /*System.out.println("operand"); - for (int operand : operands) { - System.out.println(operand); - } - - System.out.println("operators"); - for (String operator : operators) { - System.out.println(operator); - }*/ - } - - public List getOperands() { - return operands; - } - - public List getOperators() { - return operators; - } -} diff --git a/src/main/java/calculator/Operation.java b/src/main/java/calculator/Operation.java new file mode 100644 index 0000000..4cf663e --- /dev/null +++ b/src/main/java/calculator/Operation.java @@ -0,0 +1,76 @@ +package calculator; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Operation { + private final String[] operation; + private final Stream stream; + private List operands; + private List operators; + + public Operation(String operation) { + this.operation = operation.split(" "); + stream = Arrays.stream(this.operation); + validateOperation(); + // splitOperation(); + } + + public void validateOperation() { + validateFirstIndex(); + // validateMixedSymbols(); + // validateOtherSymbols(); + validateDuplicate(); + } + + public void validateFirstIndex() { + if (!(operation[0].matches("^[0-9]*$") || operation[0].matches("^\\-[1-9]\\d*$"))) { + throw new IllegalArgumentException("숫자로 시작해야 합니다."); + } + } + + // 숫자와 문자가 섞였을 경우 + // 숫자가 음수일 경우에는 또 빼주기 + public void validateMixedSymbols() { + stream + .filter(operation -> operation.matches("~~~~~")) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("")); + } + + // 숫자와 연산자 이외의 문자 + public void validateOtherSymbols() { + // + } + + public void validateDuplicate() { + for (int i = 0; i < operation.length - 1; i++) { + if ((operation[i].matches("^[0-9]*$") && operation[i + 1].matches("^[0-9]*$")) + || (operation[i].matches("[+*/-]") && operation[i + 1].matches("[+*/-]"))) { + throw new IllegalArgumentException("기호나 숫자가 두 번 연속 입력되면 안 됩니다."); + } + } + } + + public void splitOperation() { + operands = stream + .filter(operand -> operand.matches("^[0-9]*$")) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + operators = stream + .filter(operator -> operator.matches("[+*/-]")) + .map(operator -> operator.charAt(0)) + .collect(Collectors.toList()); + } + + public List getOperands() { + return operands; + } + + public List getOperators() { + return operators; + } +} diff --git a/src/test/java/calculator/OperationTest.java b/src/test/java/calculator/OperationTest.java new file mode 100644 index 0000000..3e3a4ca --- /dev/null +++ b/src/test/java/calculator/OperationTest.java @@ -0,0 +1,47 @@ +package calculator; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class OperationTest { + + String input = "( 2/ + * -1 ? 3 4"; + private Operation operation; + + @BeforeEach + void setUp() { + operation = null; + } + + @Test + void validateFirstIndex() { + String wrongInput = "( 2 + 1"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("숫자로 시작해야 합니다."); + } + + @Test + void validateOperation() { + } + + @Test + void validateOtherSymbols() { + } + + @Test + void validateDuplicate() { + String wrongInput = "2 * / 1 + 3"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("기호나 숫자가 두 번 연속"); + } + + @Test + void splitOperation() { + } +} \ No newline at end of file From b6e8aab84216c472f36653cbd057b7f0093644ab Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Fri, 26 Feb 2021 00:58:26 +0900 Subject: [PATCH 6/7] =?UTF-8?q?chore:=20=EC=BD=94=EB=93=9C=20=EB=B3=91?= =?UTF-8?q?=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Operation 예외처리 구현 - Operation 테스트 구현 --- README.md | 17 ++----- .../java/{calculator => }/Application.java | 5 ++- src/main/java/calculator/Operation.java | 44 +++++++------------ src/test/java/calculator/OperationTest.java | 23 +++++++--- 4 files changed, 39 insertions(+), 50 deletions(-) rename src/main/java/{calculator => }/Application.java (90%) diff --git a/README.md b/README.md index 2a5f0c4..791fe72 100644 --- a/README.md +++ b/README.md @@ -25,23 +25,12 @@ String[] values = value.split(" "); ### 기능 - 예외 - [x] 숫자로 시작하지 않을 경우 - - [ ] 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) - - 음수가 있으면? ex> 2 + -1 - - [ ] 숫자와 연산자 이외의 문자 + - [x] 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) + - [x] 숫자와 연산자 이외의 문자 - [x] 연산자나 숫자가 두 번 이상 연속으로 나올 경우 - 순서 - [x] 문자열 입력 받기 - [x] 띄어쓰기로 분리해서 배열에 넣기 - - [ ] 예외 처리 + - [x] 예외 처리 - [x] 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 - [x] 계산하기 - -### 예제 - -- 2 + 4 / 3 -- 20 + 4932 * 2 -- 1 + - 9 -- 4 & 2 -- 9/3 - -2 + 4 / 3 20 + 4932 * 1 1 + - 9 4 & 2 9/3 diff --git a/src/main/java/calculator/Application.java b/src/main/java/Application.java similarity index 90% rename from src/main/java/calculator/Application.java rename to src/main/java/Application.java index 7565efd..09133f0 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/Application.java @@ -1,8 +1,9 @@ -package calculator; - import java.util.List; import java.util.Scanner; +import calculator.Calculator; +import calculator.Operation; + public class Application { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); diff --git a/src/main/java/calculator/Operation.java b/src/main/java/calculator/Operation.java index 4cf663e..15e8a68 100644 --- a/src/main/java/calculator/Operation.java +++ b/src/main/java/calculator/Operation.java @@ -3,64 +3,52 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; public class Operation { private final String[] operation; - private final Stream stream; private List operands; private List operators; - public Operation(String operation) { - this.operation = operation.split(" "); - stream = Arrays.stream(this.operation); + public Operation(String input) { + this.operation = input.split(" "); validateOperation(); - // splitOperation(); + splitOperation(); } public void validateOperation() { validateFirstIndex(); - // validateMixedSymbols(); - // validateOtherSymbols(); validateDuplicate(); } - public void validateFirstIndex() { - if (!(operation[0].matches("^[0-9]*$") || operation[0].matches("^\\-[1-9]\\d*$"))) { - throw new IllegalArgumentException("숫자로 시작해야 합니다."); + public void validateOtherSymbols() { + for (String tempStr : operation) { + if (!(tempStr.matches("^[0-9]*$") || tempStr.matches("^\\-[1-9]\\d*$") || tempStr.matches("[+*/-]"))) + throw new IllegalArgumentException("한 String에 숫자와 연산자가 함께 있거나, 숫자 연산자 이외의 입력입니다."); } } - // 숫자와 문자가 섞였을 경우 - // 숫자가 음수일 경우에는 또 빼주기 - public void validateMixedSymbols() { - stream - .filter(operation -> operation.matches("~~~~~")) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException("")); - } - - // 숫자와 연산자 이외의 문자 - public void validateOtherSymbols() { - // + public void validateFirstIndex() { + if (operation[0].matches("[+*/-]")) { + throw new IllegalArgumentException("숫자로 시작해야 합니다."); + } } public void validateDuplicate() { for (int i = 0; i < operation.length - 1; i++) { - if ((operation[i].matches("^[0-9]*$") && operation[i + 1].matches("^[0-9]*$")) + if ((!operation[i].matches("[+*/-]") && !operation[i + 1].matches("[+*/-]")) || (operation[i].matches("[+*/-]") && operation[i + 1].matches("[+*/-]"))) { - throw new IllegalArgumentException("기호나 숫자가 두 번 연속 입력되면 안 됩니다."); + throw new IllegalArgumentException("기호나 숫자가 두 번 연속 입력되었습니다."); } } } public void splitOperation() { - operands = stream - .filter(operand -> operand.matches("^[0-9]*$")) + operands = Arrays.stream(this.operation) + .filter(operand -> operand.matches("^[0-9]*$") || operand.matches("^\\-[1-9]\\d*$")) .map(Integer::parseInt) .collect(Collectors.toList()); - operators = stream + operators = Arrays.stream(this.operation) .filter(operator -> operator.matches("[+*/-]")) .map(operator -> operator.charAt(0)) .collect(Collectors.toList()); diff --git a/src/test/java/calculator/OperationTest.java b/src/test/java/calculator/OperationTest.java index 3e3a4ca..5087b41 100644 --- a/src/test/java/calculator/OperationTest.java +++ b/src/test/java/calculator/OperationTest.java @@ -2,12 +2,13 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class OperationTest { - String input = "( 2/ + * -1 ? 3 4"; private Operation operation; @BeforeEach @@ -16,20 +17,30 @@ void setUp() { } @Test - void validateFirstIndex() { - String wrongInput = "( 2 + 1"; + void validateMixed() { + String wrongInput = "2/ 3"; assertThatThrownBy(() -> operation = new Operation(wrongInput)) .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("숫자로 시작해야 합니다."); + .hasMessageContaining("한 String에 숫자와 연산자가 함께 있거나, "); } @Test - void validateOperation() { + void validateOtherSymbols() { + String wrongInput = "( 3 - 4"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("숫자 연산자 이외의 입력입니다"); } @Test - void validateOtherSymbols() { + void validateFirstIndex() { + String wrongInput = "- 2 + 1"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("숫자로 시작해야 합니다."); } @Test From 204937726fc451d7629e49e0b51436fc7d944cb9 Mon Sep 17 00:00:00 2001 From: jummi <98qkrwjdal@naver.com> Date: Fri, 26 Feb 2021 01:08:50 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EB=A7=88=EC=A7=80=EB=A7=89=20?= =?UTF-8?q?=EC=A4=84=20=EA=B3=B5=EB=B0=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Operation.java | 1 + src/test/java/calculator/CalculatorTest.java | 2 +- src/test/java/calculator/OperationTest.java | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/Operation.java b/src/main/java/calculator/Operation.java index 15e8a68..54dd0b9 100644 --- a/src/main/java/calculator/Operation.java +++ b/src/main/java/calculator/Operation.java @@ -16,6 +16,7 @@ public Operation(String input) { } public void validateOperation() { + validateOtherSymbols(); validateFirstIndex(); validateDuplicate(); } diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index b7c6866..be845ed 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -41,4 +41,4 @@ void calculate() { assertThat(division).isEqualTo(0.5); assertThat(wrongOperatorResult).isEqualTo(0); } -} \ No newline at end of file +} diff --git a/src/test/java/calculator/OperationTest.java b/src/test/java/calculator/OperationTest.java index 5087b41..ac7a14c 100644 --- a/src/test/java/calculator/OperationTest.java +++ b/src/test/java/calculator/OperationTest.java @@ -54,5 +54,11 @@ void validateDuplicate() { @Test void splitOperation() { + String input = "2 + -3 * 4 / 2"; + + operation = new Operation(input); + + assertThat(operation.getOperands()).isEqualTo(Arrays.asList(2, -3, 4, 2)); + assertThat(operation.getOperators()).isEqualTo(Arrays.asList('+', '*', '/')); } -} \ No newline at end of file +}