-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorUpdated.java
More file actions
176 lines (161 loc) · 6 KB
/
CalculatorUpdated.java
File metadata and controls
176 lines (161 loc) · 6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package javaapplication2;
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Calculator calk = new Calculator();
String operation, operTwo = "";
while (true) {
calk.first = 0;
calk.second = 0;
System.out.print("Enter operation \n \'+\' \n \'-\' \n \'*\' \n \'/\' \n \'M\' \nif you want to exit, print - \'exit\'\n >: ");
operation = sc.next();
if (!operation.equalsIgnoreCase("M") &&
!operation.equalsIgnoreCase("exit") &&
!operation.equalsIgnoreCase("+") &&
!operation.equalsIgnoreCase("-") &&
!operation.equalsIgnoreCase("*") &&
!operation.equalsIgnoreCase("/")
)
{
System.out.println(" Error:\n \'" + operation + "\' isn't operation\n");
continue;
}
if (operation.equalsIgnoreCase("M")) {
System.out.print("Enter next operation: ");
operTwo = sc.next();
if (!operTwo.equalsIgnoreCase("+") &&
!operTwo.equalsIgnoreCase("-") &&
!operTwo.equalsIgnoreCase("*") &&
!operTwo.equalsIgnoreCase("/")
)
{
System.out.println(" Error:\n \'" + operTwo + "\' is not operation\n");
continue;
}
}
else if(operation.equalsIgnoreCase("exit"))
{
break;
}
System.out.print("Enter first number (\'M\' - " + calk.res + " ) \n >: ");
String firstNum = sc.next();
if (!firstNum.equalsIgnoreCase("M") && !calk.checkString(firstNum))
{
System.out.println(" Error \n\'" + firstNum + "\' is not number or \'M\'\n");
continue;
}
System.out.print("Enter second number (\'M\' - " + calk.res + " ) \n >: ");
String secNum = sc.next();
if (!secNum.equalsIgnoreCase("M") && !calk.checkString(secNum)){
System.out.println(" Error \n\'" + secNum + "\' is not number or \'M\'\n");
continue;
}
if (operation.equalsIgnoreCase("M")){
System.out.println("Result: " + calk.operWithM(operTwo, firstNum, secNum));
}
else if (!operation.equalsIgnoreCase("M")){
System.out.println("Result: " + calk.operNotM(operation, firstNum, secNum));
}
System.out.println();
}
}
}
class Calculator{
double res = 0;
double first = 0;
double second = 0;
double operNotM(String operation, String firstNum, String secNum){
switch (operation) {
case "+": return plusNotM(firstNum,secNum);
case "-": return minusNotM(firstNum,secNum);
case "*": return multiplyNotM(firstNum,secNum);
case "/": return divideNotM(firstNum,secNum);
}
return 0;
}
double operWithM(String operTwo, String firstNum, String secNum){
switch (operTwo) {
case "+": return plusM(firstNum,secNum);
case "-": return minusM(firstNum,secNum);
case "*": return multiplyM(firstNum,secNum);
case "/": return divideM(firstNum,secNum);
}
return 0;
}
private void checkM(String firstNum, String secNum){
if (firstNum.equalsIgnoreCase("M") &&
!secNum.equalsIgnoreCase("M")){
first = res;
second = Double.parseDouble(secNum);
}
else if (!firstNum.equalsIgnoreCase("M") &&
secNum.equalsIgnoreCase("M")){
first = Double.parseDouble(firstNum);
second = res;
}
else if(firstNum.equalsIgnoreCase("M") &&
secNum.equalsIgnoreCase("M")){
first = res;
second = res;
}
else {
first = Double.parseDouble(firstNum);
second = Double.parseDouble(secNum);
}
}
private double plusNotM(String firstNum, String secNum){
checkM(firstNum, secNum);
return first + second;
}
private double minusNotM(String firstNum, String secNum){
checkM(firstNum, secNum);
return first - second;
}
private double multiplyNotM(String firstNum, String secNum){
checkM(firstNum, secNum);
return first * second;
}
private double divideNotM(String firstNum, String secNum){
checkM(firstNum, secNum);
return first / second;
}
private double plusM(String firstNum, String secNum){
checkM(firstNum, secNum);
res = first + second;
return res;
}
private double minusM(String firstNum, String secNum){
checkM(firstNum, secNum);
res = first - second;
return res;
}
private double multiplyM(String firstNum, String secNum){
checkM(firstNum, secNum);
res = first * second;
return res;
}
private double divideM(String firstNum, String secNum){
checkM(firstNum, secNum);
res = first / second;
return res;
}
public boolean checkString(String string) {
if (string == null || string.length() == 0) return false;
int i = 0;
if (string.charAt(0) == '-') {
if (string.length() == 1) {
return false;
}
i = 1;
}
char c;
for (; i < string.length(); i++) {
c = string.charAt(i);
if (!(c >= '0' && c <= '9')) {
return false;
}
}
return true;
}
}