-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathCalculator.java
More file actions
60 lines (51 loc) · 1.49 KB
/
Calculator.java
File metadata and controls
60 lines (51 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class Calculator {
public int calculator(String value) {
String[] values = isBlank(value);
int result = stringToInt(values[0]);
for (int i = 1; i < values.length; i += 2) {
result = calc(values[i],result,stringToInt(values[i+1]));
}
return result;
}
public int calc(String value, int result, int nextNumber) {
switch(value){
case "+" :
return sum(result, nextNumber);
case "-" :
return sub(result, nextNumber);
case "/" :
return divide(result, nextNumber);
case "*" :
return multiply(result, nextNumber);
default:
return -1;
}
}
public String[] isBlank(String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("입력값을 확인해 주세요");
}
return value.split(" ");
}
public int stringToInt(String value) {
return Integer.parseInt(value);
}
public int sum(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public int divide(int a, int b) {
try {
return a / b;
} catch (Exception e) {
System.out.println("0으로 나눌 수 없습니다.");
return 0;
}
}
public int multiply(int a, int b) {
return a * b;
}
}