|
@@ -100,7 +100,7 @@ public class Calculations
|
100
|
100
|
while(matcher.find()){
|
101
|
101
|
String number = matcher.group().trim();
|
102
|
102
|
Double deci = Double.valueOf(number);
|
103
|
|
- System.out.println(deci);
|
|
103
|
+
|
104
|
104
|
results.add(deci);
|
105
|
105
|
}
|
106
|
106
|
|
|
@@ -111,6 +111,40 @@ public class Calculations
|
111
|
111
|
return null;
|
112
|
112
|
}
|
113
|
113
|
|
|
114
|
+ public static ArrayList<Double> getNumber(String userInput){
|
|
115
|
+ ArrayList<Double> results = new ArrayList<Double>();
|
|
116
|
+
|
|
117
|
+ //takes in a string
|
|
118
|
+ //finds the first occurence of a number
|
|
119
|
+ //store it as a double ( left )
|
|
120
|
+ //add this to the arraylist
|
|
121
|
+ //find the second occurence of a number
|
|
122
|
+ //store it as a double (right)
|
|
123
|
+ //add this to the array list
|
|
124
|
+ //if the size of the arraylist is 2
|
|
125
|
+ //return the array list
|
|
126
|
+ //else return null'
|
|
127
|
+
|
|
128
|
+ Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
|
|
129
|
+ Matcher matcher = pattern.matcher(userInput);
|
|
130
|
+
|
|
131
|
+ while(matcher.find()){
|
|
132
|
+ String number = matcher.group().trim();
|
|
133
|
+ Double deci = Double.valueOf(number);
|
|
134
|
+
|
|
135
|
+ results.add(deci);
|
|
136
|
+
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ if(results.size() == 1)
|
|
140
|
+ {
|
|
141
|
+ System.out.println(results.get(0));
|
|
142
|
+ return results;
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ return null;
|
|
146
|
+ }
|
|
147
|
+
|
114
|
148
|
public static String Multiply(String userInput)
|
115
|
149
|
{
|
116
|
150
|
String result = "";
|
|
@@ -130,15 +164,33 @@ public class Calculations
|
130
|
164
|
//perform the operation
|
131
|
165
|
//convert to string
|
132
|
166
|
//return string
|
133
|
|
- return null;
|
|
167
|
+ String result = "";
|
|
168
|
+ ArrayList<Double> results = getNumbers(userInput);
|
|
169
|
+ if(results != null)
|
|
170
|
+ {
|
|
171
|
+ Double quotient = results.get(0) / results.get(1);
|
|
172
|
+ result = String.valueOf(quotient);
|
|
173
|
+ return result;
|
|
174
|
+ } else {
|
|
175
|
+ return invalidArgumentsAmountError;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+
|
134
|
179
|
}
|
135
|
180
|
|
136
|
181
|
public static String Add(String userInput)
|
137
|
182
|
{
|
138
|
|
- //perform the operation
|
139
|
|
- //convert to string
|
140
|
|
- //return string
|
141
|
|
- return null;
|
|
183
|
+ String result = "";
|
|
184
|
+ ArrayList<Double> results = getNumbers(userInput);
|
|
185
|
+ if(results != null)
|
|
186
|
+ {
|
|
187
|
+ Double sum = results.get(0) + results.get(1);
|
|
188
|
+ result = String.valueOf(sum);
|
|
189
|
+ return result;
|
|
190
|
+ } else {
|
|
191
|
+ return invalidArgumentsAmountError;
|
|
192
|
+ }
|
|
193
|
+
|
142
|
194
|
}
|
143
|
195
|
|
144
|
196
|
public static String Subtract(String userInput)
|
|
@@ -146,7 +198,17 @@ public class Calculations
|
146
|
198
|
//perform the operation
|
147
|
199
|
//convert to string
|
148
|
200
|
//return string
|
149
|
|
- return null;
|
|
201
|
+ String result = "";
|
|
202
|
+ ArrayList<Double> results = getNumbers(userInput);
|
|
203
|
+ if(results != null)
|
|
204
|
+ {
|
|
205
|
+ Double difference = results.get(0) - results.get(1);
|
|
206
|
+ result = String.valueOf(difference);
|
|
207
|
+ return result;
|
|
208
|
+ } else {
|
|
209
|
+ return invalidArgumentsAmountError;
|
|
210
|
+ }
|
|
211
|
+
|
150
|
212
|
}
|
151
|
213
|
|
152
|
214
|
public static String InvertSign(String userInput)
|