public class Calculator { private static final List<NewArithmeticOperator> arithmeticOperators = List.of(new AddithionOperator(), new SubtractionOperator(), new MultiplicationOperator(), new DivisionOperator()); public static int calculator(PositiveNumber operand1, String operator, PositiveNumber operand2) { return arithmeticOperators.stream() .filter(arithmeticOperators -> arithmeticOperators.supports(operator)) .map(arithmeticOperators -> arithmeticOperators.calculate(operand1, operand2)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("올바른 사칙연산이 아닙니다.")); } } package com.gooot; import java.util.Arrays; public enum ArithmeticOperator { ADDITION("+") { @Override public int arithmeticCalculate(int operand1, int operand2) { return operand1 + operand2; } }, SUBTRACTION("-") { @Override public int arithmeticCalculate(int operand1, int operand2) { return operand1 - operand2; } }, MULTIPLICATION("*") { @Override public int arithmeticCalculate(int operand1, int operand2) { return operand1 * operand2; } }, DIVISION("/") { @Override public int arithmeticCalculate(int operand1, int operand2) { return operand1 / operand2; } }; private final String operator; ArithmeticOperator(String operator) { this.operator = operator; } public abstract int arithmeticCalculate(final int operand1, final int operand2); public static int calculate(int operand1,String operator, int operand2) { ArithmeticOperator arithmeticOperator = Arrays.stream(values()) .filter(v->v.operator.equals(operator)) .findFirst() .orElseThrow(()-> new IllegalArgumentException(operator+" is not supported")); return arithmeticOperator.arithmeticCalculate(operand1,operand2); } } package com.gooot.calculate; public class PositiveNumber { private final int value; public PositiveNumber(int value) { validate(value); this.value = value; } private void validate(int value) { if(isNegativeNumber(value)){ throw new IllegalArgumentException("0 또는 음수를 전달 할 수 없습니다."); } } private boolean isNegativeNumber(int value) { return value <= 0; } public int toInt(){ return value; } } package com.gooot.calculate; public interface NewArithmeticOperator { boolean supports(String operator); int calculate(PositiveNumber operand1, PositiveNumber operand2); } package com.gooot.calculate; public class AddithionOperator implements NewArithmeticOperator { @Override public boolean supports(String operator) { return "+".equals(operator); } @Override public int calculate(PositiveNumber operand1, PositiveNumber operand2) { return operand1.toInt() + operand2.toInt(); } }