Skip to content

Commit

Permalink
Completes extra credit 2 and closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Vanhoef authored and Peter Vanhoef committed Apr 2, 2017
1 parent 2839b45 commit 86e09ef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Calculator/Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
C6586B6D1E7EB8B700AE8920 /* CalculatorUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6586B6C1E7EB8B700AE8920 /* CalculatorUITests.swift */; };
C6A38E051E91592900064B44 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6A38E041E91592900064B44 /* Constants.swift */; };
C6CE27911E7D733D004FCFC3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CE27901E7D733D004FCFC3 /* AppDelegate.swift */; };
C6CE27931E7D733D004FCFC3 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CE27921E7D733D004FCFC3 /* ViewController.swift */; };
C6CE27961E7D733D004FCFC3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C6CE27941E7D733D004FCFC3 /* Main.storyboard */; };
Expand Down Expand Up @@ -39,6 +40,7 @@
C6586B6A1E7EB8B700AE8920 /* CalculatorUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CalculatorUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
C6586B6C1E7EB8B700AE8920 /* CalculatorUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorUITests.swift; sourceTree = "<group>"; };
C6586B6E1E7EB8B700AE8920 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C6A38E041E91592900064B44 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
C6CE278D1E7D733D004FCFC3 /* Calculator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Calculator.app; sourceTree = BUILT_PRODUCTS_DIR; };
C6CE27901E7D733D004FCFC3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C6CE27921E7D733D004FCFC3 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,6 +115,7 @@
C6CE27921E7D733D004FCFC3 /* ViewController.swift */,
C6CE27941E7D733D004FCFC3 /* Main.storyboard */,
C6F47CD41E7D969700E92C9E /* CalculatorBrain.swift */,
C6A38E041E91592900064B44 /* Constants.swift */,
C6CE27B01E7D7391004FCFC3 /* Supporting Files */,
);
path = Calculator;
Expand Down Expand Up @@ -287,6 +290,7 @@
C6F47CD51E7D969700E92C9E /* CalculatorBrain.swift in Sources */,
C6CE27931E7D733D004FCFC3 /* ViewController.swift in Sources */,
C6CE27911E7D733D004FCFC3 /* AppDelegate.swift in Sources */,
C6A38E051E91592900064B44 /* Constants.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 6 additions & 1 deletion Calculator/Calculator/CalculatorBrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ struct CalculatorBrain {

mutating func setOperand(_ operand: Double) {
accumulator = operand
accumulatorString = String(format: "%g", operand)

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.usesGroupingSeparator = false
numberFormatter.maximumFractionDigits = Constants.numberOfDigitsAfterDecimalPoint
accumulatorString = numberFormatter.string(from: NSNumber(value: operand))
}

var result: Double? {
Expand Down
13 changes: 13 additions & 0 deletions Calculator/Calculator/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Constants.swift
// Calculator
//
// Created by Peter Vanhoef on 2/04/17.
// Copyright © 2017 Peter Vanhoef. All rights reserved.
//

import Foundation

struct Constants {
static let numberOfDigitsAfterDecimalPoint = 6
}
6 changes: 5 additions & 1 deletion Calculator/Calculator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class ViewController: UIViewController {
return Double(display.text!)!
}
set {
display.text = String(format: "%g", newValue)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.usesGroupingSeparator = false
numberFormatter.maximumFractionDigits = Constants.numberOfDigitsAfterDecimalPoint
display.text = numberFormatter.string(from: NSNumber(value: newValue))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Calculator/CalculatorTests/CalculatorBrainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class CalculatorBrainTests: XCTestCase {
testBrain.performOperation("+")
testBrain.setOperand(1)
testBrain.performOperation("=")
XCTAssertEqual(testBrain.description, "4.123456 + 1")
XCTAssertEqual(testBrain.description, "4.123457 + 1")
XCTAssertFalse(testBrain.resultIsPending)
XCTAssertTrue(abs(testBrain.result! - 5.123456) < 0.0001)
}
Expand Down
6 changes: 3 additions & 3 deletions Calculator/CalculatorUITests/CalculatorUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ class CalculatorUITests: XCTestCase {
app.buttons["8"].tap()
app.buttons["9"].tap()
app.buttons["+"].tap()
XCTAssert(app.staticTexts["4.123456 + …"].exists)
XCTAssert(app.staticTexts["4.123457 + …"].exists)
app.buttons["1"].tap()
app.buttons["="].tap()
XCTAssert(app.staticTexts["4.123456 + 1 ="].exists)
XCTAssert(app.staticTexts["5.123456"].exists)
XCTAssert(app.staticTexts["4.123457 + 1 ="].exists)
XCTAssert(app.staticTexts["5.123457"].exists)
}
}

0 comments on commit 86e09ef

Please sign in to comment.