Parcourir la source

Basic perations added, need to refine UX

NedRedmond il y a 6 ans
Parent
révision
d6f35cf800
3 fichiers modifiés avec 133 ajouts et 12 suppressions
  1. 5
    0
      Console.java
  2. 69
    8
      opSwitch.java
  3. 59
    4
      simpleOp.java

+ 5
- 0
Console.java Voir le fichier

@@ -85,6 +85,9 @@ public class Console {
85 85
     public static String getOp(){
86 86
         //GET OPERATION FROM PERSON
87 87
             String operation = getStringInput("Please choose your operation.");
88
+            if (operation == null) {
89
+                System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
90
+            }
88 91
             quit(operation);
89 92
             opSwitch.choose(operation);
90 93
             //return opSwitch.choose(operation1);
@@ -94,6 +97,8 @@ public class Console {
94 97
     public static void quit(String answer){
95 98
         if(answer.equals("quit") || answer.equals("exit")){
96 99
                System.exit(0);
100
+        } else if(answer.equals("clear")) {
101
+            System.out.print('\f');
97 102
         }
98 103
     }
99 104
    

+ 69
- 8
opSwitch.java Voir le fichier

@@ -8,14 +8,75 @@
8 8
 public class opSwitch
9 9
 {
10 10
     public static void choose(String opInput){
11
-        //if/else state
12
-        if(opInput.equals("add") || opInput.equals("+")){
13
-            String addRequest1 = Console.getStringInput("Whats the first number you'll like to add?");
14
-            String addRequest2 = Console.getStringInput("Whats the second number you'll like to add?");
15
-            simpleOp.add(Console.getInput(addRequest1), Console.getNumber(addRequest2));
16
-        }else if(opInput.equals("invert")){
17
-            String invertRequest = Console.getStringInput("What number would you like to invert?");
18
-            simpleOp.invert(Console.getInput(invertRequest));
11
+        String x;
12
+        String y;
13
+        // switchboard for operations
14
+        switch (opInput) {
15
+            case "?":
16
+                System.out.println("Commands include:\n"
17
+                +"+"+"\n"
18
+                +"-"+"\n"
19
+                +"*"+"\n"
20
+                +"/"+"\n"
21
+                +"square"+"\n"
22
+                +"square root"+"\n"
23
+                +"exponent"+"\n"
24
+                +"inverse"+"\n"
25
+                +"quit"+"\n"
26
+                +"clear"+"\n");
27
+                break;
28
+            case "+": 
29
+                x = Console.getStringInput("Please enter first number.");
30
+                y = Console.getStringInput("Please enter second number.");
31
+                simpleOp.add(Console.getNumber(x), Console.getNumber(y));
32
+                break;
33
+            case "-": 
34
+                x = Console.getStringInput("Please enter first number.");
35
+                y = Console.getStringInput("Please enter second number.");
36
+                simpleOp.sub(Console.getNumber(x), Console.getNumber(y));
37
+                break;
38
+            case "*": 
39
+                x = Console.getStringInput("Please enter multiplicand.");
40
+                y = Console.getStringInput("Please enter multiplier.");
41
+                simpleOp.mul(Console.getNumber(x), Console.getNumber(y));
42
+                break;
43
+            case "/": 
44
+                x = Console.getStringInput("Please enter dividend.");
45
+                y = Console.getStringInput("Please enter divisor.");
46
+                simpleOp.div(Console.getNumber(x), Console.getNumber(y));
47
+                break;
48
+            case "square": 
49
+                x = Console.getStringInput("Please enter your number.");
50
+                simpleOp.squ(Console.getNumber(x));
51
+                break;
52
+            case "square root": 
53
+                x = Console.getStringInput("Please enter your number.");
54
+                simpleOp.sqrt(Console.getNumber(x));
55
+                break;
56
+            case "exponent": 
57
+                x = Console.getStringInput("Please enter base.");
58
+                y = Console.getStringInput("Please enter exponent.");
59
+                simpleOp.exp(Console.getNumber(x), Console.getNumber(y));
60
+                break;
61
+            case "inverse":
62
+                x = Console.getStringInput("Please enter your number.");
63
+                simpleOp.inv(Console.getNumber(x));
64
+                break;
65
+            // default:
66
+                // System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
67
+                // break;
19 68
         }
69
+        
70
+        // //if/else state
71
+        // if(opInput.equals("add") || opInput.equals("+")){
72
+            // String addRequest1 = Console.getStringInput("Whats the first number you'll like to add?");
73
+            // String addRequest2 = Console.getStringInput("Whats the second number you'll like to add?");
74
+            // simpleOp.add(Console.getInput(addRequest1), Console.getNumber(addRequest2));
75
+        // }else if(opInput.equals("invert")){
76
+            // String invertRequest = Console.getStringInput("What number would you like to invert?");
77
+            // simpleOp.invert(Console.getInput(invertRequest));
78
+        // 
20 79
     }
80
+    
81
+    
21 82
 }

+ 59
- 4
simpleOp.java Voir le fichier

@@ -5,13 +5,67 @@
5 5
  * @author (your name)
6 6
  * @version (a version number or a date)
7 7
  */
8
+import java.util.*;
8 9
 public class simpleOp
9 10
 {
10
-    public static int add(int num1, int num2){
11
+    public static int add(int x, int y){
11 12
         //CALULATES THE SUM OF TWO NUMBERS
12
-        int num3 = num1 + num2;
13
-        System.out.println(num1 + " + " +num2 + " = " + num3);
14
-        return num3;
13
+        int z = x + y;
14
+        System.out.println(x + " + " + y + " = " + z);
15
+        return z;
16
+    }
17
+    
18
+    public static int sub(int x, int y){
19
+        //CALULATES THE DIFFERENCE OF TWO NUMBERS
20
+        int z = x - y;
21
+        System.out.println(x + " - " + y + " = " + z);
22
+        return z;
23
+    }
24
+    
25
+    public static int mul(int x, int y){
26
+        //CALULATES THE PRODUCT OF TWO NUMBERS
27
+        int z = x * y;
28
+        System.out.println(x + " * " + y + " = " + z);
29
+        return z;
30
+    }
31
+    
32
+    public static int div(int x, int y){
33
+        //CALULATES THE QUOTIENT OF TWO NUMBERS
34
+        int z = x / y;
35
+        System.out.println(x + " / " + y + " = " + z);
36
+        return z;
37
+    }
38
+    
39
+    public static int squ(int x){
40
+        //CALULATES THE SQUARE OF ONE NUMBER
41
+        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
42
+        int z = (int) Math.pow(x,2);
43
+        System.out.println(x + "\u00B2 = " + z);
44
+        return z;
45
+    }
46
+    
47
+    public static int sqrt(int x){
48
+        //CALULATES THE SQUARE ROOT OF ONE NUMBER
49
+        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
50
+        int z = (int) Math.sqrt(x);
51
+        System.out.println("√" + x + " = " + z);
52
+        return z;
53
+    }
54
+    
55
+    public static int exp(int x, int y){
56
+        //CALULATES EXPONENTIATION OF X TO POWER OF Y
57
+        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
58
+        int z = (int) Math.pow(x,y);
59
+        System.out.println(x + "^" + y + " = " + z);
60
+        return z;
61
+    }
62
+    
63
+    public static double inv(double x){
64
+        //CALULATES THE INVERSE OF NUMBER
65
+        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
66
+        double z = 1 / x;
67
+        System.out.println("1 / " + x + " = " + z);
68
+        return z;
15 69
     }
16 70
     
17 71
     public static int invert(int number){
@@ -20,4 +74,5 @@ public class simpleOp
20 74
             System.out.println(invertedNumber);
21 75
             return invertedNumber;
22 76
     }
77
+    
23 78
 }