55
66public class Calculator {
77 Message message = new Message ();
8+
9+ /**
10+ * calculation method
11+ * 1. getEquation - Receives numeric expression in string format entered by user
12+ * 2. Put numeric expressions on the stack
13+ * 3. Passing the stack to the operatorSetting method
14+ *
15+ * return type : void
16+ */
817 public void calculation (){
918 String [] equation_list = getEquation ();
1019 ArithmeticExpressionStack arithmeticExpressionStack = new ArithmeticExpressionStack (equation_list , equation_list .length );
1120 OperatorSetting (arithmeticExpressionStack );
1221
1322 }
1423
24+ /**
25+ * OperatorSetting method
26+ * 1. Pop each operator and number onto the stack.
27+ * 2. Perform error exception check and calculate
28+ * 3. After the calculation process is complete, call init() to determine whether to return the calculator
29+ *
30+ * return type : void
31+ */
1532 public void OperatorSetting (ArithmeticExpressionStack arithmeticExpressionStack ) {
1633 ErrorException exceptionCheck = new ErrorException ();
1734 String firstString = arithmeticExpressionStack .pop ();
35+
36+ // Error checking when converting string to int
1837 int cumulativeResult = exceptionCheck .NumericalError (firstString );
1938
2039 for (int i = 0 ; i <(arithmeticExpressionStack .getStackSize ())/2 ;i ++){
2140 String operator = arithmeticExpressionStack .pop ();
2241 String secondString = arithmeticExpressionStack .pop ();
2342 int secondNumber = exceptionCheck .NumericalError (secondString );
43+
44+ // calculated value are continuously accumulated
2445 cumulativeResult = chooseOperatorAndCalculate (cumulativeResult , operator , secondNumber );
2546 }
2647
2748 if (cumulativeResult != 0 ){
2849 message .calculationResult (cumulativeResult );
2950 }
51+
52+ // Whether to re-execute after calculation
3053 init ();
3154 }
3255
56+ /**
57+ * chooseOperatorAndCalculate method
58+ * 1. Pop each operator and number onto the stack.
59+ * 2. Perform error exception check and calculate
60+ * 3. After the calculation process is complete, call init() to determine whether to return the calculator
61+ *
62+ * return type : int
63+ */
3364 public int chooseOperatorAndCalculate (int firstNumber , String operator , int SecondNumber ){
3465 AddOperation addOperation = new AddOperation ();
3566 SubOperation subOperation = new SubOperation ();
3667 MultiplyOperation multiplyOperation = new MultiplyOperation ();
3768 DivideOperation divideOperation = new DivideOperation ();
3869 int result = 0 ;
70+
71+ // When the operator is equal to "+"
3972 if (operator .equals (addOperation .operationName ())){
4073 result = addOperation .calculation (firstNumber , SecondNumber );
4174 }
75+
76+ // When the operator is equal to "-"
4277 else if (operator .equals (subOperation .operationName ())){
4378 result = subOperation .calculation (firstNumber , SecondNumber );
4479 }
80+
81+ // When the operator is equal to "*"
4582 else if (operator .equals (multiplyOperation .operationName ())){
4683 result = multiplyOperation .calculation (firstNumber , SecondNumber );
4784 }
85+
86+ // When the operator is equal to "/"
4887 else if (operator .equals (divideOperation .operationName ())){
4988 result = divideOperation .calculation (firstNumber , SecondNumber );
5089 }
90+
91+ // Raise error when a symbol that does not correspond to the arithmetic operations(+, -, *, /) comes
5192 else {
5293 message .exceptionResult ("NOT_OPERATOR" );
5394 }
5495
5596 return result ;
5697 }
5798
99+ /**
100+ * init method
101+ * - Input "1" to redo calculator.
102+ * - Input another key to exit
103+ *
104+ * return type : void
105+ */
58106 public void init (){
59107 Scanner scanner = new Scanner (System .in );
60108 System .out .println ("계산을 계속 진행하시려면 1, 멈추시려면 임의의 다른키를 눌러주세요" );
@@ -68,6 +116,12 @@ public void init(){
68116 }
69117 }
70118
119+ /**
120+ * getEquation method
121+ * - Method to receive user input data
122+ *
123+ * return type : void
124+ */
71125 public String [] getEquation (){
72126 Scanner scanner = new Scanner (System .in );
73127 System .out .print ("수식을 입력해주세요 : " );
0 commit comments