Python Forum
PayrollSystem with file handling
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PayrollSystem with file handling
#2
You do not save enough information in the csv file. There is no way to differentiate between commissioned and hourly employees. I would save all five employee fields PLUS a field identifying the employee wage type ("salary", "commission", "hourly"). This will make it easy to determine what class to use when loading employee data from the file.

All that code in main() should be part of PayrollSystem. I would expect main() to look more like this:
def main():
    payroll = PayrollSystem()
    payroll.restore()  # Restore from file at startup.
    while True:
        print("Options\n(1) Add employees\n(2) Print payroll\n(3) Quit")
        match input("Enter: "):
            case "1":
                payroll.add_employee()  # Add 1 employee
                payroll.save()  # Save updated employee list.
            case "2":
                payroll.print()
            case "3":
                break
case _:
print("Invalid selection.")
PayrollSystem should have the brains and the most main needs to do is provide a way to call the methods provided by the PayrollSystem.

Your Employee classes are backward. Your subclasses do most of the work, and the base class does almost nothing. Your base class should do most of the work, and the subclasses do only the parts that are specific to that class. The more work you can push up from subclasses to parent classes, the better the design (usually). As an example, the following method could be added to the Employee class and used to calculate pay for any employee.
    def pay(self):
        """Calculate employee pay."""
        return self.salary + self.commission + self.rate * self.hours
Salaried employees will have 0 for commission, rate and hours. Hourly employees will have 0 for salary and commission.
nikadry1 likes this post
Reply


Messages In This Thread
PayrollSystem with file handling - by nikadry1 - Apr-29-2024, 01:16 PM
RE: PayrollSystem with file handling - by deanhystad - Apr-30-2024, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Handling IO Error / Reading from file Expel 10 5,151 Jul-18-2019, 01:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020