Advertisement
Guest User

lab11 2nd task

a guest
Oct 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import string
  2.  
  3. class Employee():
  4.     def __init__(self,name,rate,hours):
  5.         self.name = name
  6.         self.rate = rate
  7.         self.hours = hours
  8.  
  9. class SalariedEmployee(Employee):
  10.  
  11.     def calculatePay(self):
  12.         compensation = self.rate/40*self.hours
  13.         return compensation
  14.                
  15. class HourlyEmployee(Employee):
  16.     def calculatePay(self):
  17.         compensation = self.rate*self.hours
  18.         return compensation
  19.  
  20. def main():
  21.  
  22.     total = {}
  23.     salaried = 0
  24.     hours = 0
  25.  
  26.     while True:
  27.         while True:
  28.             try:
  29.                
  30.                 name = input("Enter employees's name: ")
  31.                 name = name.lower().split()
  32.                 name = [item.capitalize() for item in name]
  33.                 name = ' '.join(name)
  34.  
  35.                 classification = input("Enter employee's classification(Salaried or Hourly): ").capitalize()
  36.  
  37.                 for letter in name:
  38.                     if letter not in string.ascii_letters and letter != ' ':
  39.                         raise Exception
  40.                 for letter in classification:
  41.                     if letter not in string.ascii_letters:
  42.                         raise Exception
  43.             except:
  44.                 print('Only letters are allowed, please try again.')
  45.             else:
  46.                 if classification == 'Salaried':
  47.                     while True:
  48.                         try:
  49.                             time = float(input('Enter hours worked: '))
  50.                             pay = float(input('Enter weekly salary: '))
  51.                         except:
  52.                             print('Only numbers are allowed, please try again.')
  53.                         else:
  54.                             break
  55.            
  56.                     total[name] = SalariedEmployee.calculatePay(Employee(name,pay,time))
  57.                     salaried+=1
  58.                     hours += time
  59.                     break
  60.  
  61.                 elif classification == 'Hourly':
  62.                     while True:
  63.                         try:
  64.                             time = float(input('Enter hours worked: '))
  65.                             pay = float(input('Enter hourly wage: '))
  66.                         except:
  67.                             print('Only numbers are allowed, please try again.')
  68.                         else:
  69.                             break
  70.            
  71.                     total[name] = HourlyEmployee.calculatePay(Employee(name,pay,time))
  72.                     hours += time
  73.                     break
  74.  
  75.                 else:
  76.                     print('Invalid input, please try again')
  77.  
  78.         while True:
  79.             ask = input('Do you want to continue? Y/N: ')
  80.             if ask.upper() == 'N':
  81.                 for key in total:
  82.                     print(f'{key}: ${total[key]}')
  83.                 print(f'Number of employees: {len(total)}')
  84.                 print(f'Number of salaried employees: {salaried}')
  85.                 print(f'Total payroll: {sum(total.values())}')
  86.                 print(f'Average number of hours worked per employee: {hours/len(total)}')
  87.                 break
  88.        
  89.             elif ask.upper() != 'Y':
  90.                 print('Invalid input, please try again')
  91.  
  92.             else:
  93.                 break
  94.            
  95.         if ask.upper() =='N':
  96.             break
  97.  
  98.  
  99.  
  100. if __name__ == "__main__":
  101.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement