Last active
October 14, 2015 16:39
-
-
Save RonJeffries/009495abec81b2b1890b to your computer and use it in GitHub Desktop.
A Swift Playground for payroll. I have my reasons ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift playground experimenting with payroll. | |
// (I have my reasons.) | |
println("Hello") | |
import Foundation | |
// This stuff sets rounding to two places after decimal | |
var mode = NSRoundingMode.RoundPlain | |
var handler = NSDecimalNumberHandler( | |
roundingMode: mode, | |
scale: 2, | |
raiseOnExactness: true, | |
raiseOnOverflow: true, | |
raiseOnUnderflow: true, | |
raiseOnDivideByZero: true) | |
NSDecimalNumber.setDefaultBehavior(handler) | |
// rounding function saved for reference | |
// no longer necessary now that I've set default, | |
// might come in handy for other than two digit rounding | |
func round(n: NSDecimalNumber) -> NSDecimalNumber { | |
return n.decimalNumberByRoundingAccordingToBehavior(handler) | |
} | |
// operator extensions for convenient decimal | |
func dec(n: Double) -> NSDecimalNumber { | |
return NSDecimalNumber(double: n) | |
} | |
@infix func +(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber { | |
return left.decimalNumberByAdding(right) | |
} | |
@infix func -(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber { | |
return left.decimalNumberBySubtracting(right) | |
} | |
@infix func /(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber { | |
return left.decimalNumberByDividingBy(right) | |
} | |
@infix func *(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber { | |
return left.decimalNumberByMultiplyingBy(right) | |
} | |
// useful constants | |
let twelve = dec(12.0) | |
let taxRate = dec(0.25) | |
let zero = dec(0) | |
// classes | |
class Person { | |
let name: String | |
let payRate: NSDecimalNumber | |
init (name: String, rate: Double) { | |
self.name = name | |
payRate = dec(rate) | |
} | |
} | |
class PayStub { | |
let person: Person | |
var salary = zero | |
var grossPay = zero | |
var fedTax = zero | |
var deductions = zero | |
var netPay = zero | |
init(person: Person) { | |
self.person = person | |
} | |
func pay() { | |
salary = person.payRate / twelve | |
grossPay = zero | |
grossPay = grossPay + salary | |
fedTax = grossPay * taxRate | |
deductions = zero | |
deductions = deductions + fedTax | |
netPay = grossPay - deductions | |
} | |
} | |
var person = Person(name: "Jones", rate: 50000) | |
var jones = person | |
var smith = Person(name: "Smith", rate: 30000) | |
var stub = PayStub(person: person) | |
stub.pay() | |
stub.person.name | |
stub.salary | |
stub.grossPay | |
stub.fedTax | |
stub.deductions | |
stub.netPay | |
/*var inc = dec(0.01) | |
var tot = zero | |
for i in (0..100) { | |
tot = tot + inc | |
} | |
tot | |
tot == dec(1.0)*/ | |
// how to find the comma, god save me | |
var test = "123, Jones" | |
var j = test.rangeOfString(",") | |
var s = j.startIndex | |
var e = j.endIndex | |
test[s...e] | |
// maybe for our purposes we can just keep data in arrays | |
var jonesrecord = [1, "jones"] | |
var smithrecord = [2, "smith"] | |
// or dictionaries. yeah, that's it, dictionaries | |
var personnel = [1: jones, 2: smith] | |
personnel[2] // returns Person smith | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome stuff. Thanks