瀏覽代碼

memory update

Yauheni Papko 6 年之前
父節點
當前提交
04debbf781
共有 3 個檔案被更改,包括 27 行新增22 行删除
  1. 15
    0
      Calculator.java
  2. 1
    2
      Display.java
  3. 11
    20
      Memory.java

+ 15
- 0
Calculator.java 查看文件

@@ -9,6 +9,8 @@ public class Calculator
9 9
 {
10 10
     public static void  runCommandLoop() {
11 11
         Display display = new Display();
12
+        Memory memory = new Memory();
13
+        memory.resetMemory();
12 14
         String initial = Console.getStringInput("Enter number");
13 15
         display.setDisplay(initial);
14 16
         while (true) {  
@@ -86,6 +88,19 @@ public class Calculator
86 88
                 Console.println(Double.toString(out));
87 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 104
             /*String s = Console.getStringInput("Enter a string");
90 105
             Integer i = Console.getIntegerInput("Enter an integer");
91 106
             Double d = Console.getDoubleInput("Enter a double.");

+ 1
- 2
Display.java 查看文件

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

+ 11
- 20
Memory.java 查看文件

@@ -7,27 +7,18 @@
7 7
  */
8 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
 }