Przeglądaj źródła

Added division, subtraction, addition.. getNumber()

Elliott Stansbury 6 lat temu
rodzic
commit
4f01b86084
2 zmienionych plików z 85 dodań i 23 usunięć
  1. 69
    7
      Calculations.java
  2. 16
    16
      package.bluej

+ 69
- 7
Calculations.java Wyświetl plik

@@ -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)

+ 16
- 16
package.bluej Wyświetl plik

@@ -8,19 +8,19 @@ dependency2.type=UsesDependency
8 8
 dependency3.from=Calculations
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=800
12
-editor.fx.0.width=1280
13
-editor.fx.0.x=0
14
-editor.fx.0.y=0
15
-objectbench.height=200
11
+editor.fx.0.height=709
12
+editor.fx.0.width=800
13
+editor.fx.0.x=240
14
+editor.fx.0.y=23
15
+objectbench.height=198
16 16
 objectbench.width=595
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.6854103343465046
19
-package.editor.height=428
18
+package.divider.vertical=0.685099846390169
19
+package.editor.height=439
20 20
 package.editor.width=493
21 21
 package.editor.x=35
22 22
 package.editor.y=23
23
-package.frame.height=716
23
+package.frame.height=709
24 24
 package.frame.width=619
25 25
 package.numDependencies=3
26 26
 package.numTargets=3
@@ -37,19 +37,19 @@ target1.name=Calculations
37 37
 target1.showInterface=false
38 38
 target1.type=ClassTarget
39 39
 target1.width=100
40
-target1.x=310
41
-target1.y=260
40
+target1.x=240
41
+target1.y=140
42 42
 target2.height=50
43 43
 target2.name=Console
44 44
 target2.showInterface=false
45 45
 target2.type=ClassTarget
46 46
 target2.width=80
47
-target2.x=190
48
-target2.y=160
49
-target3.height=120
47
+target2.x=30
48
+target2.y=200
49
+target3.height=50
50 50
 target3.name=MainApplication
51 51
 target3.showInterface=false
52 52
 target3.type=ClassTarget
53
-target3.width=230
54
-target3.x=320
55
-target3.y=20
53
+target3.width=120
54
+target3.x=70
55
+target3.y=70