Advertisement
Guest User

project

a guest
Feb 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.65 KB | None | 0 0
  1. //
  2. //  main.swift
  3. //  payroll
  4. //
  5. //  Created by Ian Huang on 2/6/18.
  6. //  Copyright © 2018 Ian Huang. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. enum eType {
  12.     case Employee,Salaried,Hourly,Manager,Executive
  13. }
  14.  
  15. class Company {
  16.     var people: [Employee]
  17.     var executives: Int
  18.     init () {
  19.         self.executives = 0
  20.         people = [Employee]()
  21.     }
  22.     var annual:Int {
  23.         var total = 0
  24.         for person in people {
  25.             if person.type == .Executive {
  26.                 continue;
  27.             }
  28.             print(person.type)
  29.             total+=person.paycheck()*52
  30.         }
  31.         return total
  32.     }
  33.    
  34.     func hire(_ new:Employee) {
  35.         people.append(new)
  36.     }
  37. }
  38.  
  39. class Employee: CustomStringConvertible {
  40.     static var nextid = 0
  41.     var id: Int
  42.     var first: String
  43.     var last: String
  44.     var type: eType
  45.     var years: Int
  46.     init(_ first: String, _ last: String) {
  47.         id = Employee.nextId()
  48.         self.first = first
  49.         self.last = last
  50.         type = .Employee
  51.         years = 0
  52.     }
  53.     var description: String {
  54.         return "Hello, my name is \(first) \(last) #\(id) and I have been working for \(years) years"
  55.     }
  56.     static func nextId () -> Int {
  57.         nextid+=1
  58.         return nextid
  59.     }
  60.     func paycheck() -> Int {
  61.         return 0
  62.     }
  63.     func raise(_ amount:Int) {
  64.     }
  65. }
  66.  
  67. class Salaried: Employee {
  68.     var annual:Int
  69.     init(_ first:String, _ last: String, _ annual: Int) {
  70.         self.annual = annual
  71.         super.init(first,last)
  72.         type = .Salaried
  73.        
  74.     }
  75.     override var description: String {
  76.         return super.description+" and I get paid $\(annual) per year"
  77.     }
  78.     override func paycheck() -> Int {
  79.         return annual/52
  80.     }
  81.     override func raise(_ amount:Int) {
  82.         annual+=amount
  83.     }
  84. }
  85. class Hourly: Employee {
  86.     var hourly:Int
  87.     var hours:Int
  88.     var hhours:Int
  89.     init(_ first:String, _ last: String, _ hourly: Int) {
  90.         self.hourly = hourly
  91.         self.hours = 0
  92.         self.hhours = 0
  93.         super.init(first,last)
  94.         type = .Hourly
  95.        
  96.     }
  97.     override var description: String {
  98.         return super.description+" and I get paid $\(hourly) per hour"
  99.     }
  100.     override func paycheck() -> Int {
  101.         let base = min(hours*hourly,40)
  102.         let over = Double(max(0,hours-40)*hourly)*1.5
  103.         let over2 = Double(max(0,hours-80)*hourly)*2.0
  104.         let overtime = over+over2 // The Swift compiler is too stupid to interpret long chains of '+' signs
  105.         let holiday = hhours*hourly*2;
  106.         return Int(base)+Int(overtime)+holiday
  107.     }
  108.     func workHours(_ hrs:Int) {
  109.         hours+=hrs
  110.     }
  111.     func workDoubleTimeHours(_ hrs:Int) {
  112.         hhours+=hrs
  113.     }
  114.  
  115.     override func raise(_ amount: Int) {
  116.         hourly+=amount
  117.     }
  118. }
  119. class Manager: Salaried {
  120.     var rating: Int
  121.     init(_ first:String,_ last:String,_ annual:Int, _ rating:Int) {
  122.         self.rating = rating
  123.         super.init(first,last,annual)
  124.         type = .Salaried
  125.     }
  126.     override func paycheck() -> Int {
  127.         return Int(Double(super.paycheck())+(Double(rating)*0.1*Double(annual)))
  128.     }
  129.     func setManagerRating(_ new:Int) {
  130.         rating = new
  131.     }
  132. }
  133. class Executive: Manager {
  134.     static var execs = 0
  135.     static var annualProfit = 10000000
  136.     override init (_ first:String,_ last:String,_ annual:Int, _ rating:Int) {
  137.         Executive.execs+=1
  138.         super.init(first,last,annual,rating)
  139.         type = .Executive
  140.     }
  141.     override func paycheck() -> Int {
  142.         return super.paycheck()+Int(
  143.             0.1*Double(Executive.annualProfit)/Double(Executive.execs))
  144.     }
  145.     override var description: String {
  146.         return "My name is \(first) \(last) and I get paid \(paycheck()*52) annually"
  147.     }
  148.     static func setAnnualProfit(_ profit:Int) {
  149.         annualProfit = profit
  150.     }
  151. }
  152.  
  153. var employees = [Employee] ()
  154.  
  155. var s = Salaried("Fred","Jones",35000)
  156. s.raise(10000)
  157. employees.append(s)
  158.  
  159. var h = Hourly("Phil","Smith",22)
  160. h.raise(3)
  161. h.workHours(10)
  162. h.workHours(40)
  163. h.workHours(35)
  164. h.workDoubleTimeHours(20)
  165. employees.append(h)
  166.  
  167. var m = Manager("Henry","Talbot",65000,3)
  168. m.raise(10000)
  169. m.setManagerRating(4)
  170. employees.append(m)
  171.  
  172. var x = Executive("Sue","Strong",125000,3)
  173. employees.append(x)
  174. var x2 = Executive("Ted","Bran",120000,4)
  175. employees.append(x2)
  176. var x3 = Executive("Ann","White",175000,2)
  177. employees.append(x3)
  178.  
  179. Executive.setAnnualProfit(2000000)
  180.  
  181. for e in employees{print(e,"\n")}
  182. print("\n\n\nWeekly payroll check run:\n")
  183. for e in employees {
  184.     print("\(e.first) \(e.last) $\(e.paycheck()) per week")
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement