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