Commits

Jay Yu committed f6ce29f

refactored display labels update code to a separate method

Comments (0)

Files changed (1)

Calculator/CalculatorViewController.m

     double result = [self.brain performOperation:operation];
     self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
     self.display.text = [NSString stringWithFormat:@"%g", result];
-
-//    [self appendHistory:@"="];
 }
 
 - (IBAction)variablePressed:(UIButton *)sender
 {
+    if (self.userIsInTheMiddleOfEnteringANumber) {
+        [self enterPressed];
+    }
     NSString *variable = sender.currentTitle;
     [self.brain pushVariable:variable];
     self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
 {
     NSString *testNumber = sender.currentTitle;
 
+    if (self.userIsInTheMiddleOfEnteringANumber) {
+        [self enterPressed];
+    }
+
     if ([@"Test 1" isEqualToString:testNumber]) {
         self.testVariableValues = [NSDictionary dictionaryWithObjectsAndKeys:
-                                   [NSNumber numberWithDouble:9.23], @"x", 
-                                   [NSNumber numberWithDouble:-20], @"y", 
-                                   [NSNumber numberWithDouble:0], @"z", nil]; 
+                                   [NSNumber numberWithDouble:9.23], @"x",
+                                   [NSNumber numberWithDouble:-20], @"y",
+                                   [NSNumber numberWithDouble:0], @"z", nil];
     }
 
     if ([@"Test 2" isEqualToString:testNumber]) {
         self.testVariableValues = [NSDictionary dictionaryWithObjectsAndKeys:
-                                   [NSNumber numberWithDouble:-2.11], @"x", 
-                                   [NSNumber numberWithDouble:38.000], @"y", 
-                                   [NSNumber numberWithDouble:0.18], @"z", nil]; 
+                                   [NSNumber numberWithDouble:-2.11], @"x",
+                                   [NSNumber numberWithDouble:38.000], @"y",
+                                   [NSNumber numberWithDouble:0.18], @"z", nil];
     }
 
     if ([@"Test 3" isEqualToString:testNumber]) {
-        self.testVariableValues = nil; 
-        self.variablesDisplay.text = @"Variable not set";
-        return;
+        self.testVariableValues = nil;
     }
-    
-    NSMutableArray *varList = [[NSMutableArray alloc] init];
-    for (id variable in [CalculatorBrain variablesUsedInProgram:self.brain.program]) {
-        [varList addObject:[NSString stringWithFormat:@"%@ = %@", variable, [self.testVariableValues objectForKey:variable]]];        
+
+    [self updateDisplays];
+}
+
+- (void)updateDisplays
+{
+    [self updateDisplays:self.brain.program usingVariables:self.testVariableValues];
+}
+
+- (void)updateDisplays:(id)program
+        usingVariables:(NSDictionary *)variables
+{
+    if (! program) {
+        program = self.brain.program;
     }
-    self.variablesDisplay.text = [varList componentsJoinedByString:@", "];
+
+    if (! variables) {
+        variables = self.testVariableValues;
+    }
+
+    double result = [CalculatorBrain runProgram:program
+                            usingVariableValues:self.testVariableValues];
+    self.history.text = [CalculatorBrain descriptionOfProgram:program];
+    self.display.text = [NSString stringWithFormat:@"%g", result];
+
+    if (! variables) {
+        self.variablesDisplay.text = @"Variables not set";
+
+    } else {
+        NSMutableArray *variableList = [[NSMutableArray alloc] init];
+        for (id variable in [CalculatorBrain variablesUsedInProgram:program]) {
+            id object = [self.testVariableValues objectForKey:variable];
+            if (! object) {
+                object = @"0";
+            }
+            [variableList addObject:[NSString stringWithFormat:@"%@ = %@", variable, object]];
+        }
+        self.variablesDisplay.text = [variableList componentsJoinedByString:@", "];
+    }
 }
 
 - (void)viewDidUnload {
     self.history = nil;
     self.variablesDisplay = nil;
-    self.testVariableValues = nil; 
+    self.testVariableValues = nil;
     [self setVariablesDisplay:nil];
     [super viewDidUnload];
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.