Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.swift
- // payroll
- //
- // Created by Ian Huang on 2/6/18.
- // Copyright © 2018 Ian Huang. All rights reserved.
- //
- import Foundation
- enum eType {
- case Employee,Salaried,Hourly,Manager,Executive
- }
- class Company {
- var people: [Employee]
- var executives: Int
- init () {
- self.executives = 0
- people = [Employee]()
- }
- var annual:Int {
- var total = 0
- for person in people {
- if person.type == .Executive {
- continue;
- }
- print(person.type)
- total+=person.paycheck()*52
- }
- return total
- }
- func hire(_ new:Employee) {
- people.append(new)
- }
- }
- class Employee: CustomStringConvertible {
- static var nextid = 0
- var id: Int
- var first: String
- var last: String
- var type: eType
- var years: Int
- init(_ first: String, _ last: String) {
- id = Employee.nextId()
- self.first = first
- self.last = last
- type = .Employee
- years = 0
- }
- var description: String {
- return "Hello, my name is \(first) \(last) #\(id) and I have been working for \(years) years"
- }
- static func nextId () -> Int {
- nextid+=1
- return nextid
- }
- func paycheck() -> Int {
- return 0
- }
- func raise(_ amount:Int) {
- }
- }
- class Salaried: Employee {
- var annual:Int
- init(_ first:String, _ last: String, _ annual: Int) {
- self.annual = annual
- super.init(first,last)
- type = .Salaried
- }
- override var description: String {
- return super.description+" and I get paid $\(annual) per year"
- }
- override func paycheck() -> Int {
- return annual/52
- }
- override func raise(_ amount:Int) {
- annual+=amount
- }
- }
- class Hourly: Employee {
- var hourly:Int
- var hours:Int
- var hhours:Int
- init(_ first:String, _ last: String, _ hourly: Int) {
- self.hourly = hourly
- self.hours = 0
- self.hhours = 0
- super.init(first,last)
- type = .Hourly
- }
- override var description: String {
- return super.description+" and I get paid $\(hourly) per hour"
- }
- override func paycheck() -> Int {
- let base = min(hours*hourly,40)
- let over = Double(max(0,hours-40)*hourly)*1.5
- let over2 = Double(max(0,hours-80)*hourly)*2.0
- let overtime = over+over2 // The Swift compiler is too stupid to interpret long chains of '+' signs
- let holiday = hhours*hourly*2;
- return Int(base)+Int(overtime)+holiday
- }
- func workHours(_ hrs:Int) {
- hours+=hrs
- }
- func workDoubleTimeHours(_ hrs:Int) {
- hhours+=hrs
- }
- override func raise(_ amount: Int) {
- hourly+=amount
- }
- }
- class Manager: Salaried {
- var rating: Int
- init(_ first:String,_ last:String,_ annual:Int, _ rating:Int) {
- self.rating = rating
- super.init(first,last,annual)
- type = .Salaried
- }
- override func paycheck() -> Int {
- return Int(Double(super.paycheck())+(Double(rating)*0.1*Double(annual)))
- }
- func setManagerRating(_ new:Int) {
- rating = new
- }
- }
- class Executive: Manager {
- static var execs = 0
- static var annualProfit = 10000000
- override init (_ first:String,_ last:String,_ annual:Int, _ rating:Int) {
- Executive.execs+=1
- super.init(first,last,annual,rating)
- type = .Executive
- }
- override func paycheck() -> Int {
- return super.paycheck()+Int(
- 0.1*Double(Executive.annualProfit)/Double(Executive.execs))
- }
- override var description: String {
- return "My name is \(first) \(last) and I get paid \(paycheck()*52) annually"
- }
- static func setAnnualProfit(_ profit:Int) {
- annualProfit = profit
- }
- }
- var employees = [Employee] ()
- var s = Salaried("Fred","Jones",35000)
- s.raise(10000)
- employees.append(s)
- var h = Hourly("Phil","Smith",22)
- h.raise(3)
- h.workHours(10)
- h.workHours(40)
- h.workHours(35)
- h.workDoubleTimeHours(20)
- employees.append(h)
- var m = Manager("Henry","Talbot",65000,3)
- m.raise(10000)
- m.setManagerRating(4)
- employees.append(m)
- var x = Executive("Sue","Strong",125000,3)
- employees.append(x)
- var x2 = Executive("Ted","Bran",120000,4)
- employees.append(x2)
- var x3 = Executive("Ann","White",175000,2)
- employees.append(x3)
- Executive.setAnnualProfit(2000000)
- for e in employees{print(e,"\n")}
- print("\n\n\nWeekly payroll check run:\n")
- for e in employees {
- print("\(e.first) \(e.last) $\(e.paycheck()) per week")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement