Skip to content

Commit fc69dd8

Browse files
authored
Merge pull request biforest#4 from oereo/feature/java-calculator-comment
feat: add comment and refactor
2 parents 52021fa + c3b416c commit fc69dd8

6 files changed

Lines changed: 78 additions & 3 deletions

File tree

src/main/java/calculator/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
public class Application {
55
public static void main(String[] args) {
6+
7+
// Declare calculator object and start calculation
68
Calculator calculator = new Calculator();
79
calculator.calculation();
810
}

src/main/java/calculator/ArithmeticExpressionStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ArithmeticExpressionStack(String[] equation, int stackSize){
2121

2222
public String pop(){
2323
if(isEmpty()){
24-
return "없는데?";
24+
return "스택이 존재하지 않습니다.";
2525
}
2626
else{
2727
return stack[flag++];

src/main/java/calculator/Calculator.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,104 @@
55

66
public 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("수식을 입력해주세요 : ");

src/main/java/calculator/DivideOperation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ public String operationName() {
1313

1414
@Override
1515
public int calculation(int beforeNumber, int afterNumber) {
16+
Message message = new Message();
1617
try{
1718
int Result = beforeNumber / afterNumber;
1819
return Result;
1920
}
2021
catch (ArithmeticException e){
21-
System.out.println("0으로 나눌수가 없습니다.");
22+
message.exceptionResult("INVALID_DIVIDE");
2223
}
2324
return 0;
2425
}

src/main/java/calculator/ErrorException.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
public class ErrorException {
66
Message message = new Message();
7+
8+
/**
9+
* NumericalError method
10+
* - Raise error when string is not converted to int
11+
*
12+
* return type : void
13+
*/
714
public int NumericalError(String number) {
815
int numberConverted = 0;
916
try {

src/main/java/calculator/Message.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
package calculator;
22

3-
import java.lang.reflect.Field;
43
import java.util.HashMap;
54

5+
/**
6+
* Message class
7+
* - message output class
8+
*
9+
* return type : void
10+
*/
611
public class Message {
712
HashMap<String, String> exceptionMessageList = new HashMap<String, String>();
13+
14+
// Result output method when operation is normally performed
815
public void calculationResult(int result){
916
System.out.print("결과 : ");
1017
System.out.println(result);
1118
}
19+
20+
// Error message list management method
1221
private void exceptionMessageList(){
1322
exceptionMessageList.put("NOT_OPERATOR", "사칙연산 기호에 해당하지 않는 기호가 존재합니다.");
1423
exceptionMessageList.put("INVALID_EQUATION", "잘못된 연산식입니다.");
24+
exceptionMessageList.put("INVALID_DIVIDE", "0으로 나눌수가 없습니다.");
1525
}
1626

27+
// Error message output method
1728
public void exceptionResult(String exceptionKeyword){
1829
exceptionMessageList();
1930
System.out.println(exceptionMessageList.get(exceptionKeyword));

0 commit comments

Comments
 (0)