|
@@ -8,9 +8,9 @@ public class Calculations
|
8
|
8
|
public static ArrayList<String> library = new ArrayList<String>();
|
9
|
9
|
public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
|
10
|
10
|
public static Pemdas pemdas = new Pemdas();
|
11
|
|
-
|
|
11
|
+
|
12
|
12
|
public Calculations(){
|
13
|
|
-
|
|
13
|
+
|
14
|
14
|
library.add("multiply");
|
15
|
15
|
library.add("divide");
|
16
|
16
|
library.add("subtract");
|
|
@@ -27,7 +27,7 @@ public class Calculations
|
27
|
27
|
library.add("resetM");
|
28
|
28
|
library.add("recallM");
|
29
|
29
|
library.add("displayM");
|
30
|
|
-
|
|
30
|
+
|
31
|
31
|
}
|
32
|
32
|
|
33
|
33
|
public static String getCommand(String userInput)
|
|
@@ -35,7 +35,7 @@ public class Calculations
|
35
|
35
|
boolean foundCommand = false;
|
36
|
36
|
String command = "";
|
37
|
37
|
String result = "";
|
38
|
|
-
|
|
38
|
+
|
39
|
39
|
String[] inputArr = userInput.split(" ");
|
40
|
40
|
for(int i = 0; i < library.size(); i ++)
|
41
|
41
|
{
|
|
@@ -47,43 +47,43 @@ public class Calculations
|
47
|
47
|
break;
|
48
|
48
|
}
|
49
|
49
|
}
|
50
|
|
-
|
|
50
|
+
|
51
|
51
|
switch(command)
|
52
|
52
|
{
|
53
|
53
|
case "multiply":
|
54
|
54
|
result = Multiply(userInput);
|
55
|
55
|
break;
|
56
|
|
-
|
|
56
|
+
|
57
|
57
|
case "divide":
|
58
|
58
|
result = Divide(userInput);
|
59
|
59
|
break;
|
60
|
|
-
|
|
60
|
+
|
61
|
61
|
case "add":
|
62
|
62
|
result = Add(userInput);
|
63
|
63
|
break;
|
64
|
|
-
|
|
64
|
+
|
65
|
65
|
case "subtract":
|
66
|
66
|
result = Subtract(userInput);
|
67
|
67
|
break;
|
68
|
|
-
|
|
68
|
+
|
69
|
69
|
case "invertS":
|
70
|
70
|
result = InvertSign(userInput);
|
71
|
71
|
break;
|
72
|
|
-
|
|
72
|
+
|
73
|
73
|
case "invertN":
|
74
|
74
|
result = InvertNumber(userInput);
|
75
|
|
- break;
|
76
|
|
-
|
|
75
|
+ break;
|
|
76
|
+
|
77
|
77
|
case "clear":
|
78
|
78
|
Clear();
|
79
|
79
|
break;
|
80
|
80
|
}
|
81
|
81
|
return result;
|
82
|
82
|
}
|
83
|
|
-
|
|
83
|
+
|
84
|
84
|
public static ArrayList<Double> getNumbers(String userInput){
|
85
|
85
|
ArrayList<Double> results = new ArrayList<Double>();
|
86
|
|
-
|
|
86
|
+
|
87
|
87
|
//takes in a string
|
88
|
88
|
//finds the first occurence of a number
|
89
|
89
|
//store it as a double ( left )
|
|
@@ -94,27 +94,27 @@ public class Calculations
|
94
|
94
|
//if the size of the arraylist is 2
|
95
|
95
|
//return the array list
|
96
|
96
|
//else return null'
|
97
|
|
-
|
|
97
|
+
|
98
|
98
|
Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
|
99
|
99
|
Matcher matcher = pattern.matcher(userInput);
|
100
|
|
-
|
|
100
|
+
|
101
|
101
|
while(matcher.find()){
|
102
|
102
|
String number = matcher.group().trim();
|
103
|
103
|
Double deci = Double.valueOf(number);
|
104
|
104
|
|
105
|
105
|
results.add(deci);
|
106
|
106
|
}
|
107
|
|
-
|
|
107
|
+
|
108
|
108
|
if(results.size() == 2)
|
109
|
|
- {
|
|
109
|
+ {
|
110
|
110
|
return results;
|
111
|
111
|
}
|
112
|
112
|
return null;
|
113
|
113
|
}
|
114
|
|
-
|
|
114
|
+
|
115
|
115
|
public static ArrayList<Double> getNumber(String userInput){
|
116
|
116
|
ArrayList<Double> results = new ArrayList<Double>();
|
117
|
|
-
|
|
117
|
+
|
118
|
118
|
//takes in a string
|
119
|
119
|
//finds the first occurence of a number
|
120
|
120
|
//store it as a double ( left )
|
|
@@ -125,27 +125,27 @@ public class Calculations
|
125
|
125
|
//if the size of the arraylist is 2
|
126
|
126
|
//return the array list
|
127
|
127
|
//else return null'
|
128
|
|
-
|
|
128
|
+
|
129
|
129
|
Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
|
130
|
130
|
Matcher matcher = pattern.matcher(userInput);
|
131
|
|
-
|
|
131
|
+
|
132
|
132
|
while(matcher.find()){
|
133
|
133
|
String number = matcher.group().trim();
|
134
|
134
|
Double deci = Double.valueOf(number);
|
135
|
|
-
|
|
135
|
+
|
136
|
136
|
results.add(deci);
|
137
|
|
-
|
|
137
|
+
|
138
|
138
|
}
|
139
|
|
-
|
|
139
|
+
|
140
|
140
|
if(results.size() == 1)
|
141
|
|
- {
|
|
141
|
+ {
|
142
|
142
|
System.out.println(results.get(0));
|
143
|
143
|
return results;
|
144
|
144
|
}
|
145
|
|
-
|
|
145
|
+
|
146
|
146
|
return null;
|
147
|
147
|
}
|
148
|
|
-
|
|
148
|
+
|
149
|
149
|
public static String Multiply(String userInput)
|
150
|
150
|
{
|
151
|
151
|
String result = "";
|
|
@@ -175,9 +175,9 @@ public class Calculations
|
175
|
175
|
} else {
|
176
|
176
|
return invalidArgumentsAmountError;
|
177
|
177
|
}
|
178
|
|
-
|
179
|
|
-
|
180
|
|
- }
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+ }
|
181
|
181
|
|
182
|
182
|
public static String Add(String userInput)
|
183
|
183
|
{
|
|
@@ -191,9 +191,9 @@ public class Calculations
|
191
|
191
|
} else {
|
192
|
192
|
return invalidArgumentsAmountError;
|
193
|
193
|
}
|
194
|
|
-
|
|
194
|
+
|
195
|
195
|
}
|
196
|
|
-
|
|
196
|
+
|
197
|
197
|
public static String Subtract(String userInput)
|
198
|
198
|
{
|
199
|
199
|
//perform the operation
|
|
@@ -209,24 +209,26 @@ public class Calculations
|
209
|
209
|
} else {
|
210
|
210
|
return invalidArgumentsAmountError;
|
211
|
211
|
}
|
212
|
|
-
|
|
212
|
+
|
213
|
213
|
}
|
214
|
|
-
|
|
214
|
+
|
215
|
215
|
public static String InvertSign(String userInput)
|
216
|
216
|
{
|
|
217
|
+ ArrayList <Double> number1 = getNumber(userInput);
|
|
218
|
+ return number.get(0) * -1;
|
217
|
219
|
//perform the operation
|
218
|
220
|
//convert to string
|
219
|
221
|
//return string
|
220
|
222
|
return null;
|
221
|
|
- }
|
222
|
|
-
|
|
223
|
+ }
|
|
224
|
+
|
223
|
225
|
public static String InvertNumber(String userInput)
|
224
|
226
|
{
|
225
|
227
|
//perform the operation
|
226
|
228
|
//convert to string
|
227
|
229
|
//return string
|
228
|
230
|
return null;
|
229
|
|
- }
|
|
231
|
+ }
|
230
|
232
|
|
231
|
233
|
public static void Clear()
|
232
|
234
|
{
|