|
@@ -6,6 +6,7 @@ public class Calculations
|
6
|
6
|
{
|
7
|
7
|
public static Console console = new Console();
|
8
|
8
|
public static ArrayList<String> library = new ArrayList<String>();
|
|
9
|
+ public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
|
9
|
10
|
|
10
|
11
|
public Calculations(){
|
11
|
12
|
|
|
@@ -28,7 +29,7 @@ public class Calculations
|
28
|
29
|
|
29
|
30
|
}
|
30
|
31
|
|
31
|
|
- public static void getCommand(String userInput)
|
|
32
|
+ public static String getCommand(String userInput)
|
32
|
33
|
{
|
33
|
34
|
boolean foundCommand = false;
|
34
|
35
|
String command = "";
|
|
@@ -76,12 +77,12 @@ public class Calculations
|
76
|
77
|
Clear();
|
77
|
78
|
break;
|
78
|
79
|
}
|
79
|
|
- System.out.println(result);
|
80
|
|
- //return result;
|
|
80
|
+ return result;
|
81
|
81
|
}
|
82
|
82
|
|
83
|
83
|
public static ArrayList<Double> getNumbers(String userInput){
|
84
|
84
|
ArrayList<Double> results = new ArrayList<Double>();
|
|
85
|
+
|
85
|
86
|
//takes in a string
|
86
|
87
|
//finds the first occurence of a number
|
87
|
88
|
//store it as a double ( left )
|
|
@@ -92,6 +93,7 @@ public class Calculations
|
92
|
93
|
//if the size of the arraylist is 2
|
93
|
94
|
//return the array list
|
94
|
95
|
//else return null'
|
|
96
|
+
|
95
|
97
|
Pattern pattern = Pattern.compile("\\s?\\.?\\d+\\.?(\\d+)?\\s?");
|
96
|
98
|
Matcher matcher = pattern.matcher(userInput);
|
97
|
99
|
|
|
@@ -101,19 +103,25 @@ public class Calculations
|
101
|
103
|
results.add(deci);
|
102
|
104
|
}
|
103
|
105
|
|
104
|
|
- return results;
|
|
106
|
+ if(results.size() == 2)
|
|
107
|
+ {
|
|
108
|
+ return results;
|
|
109
|
+ }
|
|
110
|
+ return null;
|
105
|
111
|
}
|
106
|
112
|
|
107
|
113
|
public static String Multiply(String userInput)
|
108
|
114
|
{
|
109
|
|
- //get getNumbers
|
110
|
|
- //perform the operation
|
111
|
|
- //convert to string
|
112
|
|
- //return string
|
113
|
|
-
|
114
|
|
-
|
115
|
|
-
|
116
|
|
- return null;
|
|
115
|
+ String result = "";
|
|
116
|
+ ArrayList<Double> results = getNumbers(userInput);
|
|
117
|
+ if(results != null)
|
|
118
|
+ {
|
|
119
|
+ Double product = results.get(0) * results.get(1);
|
|
120
|
+ result = String.valueOf(product);
|
|
121
|
+ return result;
|
|
122
|
+ } else {
|
|
123
|
+ return invalidArgumentsAmountError;
|
|
124
|
+ }
|
117
|
125
|
}
|
118
|
126
|
|
119
|
127
|
public static String Divide(String userInput)
|