Browse Source

memory update

Yauheni Papko 6 years ago
parent
commit
04debbf781
3 changed files with 27 additions and 22 deletions
  1. 15
    0
      Calculator.java
  2. 1
    2
      Display.java
  3. 11
    20
      Memory.java

+ 15
- 0
Calculator.java View File

9
 {
9
 {
10
     public static void  runCommandLoop() {
10
     public static void  runCommandLoop() {
11
         Display display = new Display();
11
         Display display = new Display();
12
+        Memory memory = new Memory();
13
+        memory.resetMemory();
12
         String initial = Console.getStringInput("Enter number");
14
         String initial = Console.getStringInput("Enter number");
13
         display.setDisplay(initial);
15
         display.setDisplay(initial);
14
         while (true) {  
16
         while (true) {  
86
                 Console.println(Double.toString(out));
88
                 Console.println(Double.toString(out));
87
                 display.setDisplay(Double.toString(out));
89
                 display.setDisplay(Double.toString(out));
88
             }
90
             }
91
+            if (command.equalsIgnoreCase("m+")) {
92
+                memory.addMemory(display.getDisplay());
93
+                display.setDisplay(memory.recallMemory());
94
+                Console.println(display.getDisplay());
95
+            }
96
+            if (command.equalsIgnoreCase("mrc")) {
97
+                Console.println(memory.recallMemory());
98
+                display.setDisplay(memory.recallMemory());
99
+            }
100
+            if (command.equalsIgnoreCase("mc")) {
101
+                memory.resetMemory();
102
+                Console.println(display.getDisplay());
103
+            }
89
             /*String s = Console.getStringInput("Enter a string");
104
             /*String s = Console.getStringInput("Enter a string");
90
             Integer i = Console.getIntegerInput("Enter an integer");
105
             Integer i = Console.getIntegerInput("Enter an integer");
91
             Double d = Console.getDoubleInput("Enter a double.");
106
             Double d = Console.getDoubleInput("Enter a double.");

+ 1
- 2
Display.java View File

12
     public String getDisplay() {
12
     public String getDisplay() {
13
         return displayValue;
13
         return displayValue;
14
     }
14
     }
15
-
16
     
15
     
17
     public void setDisplay(String prompt) {
16
     public void setDisplay(String prompt) {
18
         displayValue = prompt;
17
         displayValue = prompt;
19
     }
18
     }
20
     
19
     
21
     public void resetDisplay(String prompt) {
20
     public void resetDisplay(String prompt) {
22
-        displayValue = prompt;
21
+        displayValue = "0";
23
     }
22
     }
24
     
23
     
25
 }
24
 }

+ 11
- 20
Memory.java View File

7
  */
7
  */
8
 public class Memory
8
 public class Memory
9
 {
9
 {
10
-    // instance variables - replace the example below with your own
11
-    private int x;
12
 
10
 
13
-    /**
14
-     * Constructor for objects of class Memory
15
-     */
16
-    public Memory()
17
-    {
18
-        // initialise instance variables
19
-        x = 0;
20
-    }
11
+    private String memoryValue;
21
 
12
 
22
-    /**
23
-     * An example of a method - replace this comment with your own
24
-     *
25
-     * @param  y  a sample parameter for a method
26
-     * @return    the sum of x and y
27
-     */
28
-    public int sampleMethod(int y)
29
-    {
30
-        // put your code here
31
-        return x + y;
13
+    public String recallMemory() {
14
+        return memoryValue;
15
+    }
16
+    
17
+    public void addMemory(String prompt) {
18
+        memoryValue = Double.toString(Double.parseDouble(memoryValue) + Double.parseDouble(prompt));
19
+    }
20
+    
21
+    public void resetMemory() {
22
+        memoryValue = "0";
32
     }
23
     }
33
 }
24
 }