|
| 1 | +package calculator; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | + |
| 6 | +public class Calculator { |
| 7 | + Message message = new Message(); |
| 8 | + public void calculation(){ |
| 9 | + String[] equation_list = getEquation(); |
| 10 | + ArithmeticExpressionStack arithmeticExpressionStack = new ArithmeticExpressionStack(equation_list, equation_list.length); |
| 11 | + OperatorSetting(arithmeticExpressionStack); |
| 12 | + |
| 13 | + } |
| 14 | + |
| 15 | + public void OperatorSetting(ArithmeticExpressionStack arithmeticExpressionStack) { |
| 16 | + ErrorException exception = new ErrorException(); |
| 17 | + String firstString = arithmeticExpressionStack.pop(); |
| 18 | + int firstNumber = exception.NumericalError(firstString); |
| 19 | + |
| 20 | + for(int i = 0; i<(arithmeticExpressionStack.getStackSize())/2;i++){ |
| 21 | + String operator = arithmeticExpressionStack.pop(); |
| 22 | + String secondString = arithmeticExpressionStack.pop(); |
| 23 | + int secondNumber = exception.NumericalError(secondString); |
| 24 | + firstNumber = chooseOperatorAndCalculate(firstNumber, operator, secondNumber); |
| 25 | + } |
| 26 | + |
| 27 | + if(firstNumber != 0){ |
| 28 | + message.calculationResult(firstNumber); |
| 29 | + } |
| 30 | + init(); |
| 31 | + } |
| 32 | + |
| 33 | + private int chooseOperatorAndCalculate(int firstNumber, String operator, int SecondNumber){ |
| 34 | + AddOperation addOperation = new AddOperation(); |
| 35 | + SubOperation subOperation = new SubOperation(); |
| 36 | + MultiplyOperation multiplyOperation = new MultiplyOperation(); |
| 37 | + DivideOperation divideOperation = new DivideOperation(); |
| 38 | + int result = 0; |
| 39 | + if (operator.equals(addOperation.operationName())){ |
| 40 | + result = addOperation.calculation(firstNumber, SecondNumber); |
| 41 | + } |
| 42 | + else if (operator.equals(subOperation.operationName())){ |
| 43 | + result = subOperation.calculation(firstNumber, SecondNumber); |
| 44 | + } |
| 45 | + else if (operator.equals(multiplyOperation.operationName())){ |
| 46 | + result = multiplyOperation.calculation(firstNumber, SecondNumber); |
| 47 | + } |
| 48 | + else if (operator.equals(divideOperation.operationName())){ |
| 49 | + result = divideOperation.calculation(firstNumber, SecondNumber); |
| 50 | + } |
| 51 | + else{ |
| 52 | + message.exceptionResult("NOT_OPERATOR"); |
| 53 | + } |
| 54 | + |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + public void init(){ |
| 59 | + Scanner scanner = new Scanner(System.in); |
| 60 | + System.out.println("계산을 계속 진행하시려면 1, 멈추시려면 임의의 다른키를 눌러주세요"); |
| 61 | + String value = scanner.nextLine(); |
| 62 | + |
| 63 | + if(value.equals("1")){ |
| 64 | + calculation(); |
| 65 | + } |
| 66 | + else{ |
| 67 | + System.exit(0); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public String[] getEquation(){ |
| 72 | + Scanner scanner = new Scanner(System.in); |
| 73 | + System.out.print("수식을 입력해주세요 : "); |
| 74 | + String value = scanner.nextLine(); |
| 75 | + String[] values = value.split(" "); |
| 76 | + return values; |
| 77 | + } |
| 78 | +} |
0 commit comments