// 연산자 enum 클래스 화
enum OperatorType {
ADD("+"),
SUB("-"),
MULT("*"),
DIV("/"),
REM("%");
// 프로그램이 실행되면 Map을 캐싱해 찾고자 하는 키값과 필드값을 매칭
private static final Map<String, OperatorType> OPERATOR_MAP =
Collections.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(OperatorType::getOperator, Function.identity())));
private final String operator;
// 생성자를 통한 String 매칭
OperatorType(String operator) {
this.operator = operator;
}
// 입력된 연산자를 통해 Map 안에서 열거 객체 리턴
public static OperatorType find(String operator) {
if (OPERATOR_MAP.containsKey(operator)) {
return OPERATOR_MAP.get(operator);
}
//연산자 외 문자 입력시 예외 발생 유도
throw new IllegalArgumentException("맞지 않는 연산자 입력 : " + operator);
}
public String getOperator() {
return operator;
}
}
public double calculate(T firstNum, T secondNum, String operator) throws ArithmeticException, OperatorInputException {
operatorType = OperatorType.find(operator);
switch (operatorType) {
case ADD:
result = firstNum.doubleValue() + secondNum.doubleValue();
// 연산 과정 및 결과 저장 메서드 호출.
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
case SUB:
result = firstNum.doubleValue() - secondNum.doubleValue();
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
case MULT:
result = firstNum.doubleValue() * secondNum.doubleValue();
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
case DIV:
// 분모가 0일경우 예외 발생 유도.
if (secondNum.doubleValue() == 0 || secondNum.doubleValue() == 0.0) {
throw new ArithmeticException("0으로는 나눌 수 없습니다.");
}
result = firstNum.doubleValue() / secondNum.doubleValue();
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
case REM:
result = firstNum.doubleValue() % secondNum.doubleValue();
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
}
return result;
}
static 키워드를 통해 프로그램이 실행될 때 람다,스트림을 활용하여 Map<'필드명',열거상수 객체>로 매핑하였다.
참조 :
https://itellyhood.tistory.com/70
https://velog.io/@ljinsk3/Enum%EC%9D%98-%EC%9A%94%EC%86%8C%EB%A5%BC-%EC%A1%B0%ED%9A%8C%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95-%EB%B9%84%EA%B5%90-%ED%8C%A9%ED%86%A0%EB%A6%AC-%EB%A9%94%EC%84%9C%EB%93%9C-vs-HashMap
public class ArithmeticCalculator<T extends Number> {
private double result = 0;
// 연산과정 및 결과 저장 List 생성
private ArrayList<T> firstNumbers = new ArrayList<>();
private ArrayList<T> secondNumbers = new ArrayList<>();
public double calculate(T firstNum, T secondNum, String operator) throws ArithmeticException, OperatorInputException {
operatorType = OperatorType.find(operator);
switch (operatorType) {
case ADD:
result = firstNum.doubleValue() + secondNum.doubleValue();
// 연산 과정 및 결과 저장 메서드 호출.
saveCalculationProcess(firstNum, secondNum, result, operator);
break;
private void saveCalculationProcess(T firstNumber, T secondNumber, double result, String operator) {
firstNumbers.add(firstNumber);
secondNumbers.add(secondNumber);
results.add(result);
operators.add(operator);
}
참조 :
https://inpa.tistory.com/entry/JAVA-%E2%98%95-%EC%A0%9C%EB%84%A4%EB%A6%ADGenerics-%EA%B0%9C%EB%85%90-%EB%AC%B8%EB%B2%95-%EC%A0%95%EB%B3%B5%ED%95%98%EA%B8%B0
https://codevang.tistory.com/165
https://notbusyperson.tistory.com/43
// 입력값보다 큰값 출력, 스트림,람다식 사용
public void getInputLargerThanContent(double baseValue) {
System.out.println(baseValue + " 보다 큰 값은");
results.stream().filter(result -> baseValue < result).forEach(System.out::println);
System.out.println("입니다.");
}
// 결과 리스트의 평균값 리턴
public double getAvg() throws EmptyListException {
if (emptyListChecker()) {
throw new EmptyListException("저장된 결과값이 없습니다.");
} else {
return results.stream().filter(Objects::nonNull).mapToDouble(Double::doubleValue).average().orElse(0);
}
}
// 결과 리스트중 최댓값 리턴
public double getMax() throws EmptyListException {
if (emptyListChecker()) {
throw new EmptyListException("저장된 결과값이 없습니다.");
} else {
return results.stream().filter(Objects::nonNull).mapToDouble(Double::doubleValue).max().orElse(0);
}
}
// 결과 리스트중 최솟값 리턴
public double getMin() throws EmptyListException {
if (emptyListChecker()) {
throw new EmptyListException("저장된 결과값이 없습니다.");
} else {
return results.stream().filter(Objects::nonNull).mapToDouble(Double::doubleValue).min().orElse(0);
}
참조 :
https://mangkyu.tistory.com/114
https://mong-dev.tistory.com/28
https://www.elancer.co.kr/blog/view?seq=255




📦
├─ .gitignore
├─ .idea
│ ├─ .gitignore
│ ├─ material_theme_project_new.xml
│ ├─ misc.xml
│ ├─ modules.xml
│ ├─ uiDesigner.xml
│ └─ vcs.xml
├─ CalculatingMachineProject.iml
├─ gitmessage.txt
└─ src
└─ com
└─ calculator
├─ lv1 : Lv.1 제약으로 구현한 계산기
│ └─ Calculator.java
├─ lv2 : Lv.2 제약으로 구현한 계산기
│ ├─ App.java : main 실행 class
│ ├─ Calculator.java : 계산 담당 Class
│ ├─ ServiceManager.java : 프로그램 흐름 제어 class
│ └─ customexception : 커스텀 예외
│ ├─ EmptyListException.java
│ └─ OperatorInputException.java
└─ lv3: Lv.3 제약으로 구현한 계산기
├─ AbstractCalculator.java : 계산기능 추상 Class
├─ AddCalculator.java
├─ App.java : main 실행 class
├─ ArithmeticCalculator.java : 계산 흐름 담당 및 추가기능 담당 Class
├─ CalculatorManager.java : 계산기능구현 Class 관리 Class
├─ DivCalculator.java
├─ MultCalculator.java
├─ RemCalculator.java
├─ ServiceManager.java : 프로그램 흐름 제어 Class
├─ SubCalculator.java
└─ customexception
├─ EmptyListException.java
└─ OperatorInputException.java