1

I'm attempting to write a program with subclasses. I've been debugging for hours and can't find a solution.

I'm need to write an Employee class that keeps data attributes for Employee name and Employee number. Next, I need to write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for Shift number, Hourly pay rate, and Hours worked this week. The workday is divided into two shifts: day and night. The shift attribute will hold an integer value representing the shift that the employee works. The day shift is 1 and the night shift is 2. Write the appropriate accessor and mutator methods for each class.

Next, I need to create an object of the ProductionWorker class and prompts the user to enter data for each of the object’s data attributes. I use a data validation class to make sure the user enters valid values for pay rate and hours. I Store the data in the object and then use the object’s accessor methods to retrieve it and display it on the screen. I need to also display this worker’s total pay for the week. Total pay = hourly rate * hours worked. I can't do any calculations on the main file.

Next, I need to write a ShiftSupervisor class that is a separate subclass of the Employee class. Shift supervisors earn a salary and a yearly bonus if their shift meets production goals. The ShiftSupervisor class should keep a data attribute for the annual salary and a data attribute for the annual production bonus that a shift supervisor has earned.
I need to write a second program that will create an object of the ShiftSupervisor class and prompt the user to enter data for each of the object’s data attributes. I need to display this supervisor’s total annual pay.

When I run the main file I get this error:

        Traceback (most recent call last):
  File "/Users/Jeremy/Documents/Python Projects/Mosier_Jeremy_HW8/HW8MAIN.py", line 4, in <module>
    employeeObject = EmployeeFile.ProductionWorker ()
TypeError: __init__() missing 5 required positional arguments: 'name', 'number', 'shift', 'rate', and 'hours'

Here is my ValidationFile:

    class ValidationClass:

    def checkFloat (self, inputString):

        try:
            result = float (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

    def checkInteger (self, inputString):

        try:
            result = int (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

Here is my EmployeeFile:

    class Employee ():
    def __init__(self, name, number):
        self.__Name = name
        self.__Number = number

    def set_Name (self, name):
        self.__Name = value

    def set_Number(self, number):
        self.__Number = value

    def get_Name (self):
        return self.__Name

    def get_Number (self):
        return self.__Number

class ProductionWorker (Employee):
    def __init__(self, name, number, shift, rate, hours):
        self.__Shift = shift
        self.__Rate = rate
        self.__Hours = hours
        Employee.__init__(self, name, number)

    def set_Shift (self, shift):
        self.__Shift = shift

    def set_PayRate (self, rate):
        self.__PayRate = rate

    def set_Hours (self, hours):
        self.__Hours = hours

    def get_Shift (self):
       if self.__Shift == 1:
            s = 'Day shift'
       elif self.__Shift == 2:
            s = 'Night shift'
       return s

    def get_PayRate (self):
        return self.__PayRate

    def get_Hours (self):
        return self.__Hours

    def get_Pay (self):
        return self.__Pay

    def calcPay (self):
        self.__Pay = (self.__PayRate) * (self.__Hours)

class ShiftSupervisor (Employee):
    def __init__ (self, name, number, salary, bonus):
        self.__Salary = salary
        self.__Bonus = bonus
        Employee.__init__ (self, name, number)

    def set_Salary (self, salary):
        return self.__Salary

    def set_Bonus (self, bonus):
        return self.__Bonus

    def get_Salary (self):
        self.__Salary = salary

    def get_Bonus (self):
        self.__Bonus = bonus

    def calcPay (self):
        self.__Pay = (self.__Salary) + (self.__Bonus)

Here is my main file:

    import EmployeeFile
import ValidationFile

employeeObject = EmployeeFile.ProductionWorker ()
validationObject = ValidationFile.ValidationClass ()

employeeName = -1
while employeeName == -1:
    employeeName = input ('Please enter the employee name: ')
    if employeeName == '':
        print ('ERROR: Please enter a valid name.')
        employeeName = -1

employeeNumber = -1
while employeeNumber == -1:
    employeeNumber = input ('Please enter the employee name: ')
    if employeeNumber == '':
        print ('ERROR: Please enter a valid name.')
        employeeNumber = -1

shiftNumber = -1
while shiftNumber == -1:
    shiftNumber = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    if shiftNumber < 1 or shiftNumber > 2:
        print ('ERROR: Shift number must be entered as a 1 or a 2.')

payRate = -1
while payRate == -1:
    payEntry = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    payRate = validationObject.checkFloat (payEntry)
    if payRate == -1:
        print ('ERROR: Pleae enter a valid payRate.')

hours = -1
while hours == -1:
    hoursEntry = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    payRate = validationObject.checkFloat (hoursEntry)
    if payRate == -1:
        print ('ERROR: Pleae enter a valid payRate.')

#populate inputs
employeeObject.set_Name (employeeName)
employeeObject.set_Number (employeeNumber)
employeeObject.set_Shift (shiftNumber)
employeeObject.set_PayRate (payRate)
employeeObject.set_Hours (hours)

print ('Employee name: ', employeeName)
print ('Employee number: ', employeeNumber)
print ('Shift: ', shiftNumber)
print ('Pay rate: ', payRate)
print ('Hours worked: ', hours)
  • one solution might be kwargs like `def __init__(self, name=None, number=None, shift=None, rate=None, hours=None):` other wise you have to call the constructor like: `EmployeeFile.ProductionWorker ('name', 123, 'shift here', 7.25, 30)` ... maybe hold the values in a temporary place and then call the constructor? – jmunsch Apr 18 '17 at 23:26

2 Answers2

0

Your ProductionWorker class requires you to put in certain parameters, which is why it is complaining:

class ProductionWorker (Employee):
    def __init__(self, name, number, shift, rate, hours):
        self.__Shift = shift
        self.__Rate = rate
        self.__Hours = hours
        Employee.__init__(self, name, number)

You need to either make those optional or not create the object until you have all that information. I'd recommend waiting until you have all the information, then replacing your current populate inputs with:

#populate inputs
employeeObject = EmployeeFile.ProductionWorker(employeeName, employeeNumber, shiftNumber, payRate, hours)

Additionally, you should look into super() as the proper method for calling the parent classes init.

Community
  • 1
  • 1
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
0

You have to call your constructors in your program because you have declared several variables in the init methods:

in main program:

employeeObject = EmployeeFile.ProductionWorker("name", 12, 1, 10.0, 3)#random variable names used
Ajax1234
  • 69,937
  • 8
  • 61
  • 102