-
Notifications
You must be signed in to change notification settings - Fork 0
/
TipCalculatorModel.swift
51 lines (39 loc) · 1.01 KB
/
TipCalculatorModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// GaMobMelb.swift
// GAMOBMELB
//
// Created by ROHIT GOYAL on 17/04/2015.
// Copyright (c) 2015 RohitGoyal. All rights reserved.
//
import Foundation
// 1
class TipCalculatorModel {
// 2
var total: Double
var taxPct: Double
var subtotal: Double {
get {
return total / (taxPct + 1)
}
}
// 3
init(total: Double, taxPct: Double) {
self.total = total
self.taxPct = taxPct
}
// 4
func calcTipWithTipPct(tipPct: Double) -> Double {
return subtotal * tipPct
}
// 5
func returnPossibleTips() -> [Int: Double] {
let possibleTipsInferred = [0.15, 0.18, 0.20]
let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]
var retval = [Int: Double]()
for possibleTip in possibleTipsInferred {
let intPct = Int(possibleTip*100)
retval[intPct] = calcTipWithTipPct(possibleTip)
}
return retval
}
}