Skip to content

Commit f53fdf9

Browse files
authored
Merge pull request biforest#1 from oereo/feature/java-calculator
Merge feature/java-calculator
2 parents 6dc21d3 + 9aab010 commit f53fdf9

14 files changed

+340
-0
lines changed

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'java-calculator'
2+

src/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Java-Calculator
2+
3+
Create a string calculator and write test code for it
4+
5+
## Contributor
6+
- oereo
7+
8+
## Branch structure
9+
10+
### oereo branch
11+
* from `oereo/java-calculator[oereo branch]` to `biforest/java-calculator [oereo branch]`
12+
13+
### java-calculator-structure branch
14+
* Feature branch : Branch to develop the function ex)feature/java-calculator
15+
* Fix branch : Branch to fix the function ex)fix/java-calculator
16+
17+
## TDD
18+
### Failure case
19+
#### (1) When divided by 0
20+
```console
21+
연산을 입력해주세요 : 2/0 // error
22+
```
23+
#### (2) When two or more operation symbols appear in a row
24+
```console
25+
연산을 입력해주세요 : 2++0 // error
26+
```
27+
#### (3) When the operation symbol appears at the beginning
28+
```console
29+
연산을 입력해주세요 : +2+0 // error
30+
```
31+
#### (4) When special characters other than operation symbols are entered
32+
```console
33+
연산을 입력해주세요 : 2^4 // error
34+
```
35+
#### (5) When characters excluding numbers and operators are entered
36+
```console
37+
연산을 입력해주세요 : 2ㄱㄴ4*4 // error
38+
```
39+
#### (6) When the formula ends with an operation symbol
40+
```console
41+
연산을 입력해주세요 : 2-1* // error
42+
```
43+
## Structure
44+
#### 1. Class
45+
- Calculator class
46+
- `+ class`
47+
- `- class`
48+
- `* class`
49+
- `/ class`
50+
- Input class
51+
- Output class
52+
- `Error message` output
53+
- `Calculation result` output
54+
- Error exception class
55+
- Precautions : only use `try ~ catch`
56+
- Init class
57+
- When an error occurs, reset after outputting an error message
58+
- Reset and input when pressing any key value
59+
60+
#### 2. Algorithm
61+
- DFS, Node
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package calculator;
2+
3+
public class AddOperation implements OperationInterface {
4+
@Override
5+
public int operationPriority() {
6+
return 1;
7+
}
8+
9+
@Override
10+
public String operationName() {
11+
return "+";
12+
}
13+
14+
@Override
15+
public int calculation(int beforeNumber, int afterNumber) {
16+
int Result = beforeNumber + afterNumber;
17+
return Result;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package calculator;
2+
3+
4+
public class Application {
5+
public static void main(String[] args) {
6+
Calculator calculator = new Calculator();
7+
calculator.calculation();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package calculator;
2+
3+
4+
interface Stack{
5+
boolean isEmpty();
6+
String pop();
7+
void clear();
8+
}
9+
10+
public class ArithmeticExpressionStack implements Stack {
11+
private int stackSize;
12+
private int flag;
13+
private String stack[];
14+
15+
public ArithmeticExpressionStack(String[] equation, int stackSize){
16+
clear();
17+
flag = 0;
18+
this.stackSize = stackSize;
19+
stack = equation;
20+
}
21+
22+
public String pop(){
23+
if(isEmpty()){
24+
return "없는데?";
25+
}
26+
else{
27+
return stack[flag++];
28+
}
29+
}
30+
31+
public boolean isEmpty(){
32+
return (flag == this.stackSize);
33+
}
34+
35+
public void clear(){
36+
if(!isEmpty()){
37+
flag = -1;
38+
this.stackSize = stackSize;
39+
}
40+
}
41+
42+
public int getStackSize(){
43+
return this.stackSize;
44+
}
45+
46+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package calculator;
2+
3+
public class DivideOperation implements OperationInterface {
4+
@Override
5+
public int operationPriority() {
6+
return 1;
7+
}
8+
9+
@Override
10+
public String operationName() {
11+
return "/";
12+
}
13+
14+
@Override
15+
public int calculation(int beforeNumber, int afterNumber) {
16+
try{
17+
int Result = beforeNumber / afterNumber;
18+
return Result;
19+
}
20+
catch (ArithmeticException e){
21+
System.out.println("0으로 나눌수가 없습니다.");
22+
}
23+
return 0;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package calculator;
2+
3+
import java.util.InputMismatchException;
4+
5+
public class ErrorException {
6+
Message message = new Message();
7+
public int NumericalError(String number) {
8+
int numberConverted = 0;
9+
try {
10+
numberConverted = Integer.parseInt(number);
11+
} catch (NumberFormatException e) {
12+
message.exceptionResult("INVALID_EQUATION");
13+
Calculator calculator = new Calculator();
14+
calculator.init();
15+
}
16+
return numberConverted;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package calculator;
2+
import java.util.Scanner;
3+
4+
public class InputUserData {
5+
public Scanner getEquation(){
6+
Scanner scanner = new Scanner(System.in);
7+
return scanner;
8+
}
9+
}

src/main/java/calculator/Message.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package calculator;
2+
3+
import java.lang.reflect.Field;
4+
import java.util.HashMap;
5+
6+
public class Message {
7+
HashMap<String, String> exceptionMessageList = new HashMap<String, String>();
8+
public void calculationResult(int result){
9+
System.out.print("결과 : ");
10+
System.out.println(result);
11+
}
12+
private void exceptionMessageList(){
13+
exceptionMessageList.put("NOT_OPERATOR", "사칙연산 기호에 해당하지 않는 기호가 존재합니다.");
14+
exceptionMessageList.put("INVALID_EQUATION", "잘못된 연산식입니다.");
15+
}
16+
17+
public void exceptionResult(String exceptionKeyword){
18+
exceptionMessageList();
19+
System.out.println(exceptionMessageList.get(exceptionKeyword));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package calculator;
2+
3+
public class MultiplyOperation implements OperationInterface {
4+
@Override
5+
public int operationPriority() {
6+
return 1;
7+
}
8+
9+
@Override
10+
public String operationName() {
11+
return "*";
12+
}
13+
14+
@Override
15+
public int calculation(int beforeNumber, int afterNumber) {
16+
int Result = beforeNumber * afterNumber;
17+
return Result;
18+
}
19+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package calculator;
2+
3+
interface OperationInterface {
4+
public int operationPriority();
5+
6+
public String operationName();
7+
8+
public int calculation(int beforeNumber, int afterNumber);
9+
}
10+
11+
12+
13+
14+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package calculator;
2+
3+
public class SubOperation implements OperationInterface {
4+
@Override
5+
public int operationPriority() {
6+
return 1;
7+
}
8+
9+
@Override
10+
public String operationName() {
11+
return "-";
12+
}
13+
14+
@Override
15+
public int calculation(int beforeNumber, int afterNumber) {
16+
int Result = beforeNumber - afterNumber;
17+
return Result;
18+
}
19+
}

src/main/java/empty.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)