Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

2주차 3번째 commit #33

Open
wants to merge 1 commit into
base: master
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
77 changes: 77 additions & 0 deletions java-racing-master/src/main/java/Calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 29 additions & 0 deletions java-racing-master/src/main/java/Calculator/CalculatorMain.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions java-racing-master/src/main/java/Calculator/Operator.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer, Integer> operation;

Operator(String symbol, BiFunction<Integer, Integer, Integer> 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("유효하지 않은 연산자입니다.");
}
}
32 changes: 32 additions & 0 deletions java-racing-master/src/test/java/Calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}