From b0a23e3557c0a76c6e6fb564c5fd06a7ac83a73e Mon Sep 17 00:00:00 2001 From: 12carattoothache Date: Fri, 5 Aug 2022 19:44:35 +0900 Subject: [PATCH] =?UTF-8?q?2=EC=A3=BC=EC=B0=A8=203=EB=B2=88=EC=A7=B8=20com?= =?UTF-8?q?mit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/Calculator/Calculator.java | 77 +++++++++++++++++++ .../main/java/Calculator/CalculatorMain.java | 29 +++++++ .../src/main/java/Calculator/Operator.java | 35 +++++++++ .../test/java/Calculator/CalculatorTest.java | 32 ++++++++ 4 files changed, 173 insertions(+) create mode 100644 java-racing-master/src/main/java/Calculator/Calculator.java create mode 100644 java-racing-master/src/main/java/Calculator/CalculatorMain.java create mode 100644 java-racing-master/src/main/java/Calculator/Operator.java create mode 100644 java-racing-master/src/test/java/Calculator/CalculatorTest.java diff --git a/java-racing-master/src/main/java/Calculator/Calculator.java b/java-racing-master/src/main/java/Calculator/Calculator.java new file mode 100644 index 0000000..3ead936 --- /dev/null +++ b/java-racing-master/src/main/java/Calculator/Calculator.java @@ -0,0 +1,77 @@ +package Calculator; + +public class Calculator { + public int output; + public String[] stringArray; + + public void validationCheck(String input) throws Exception { + checkNullOrEmpty(input); + judgeLength(); + checkOperator(); + checkOperand(); + } + + // 입력값이 공백인 경우 예외 처리 + public void checkNullOrEmpty(String input) throws Exception { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException("입력값이 공백입니다."); + } + setStringArray(input); + } + + // 배열 생성 + public void setStringArray(String input) { + stringArray = input.split(" "); + } + + // 배열의 길이가 짝수인 경우 예외 처리 + public void judgeLength() throws Exception { + int length = stringArray.length; + if (length % 2 == 0) + throw new IllegalArgumentException("배열의 길이가 짝수입니다."); + } + + // 사칙연산자가 아닐 경우 예외 처리 + public void checkOperator() throws Exception { + int i = 1; + if (findInvalidOperator(i)) + throw new IllegalArgumentException("잘못된 연산자입니다."); + } + + public boolean findInvalidOperator(int i) { + if (i >= stringArray.length) + return false; + if (stringArray[i].matches("[+*-/]")) + return findInvalidOperator(i + 2); + return true; + } + + // 정수가 아닐 경우 예외 처리 + public void checkOperand() throws Exception { + for (int i = 0; i < stringArray.length; i += 2) { + findInvalidOperand(i); + } + } + + public void findInvalidOperand(int i) throws Exception { + try { + Integer.parseInt(stringArray[i]); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("잘못된 피연산자입니다."); + } + } + + public int calculator() throws Exception { + output = Integer.parseInt(stringArray[0]); + for (int i = 0; i < stringArray.length - 1; i += 2) { + output = calculate(output, stringArray[i + 1], + Integer.parseInt(stringArray[i + 2])); + } + return output; + } + + public int calculate(int n1, String symbol, int n2) throws Exception { + Operator operator = Operator.findOperator(symbol); + return operator.calculate(n1, n2); + } +} \ No newline at end of file diff --git a/java-racing-master/src/main/java/Calculator/CalculatorMain.java b/java-racing-master/src/main/java/Calculator/CalculatorMain.java new file mode 100644 index 0000000..b6299ba --- /dev/null +++ b/java-racing-master/src/main/java/Calculator/CalculatorMain.java @@ -0,0 +1,29 @@ +package Calculator; + +import java.util.Scanner; + +public class CalculatorMain { + private static Scanner scanner; + public static String input; + public static int result; + static Calculator cal = new Calculator(); + + public static void main(String[] args) throws Exception { + inputOperation(); + cal.validationCheck(input); + cal.calculator(); + printResult(); + } + + // 계산식을 직접 입력 + public static void inputOperation() { + scanner = new Scanner(System.in); + input = scanner.nextLine(); + } + + // 결과를 출력 + private static void printResult() throws Exception { + result = cal.calculator(); + System.out.println(result); + } +} \ No newline at end of file diff --git a/java-racing-master/src/main/java/Calculator/Operator.java b/java-racing-master/src/main/java/Calculator/Operator.java new file mode 100644 index 0000000..dc27c04 --- /dev/null +++ b/java-racing-master/src/main/java/Calculator/Operator.java @@ -0,0 +1,35 @@ +package Calculator; + +import java.util.function.BiFunction; + +public enum Operator { + ADD("+", (n1, n2) -> n1 + n2), + SUBTRACT("-", (n1, n2) -> n1 - n2), + MULTIPLY("*", (n1, n2) -> n1 * n2), + DIVIDE("/", (n1, n2) -> { + if (n2 == 0) + throw new ArithmeticException(); + return (n1 / n2); + }); + + private final String symbol; + private final BiFunction operation; + + Operator(String symbol, BiFunction operation) { + this.symbol = symbol; + this.operation = operation; + } + + public int calculate(int n1, int n2) { + return operation.apply(n1, n2); + } + + public static Operator findOperator(String symbol) { + for (Operator operator : Operator.values()) { + if (symbol.equals(operator.symbol)) { + return operator; + } + } + throw new IllegalArgumentException("유효하지 않은 연산자입니다."); + } +} diff --git a/java-racing-master/src/test/java/Calculator/CalculatorTest.java b/java-racing-master/src/test/java/Calculator/CalculatorTest.java new file mode 100644 index 0000000..c4c7c45 --- /dev/null +++ b/java-racing-master/src/test/java/Calculator/CalculatorTest.java @@ -0,0 +1,32 @@ +package Calculator; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class CalculatorTest { + static Calculator calculator = new Calculator(); + + @ParameterizedTest + @DisplayName("올바르지 않은 입력값으로 테스트") + @ValueSource(strings = {"", "9 + 1 , 10", + "3 ? 7 * 4, -1", "9 + 2 - a, 10"}) + void exceptionTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + calculator.calculator(); + }); + } + + @ParameterizedTest + @DisplayName("올바른 입력값으로 테스트") + @CsvSource({"5 + 3 / 4, 2", "12 - 3 * 3, 27", + "10 / 3 + 2, 5", "7 / 2 + 8, 11"}) + void calculationTest(String testInput, String testAnswer) throws Exception { + calculator.validationCheck(testInput); + assertThat(calculator.calculator()).isEqualTo(Integer.parseInt(testAnswer)); + } +} \ No newline at end of file