Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Created July 26, 2015 06:18
Show Gist options
  • Save tiagopog/e7294ee28bc0234094bb to your computer and use it in GitHub Desktop.
Save tiagopog/e7294ee28bc0234094bb to your computer and use it in GitHub Desktop.
First Swift code o/
class TipCalculator {
let total: Double
let taxPct: Double
let subtotal: Double
// Class constructor:
init(total: Double, taxPct: Double) {
// Here we need to use "self" so the compiler will not get confused about the variables with the same name:
self.total = total
self.taxPct = taxPct
// No need for "self" here since there is no any other variable with this name:
subtotal = total / (taxPct + 1)
}
func calcTipWithTipPct(tipPct: Double) -> Double {
return subtotal * tipPct
}
func printPossibleTips() {
let possibleTipsInferred = [0.15, 0.18, 0.20]
let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]
for possibleTip in possibleTipsInferred {
println("\(possibleTip * 100)%: \(calcTipWithTipPct(possibleTip))")
}
}
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)
// 3
retval[intPct] = calcTipWithTipPct(possibleTip)
}
return retval
}
}
let tipCalc = TipCalculator(total: 100.00, taxPct: 10.00)
tipCalc.printPossibleTips()
tipCalc.returnPossibleTips()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment