Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how could I add to this code to include (object oriented) inheritance with the b

ID: 3850278 • Letter: H

Question

how could I add to this code to include (object oriented) inheritance with the base class of person and subclass of employee while maintaing the ability to read from input file and write to output file?

f = open("C:\Users\user\Desktop\payroll.txt", "r")
outfile = open('C:\Usersuser\Desktop\output.txt','w')
output=[]

for line in f.readlines():
columns=line.split()
id=columns[0]
name=columns[1] + ' ' + columns[2]
wage=float(columns[3])
days=columns[4:]
totalhours=0

for hour in days:
totalhours=totalhours + float(hour)
averageHours=totalhours/len(days)
totalPay=totalhours*wage

result=name+' ID'+id+' worked '+str(totalhours)+' hourly pay $'+str(wage)+' hours: ' + str(round(averageHours,2)) + '/day Total Pay: $' + str(totalPay)

print(result)
output.append(result+' ')

# Iterating over each item in list variable
for line in output:
# Writing each line to output file
outfile.write(line)

# Closing output file
outfile.close()

Explanation / Answer

I ma giveing you an exmple . You can define and use the classes as per your reqirment. You have not given you input file. from your code it is not clear which attribut will be part of person and which attribut will be part of employee class. using the getAttry() and setattry() you can read and wtire the valu of attrybuts.

class person: # define parent class
personName = 'Name'
def __init__(self):
print (persontryName)

def personMethod(self):
print 'Calling parent method'

def setAttr(self, attr):
person.personName = attr

def getAttr(self):
print "Person attribute :", person.personName

class employee(person): # define child class
employID = 'Name'
def __init__(self):
print "Calling child constructor"

def employeeMethod(self):
print 'Calling child method'

c = employee() # instance of child
c.employeeMethod() # child calls its method
c.personMethod() # calls parent's method
c.setAttr('amit') # again call parent's method here I set the value of name as 'amit'
c.getAttr() # again call parent's method here i print the value of name