From dc23b10f8ff961ec526ee48c8e8d2fb95ab5a33c Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 00:57:56 +0900 Subject: [PATCH 1/6] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=ED=95=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index e79684b..7503c30 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,16 @@ String[] values = value.split(" "); - 문자열을 숫자로 변경하는 방법 `int number = Integer.parseInt("문자열");` + +### 기능 +- 예외 + - [x] 숫자로 시작하지 않을 경우 + - [x] 띄어쓰기가 잘 안 돼서 숫자와 문자가 섞였을 경우(공백문자 미사용) + - [x] 숫자와 연산자 이외의 문자 + - [x] 연산자나 숫자가 두 번 이상 연속으로 나올 경우 +- 순서 + - [x] 문자열 입력 받기 + - [x] 띄어쓰기로 분리해서 배열에 넣기 + - [x] 예외 처리 + - [x] 피연산자(operand 숫자)와 연산자(operator +-/*)로 나누기 + - [x] 계산하기 From 80a7b8792a90de8ffcef2a87ab15f1004d027a97 Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 00:58:34 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=EC=88=98=EC=8B=9D=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EA=B8=B0=EB=8A=A5=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.java | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/Calculator.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..ae11068 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,41 @@ + +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 f52f6f28b8e4d4c0c765ac205bf3edb5a688832b Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 00:59:19 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=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/Operation.java | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/main/java/Operation.java diff --git a/src/main/java/Operation.java b/src/main/java/Operation.java new file mode 100644 index 0000000..3a07301 --- /dev/null +++ b/src/main/java/Operation.java @@ -0,0 +1,65 @@ + +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 List operands; + private List operators; + + public Operation(String input) { + this.operation = input.split(" "); + validateOperation(); + splitOperation(); + } + + public void validateOperation() { + validateOtherSymbols(); + validateFirstIndex(); + validateDuplicate(); + } + + 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 validateFirstIndex() { + if (operation[0].matches("[+*/-]")) { + throw new IllegalArgumentException("숫자로 시작해야 합니다."); + } + } + + public void validateDuplicate() { + for (int i = 0; i < operation.length - 1; i++) { + if ((!operation[i].matches("[+*/-]") && !operation[i + 1].matches("[+*/-]")) + || (operation[i].matches("[+*/-]") && operation[i + 1].matches("[+*/-]"))) { + throw new IllegalArgumentException("기호나 숫자가 두 번 연속 입력되었습니다."); + } + } + } + + public void splitOperation() { + operands = Arrays.stream(this.operation) + .filter(operand -> operand.matches("^[0-9]*$") || operand.matches("^\\-[1-9]\\d*$")) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + operators = Arrays.stream(this.operation) + .filter(operator -> operator.matches("[+*/-]")) + .map(operator -> operator.charAt(0)) + .collect(Collectors.toList()); + } + + public List getOperands() { + return operands; + } + + public List getOperators() { + return operators; + } +} From 1cbf903c3f040e39cf170c58d36de7a48ae1b90f Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 01:00:55 +0900 Subject: [PATCH 4/6] =?UTF-8?q?test:=20=EC=88=98=EC=8B=9D=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EA=B8=B0=EB=8A=A5=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/CalculatorTest.java | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/java/CalculatorTest.java diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java new file mode 100644 index 0000000..305c886 --- /dev/null +++ b/src/test/java/CalculatorTest.java @@ -0,0 +1,43 @@ + +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 9cab3c5d54ac93294fdc5f23a035ab885c65e7ea Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 01:01:19 +0900 Subject: [PATCH 5/6] =?UTF-8?q?test:=20=EC=9E=85=EB=A0=A5=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=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/OperationTest.java | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/OperationTest.java diff --git a/src/test/java/OperationTest.java b/src/test/java/OperationTest.java new file mode 100644 index 0000000..4ea7dee --- /dev/null +++ b/src/test/java/OperationTest.java @@ -0,0 +1,64 @@ + +import static org.assertj.core.api.Assertions.*; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class OperationTest { + + private Operation operation; + + @BeforeEach + void setUp() { + operation = null; + } + + @Test + void validateMixed() { + String wrongInput = "2/ 3"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("한 String에 숫자와 연산자가 함께 있거나, "); + } + + @Test + void validateOtherSymbols() { + String wrongInput = "( 3 - 4"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("숫자 연산자 이외의 입력입니다"); + } + + @Test + void validateFirstIndex() { + String wrongInput = "- 2 + 1"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("숫자로 시작해야 합니다."); + } + + @Test + void validateDuplicate() { + String wrongInput = "2 * / 1 + 3"; + + assertThatThrownBy(() -> operation = new Operation(wrongInput)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("기호나 숫자가 두 번 연속"); + } + + @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 From e50cdb870c7929e1bc9adc6b5e302b07893c26de Mon Sep 17 00:00:00 2001 From: Chae Date: Fri, 26 Feb 2021 01:02:07 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EC=9E=91=EB=8F=99=20=EA=B8=B0=EB=8A=A5=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/Application.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/Application.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000..bcfbfda --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,17 @@ +import java.util.List; +import java.util.Scanner; + +public class Application { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + + Operation operation = new Operation(input); + List operands = operation.getOperands(); + List operators = operation.getOperators(); + + Calculator calculator = new Calculator(); + double result = calculator.calculate(operands, operators); + System.out.println("result = " + result); + } +}