In: Computer Science
For this project, you are to modify the Employee class posted on Blackboard by adding a bonus field. The bonus field should be equal to zero when an Employee object is created. You need to add a setter and a getter method to the new bonus field.
The Dirany’s company saves all employee information in the dirany.txt file (attached). The file saves each employee’s information in a new line and all attributes are separated by tabs. For example, the first employee’s information on the first line in the dirany.txt file is as follows:
John Smith 90000
John is the first name of the employee, Smith is the employee’s last name and 90000 is the employee’s annual salary.
Your program should read the dirany.txt file and create an Employee object for each employee in the file, then add the employees to a queue data structure in the same order you read them from the file. The Dirany’s company saves the employees records according to their longevity in the company.
You need to help the Dirany’s company, to calculate the bonus for each employee according to their longevity rule so the bonus of the first employee will be 20% of his/her salary and for each employee afterwards the percent of the bonus will be 1%less so the second employee will get 19% of his/her salary, the third employee will get 18% of his/her salary and so forth.
Your program should access the Employees’ objects in the queue, calculate the bonus field and set it for each Employee object and display the object status: first name, last name, pay and bonus for each employee.
Also your program should calculate and display the number of the employees in the company and the total bonus that the company will pay for all its employees.
Make sure you break up your code into a set of well-defined functions. Each function should include a comment block that briefly describes it, its parameters and any return values.
John Smith 90000 Mathew Marshal 89000 Nicole Lopaz 88200 Adam Benjamin 83400 Julia Hart 82000 Mary Loveland 79000 Varon Bansel 73400 Ali Hassan 63000 Joseph Murty 52000 Ryan Frankel 43400 Julianne Johnson 38600 Noah Expo 22000 Deven Williams 19300 Richard Peal 15200 Lee Wang 14300
in python
Program
# to use queue import module queue
import queue
# employee class defintion
class Employee:
def __init__(self,firstName,lastName,annualSalary):
self.firstName=firstName
self.lastName=lastName
self.annualSalary=annualSalary
self.bonus=0
# set bonus function
def setBonus(self,bonus):
self.bonus=bonus
# get bonus function
def getBonus(self):
return self.bonus
def fillQueuefromFile():
# fillQueuefromFile function
# takes not input
# returns queue of employees loaded from dirany file
employeeQueue=queue.Queue()
# initialize queue and open dirany.txt file
file=open("dirany.txt")
# read line by line until end of file
line=file.readline()
while(line):
# split the line according to tabs
tokens=line.split("\t")
# add to queue an employee object
employeeQueue.put(Employee(tokens[0],tokens[1],tokens[2][:-1]))
line=file.readline()
return employeeQueue
def calculateBonus(employeeQ):
# calculate bonus function
# takes input an employee queue
# returns total bonus paid and the number of employees in queue
bonus=20
# initial bonus
totalBonusAmt=0
size=0
# headers
print("%10s"%("FirstName"),"%10s"%("\tLastName"),"%10s"%("\t\tPay"),"%10s"%("\tBonus"))
# loop until the queue is empty
while not employeeQ.empty():
emp=employeeQ.get()
# increment the count for each employee deleted from queue
size=size+1
# bonus calculation
bonusAmt=float(emp.annualSalary)+(float(emp.annualSalary)*(bonus/100))
# add the bonus to total bonus amount
totalBonusAmt=totalBonusAmt+bonusAmt
emp.setBonus(bonusAmt)
# print the record with formatting
print("%10s"%(emp.firstName)+"\t"
+"%10s"%(emp.lastName)+"\t"
+"%10s"%(str(emp.annualSalary))+"\t"
+"%10s"%(str(emp.bonus)))
# bonus can't be negative
if bonus>0:
bonus=bonus-1
return totalBonusAmt,size
# main function calls all other functions
def main():
employeeQ=fillQueuefromFile()
totalBonus,size=calculateBonus(employeeQ)
# print number of employees and total bonus paid
print("Number of Employees are "+str(size))
print("Total Bonus paid by company is "+str(totalBonus))
if __name__ == '__main__':
main()
refer to below screenshots for indentation and output
Output