Kaynağa Gözat

Updated console and methods connecting to it

Trinh Tong 6 yıl önce
ebeveyn
işleme
1f1b2ca166
5 değiştirilmiş dosya ile 121 ekleme ve 98 silme
  1. 0
    2
      Basic.java
  2. 118
    2
      Console.java
  3. 1
    68
      MainApplication.java
  4. 2
    13
      SwitchDisplay.java
  5. 0
    13
      Trig.java

+ 0
- 2
Basic.java Dosyayı Görüntüle

@@ -15,8 +15,6 @@ public class Basic
15 15
     
16 16
     public double Basic()
17 17
     {
18
-        //a check to make sure user input is valid.
19
-        boolean validInput = false;
20 18
         
21 19
         //Get input of first number
22 20
         input1 = Console.getDoubleInput("What is the first number?");

+ 118
- 2
Console.java Dosyayı Görüntüle

@@ -1,12 +1,128 @@
1
- 
2 1
 
3 2
 import java.util.Scanner;
4
-
5 3
 /**
6 4
  * Created by leon on 2/9/18.
7 5
  */
8 6
 public class Console {
9 7
 
8
+    public static void main(String[] args) {
9
+        double answer;
10
+        // Create all instances of classes to be used for this program
11
+        MainMenu mainMenu = new MainMenu();
12
+        Basic basicCalc = new Basic();
13
+        SciCalc scientificCalc = new SciCalc();
14
+        MemoryFunc memory = new MemoryFunc();
15
+        SwitchDisplay baseChange = new SwitchDisplay();
16
+        Trig trigMenu = new Trig();
17
+
18
+        // Stored memory value should be able to be used throughout whole application
19
+        Double memoryValue = memory.memory;
20
+
21
+        // blanket input statement 
22
+        // create input methods to call when we need them with specific types
23
+        // we use 1 input variable statement to call the user to input, then manipulate
24
+        // Can user input M if native type is double
25
+        // if any input = "m", that input now = memoryValue;
26
+
27
+        // Menu Input
28
+        String menuInput = " ";
29
+        // Giant while loop that links back to main menu
30
+        while (!menuInput.equals("4")) {
31
+            mainMenu.mainMenuDisplay();
32
+            menuInput = Console.getStringInput("Enter the calculator you want to use by key. (ie. 1 for basic, etc.)");
33
+
34
+            switch (menuInput) {
35
+                case "1":
36
+                basicCalc.Basic();
37
+
38
+                double basicInput1 = Console.getIntegerInput("Enter the first input: ");
39
+
40
+                String basicMode = Console.getStringInput("Enter the mode: ");
41
+
42
+                double basicInput2 = Console.getIntegerInput("Enter the second input: ");
43
+
44
+                if (basicMode.equals("+")) {
45
+                    basicCalc.getSum(basicInput1, basicInput2);
46
+
47
+                    //subtraction    
48
+                } else if (basicMode.equals("-")) {
49
+                    basicCalc.getDiff(basicInput1, basicInput2);
50
+
51
+                    //multiplication
52
+                } else if (basicMode.equals("*")) {
53
+                    basicCalc.getProduct(basicInput1, basicInput2);
54
+
55
+                    //division    
56
+                } else if (basicMode.equals("/")) {
57
+                    //error message when trying to divide by 0.
58
+                    if (basicInput2 == 0) {
59
+                        Console.println("undefined");
60
+                        //Insert return to main menu.
61
+                    } else {
62
+                        basicCalc.getQuotient(basicInput1, basicInput2);
63
+                    }
64
+                } else {
65
+                    //error if wrong input was entered
66
+                    answer = Double.NaN;
67
+                    Console.println("Invalid Input");
68
+                }
69
+
70
+                
71
+                break;
72
+
73
+                case "2":
74
+
75
+                // Sends to Advanced Calculator Functions
76
+                break;
77
+
78
+                case "3":
79
+
80
+                // Sends to Scientific Calculator Functions
81
+                // displays the menu for scientific calc
82
+                scientificCalc.toSciMenu();
83
+                String sciMenuInput = Console.getStringInput("Enter the function you'd like to use by key.");
84
+                // if statement to use various calc functions
85
+                if (sciMenuInput.equals("1")) {
86
+                    // Base Conversions
87
+                    // while loop for display 
88
+                    String mode = "";
89
+                    while (!mode.equals("Invalid Input")) {
90
+                        baseChange.switchDisplay(); 
91
+                        mode = Console.getStringInput("Enter the desired display mode: ");
92
+                    }
93
+
94
+                    int userInput = getIntegerInput("Enter the number you want to convert to selected base: ");
95
+                    baseChange.switchDisplayOutput(mode, userInput);
96
+
97
+                    // display mode
98
+                    // gets mode
99
+                    // asks for input
100
+                } else if (sciMenuInput.equals("2")) {
101
+                    // Memory
102
+                    memory.memoryMenu();
103
+                } else if (sciMenuInput.equals("3")) {
104
+                    // To Trig Menu
105
+                    answer = trigMenu.TrigFunctions();
106
+                    Console.println(Double.toString(answer));
107
+                    String response = Console.getStringInput("Would you like to convert your answer from radian to degrees? Y or N").toLowerCase();
108
+
109
+                    if (response.equals("y") ) {
110
+                        trigMenu.toDegrees(answer);
111
+                    }
112
+                    break;
113
+                } else {
114
+                    break;
115
+                }
116
+                break;
117
+
118
+                case "4":
119
+                // Quits the calculator
120
+                break;
121
+            }
122
+
123
+        }
124
+    }
125
+
10 126
     public static void print(String output, Object... args) {
11 127
         System.out.printf(output, args);
12 128
     }

+ 1
- 68
MainApplication.java Dosyayı Görüntüle

@@ -6,73 +6,6 @@ import java.util.*;
6 6
 public class MainApplication {
7 7
     
8 8
     public static void main(String[] args) {
9
-        // Create all instances of classes to be used for this program
10
-        MainMenu mainMenu = new MainMenu();
11
-        SciCalc scientificCalc = new SciCalc();
12
-        MemoryFunc memory = new MemoryFunc();
13
-        SwitchDisplay baseChange = new SwitchDisplay();
14
-        
15
-        // Stored memory value should be able to be used throughout whole application
16
-        Double memoryValue = memory.memory;
17
-        
18
-        // if any input = "m", that input now = the memory value
19
-        
20
-        // Menu Input
21
-        String menuInput = " ";
22
-        // Giant while loop that links back to main menu
23
-        while (!menuInput.equals("4")) {
24
-            mainMenu.mainMenuDisplay();
25
-            menuInput = Console.getStringInput("Enter the calculator you want to use by key. (ie. 1 for basic, etc.)");
26
-           
27
-            switch (menuInput) {
28
-                case "1":
29
-                
30
-                    // Sends to basic calculator functions
31
-                    break;
32
-                
33
-                case "2":
34
-                
35
-                    // Sends to Advanced Calculator Functions
36
-                    break;
37
-                
38
-                case "3":
39
-                
40
-                    // Sends to Scientific Calculator Functions
41
-                    // displays the menu for scientific calc
42
-                    scientificCalc.toSciMenu();
43
-                    String sciMenuInput = Console.getStringInput("Enter the function you'd like to use by key.");
44
-                    // if statement to use various calc functions
45
-                    if (sciMenuInput.equals("1")) {
46
-                        // Base Conversions
47
-                        baseChange.switchDisplay();
48
-                        
49
-                    } else if (sciMenuInput.equals("2")) {
50
-                        // Memory
51
-                        memory.memoryMenu();
52
-                    } else if (sciMenuInput.equals("3")) {
53
-                        // To Trig Menu
54
-                        
55
-                    } else {
56
-                        break;
57
-                    }
58
-                    break;
59
-                    
60
-                case "4":
61
-                    // Quits the calculator
62
-                    break;
63
-                }
64
-
65
-        }
66
-
67
-        Console.println("Welcome to my calculator!");
68
-        /** String s = Console.getStringInput("Enter a string");
69
-        Integer i = Console.getIntegerInput("Enter an integer");
70
-        Double d = Console.getDoubleInput("Enter a double.");
71
-
72
-        Console.println("The user input %s as a string", s);
73
-        Console.println("The user input %s as a integer", i);
74
-        Console.println("The user input %s as a d", d);
75
-        */
76
-
9
+        Console console = new Console();
77 10
     }
78 11
 }

+ 2
- 13
SwitchDisplay.java Dosyayı Görüntüle

@@ -30,30 +30,19 @@ public class SwitchDisplay
30 30
      * switchDisplayMode(String mode) sets the display to the mode given
31 31
      */
32 32
     
33
-    public void switchDisplay()
33
+    public String switchDisplay()
34 34
     {
35 35
         String mode = "Invalid Input";
36 36
         
37 37
         while (mode.equals("Invalid Input")) {
38
-            int baseInput = Console.getIntegerInput("Enter an integer to see the base conversions.");
39
-    
40 38
             Console.println("Display Options" 
41 39
                              + "\n1: Binary"
42 40
                              + "\n2: Octal"
43 41
                              + "\n3: Decimal"
44 42
                              + "\n4: Hexadecimal"
45 43
                              + "\n5: Cancel - returns to Main Menu");
46
-                             
47
-            // Asks for the mode
48
-            mode = Console.getStringInput("Enter the desired display mode: ");
49
-            
50
-            String convertedInput = switchDisplayOutput(mode, baseInput);
51
-            
52
-            Console.println(convertedInput); 
53
-            // passes to switchDisplayOutput(mode) >> returns string
54
-            // displays the returned string
55
-            // while loops !cancel
56 44
         }
45
+        return mode = Console.getStringInput("Enter the desired display mode: ");
57 46
                          
58 47
     }
59 48
     

+ 0
- 13
Trig.java Dosyayı Görüntüle

@@ -12,19 +12,6 @@ public class Trig
12 12
     double answer;
13 13
     String operation = "";
14 14
     
15
-    public double Trig() {
16
-        TrigFunctions();
17
-        
18
-        //Ask if they would like to convert.
19
-        String response = Console.getStringInput("Would you like to convert your answer from radian to degrees?  Y or N");
20
-        response = response.toLowerCase();
21
-        if (response.equals("y")) {
22
-            toDegrees(answer);
23
-    }
24
-    
25
-        return answer;
26
-}
27
-    
28 15
     public double TrigFunctions()
29 16
     {
30 17
         //a check to make sure user input is valid