Python Forum
Making a payroll class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a payroll class
#21
(Jul-26-2024, 03:47 PM)Pedroski55 Wrote: Except, that function now needs changing too, because the poor girl cannot even use csv or os or pathlib. But that is easy to do, if she needs help with that.

But I know nothing about Python classes. If you can make it tidier, please do so!

Maybe she is allowed to use tabulate to display the results in a nice way! The whole exercise is not remotely real-world!

The teacher just suggested I use a dictionary...seriously!? that was in the chapter HE skipped Angry So, that just showed me he was not going to hold the rule, dont use what we have not covered. In the end...I used os. The program works. I am going to be very happy to get back to my computer graphics courses this next semester...because this class kicked my backside this summer. I am just happy the html classes are with information I already know. *yes, I took this class thinking I could easily get a programing certificate with my degree....I was doing this for 'fun'.
Reply
#22
An employee class would look something like this:
class Employee:
    def __init__(self, name, pay_rate, hours):
        self.name = name
        self.pay_rate = pay_rate
        self.hours = hours

    def overtime(self):
        """Return overtime hours."""
        return max(0, self.hours - 40)

    def pay(self):
        """Return pay for this latest pay period."""
        return self.pay_rate * (self.hours + 0.5 * self.overtime())

    def __str__(self):
        """Return pretty string."""
        return f"{self.name:30}  Pay Rate = {self.pay_rate:6.2f}, Hours Worked = {self.hours:6.2f}, Pay = {self.pay():8.2f}"
Cleaner than a dictionary because you don't need to use keys to access the values, plus it can compute pay, removing that burden from the payroll class.

@Pedroski55, did you read the assignment description in the initial post? There was no mention of supporting multiple companies, though that is easily supported by loading a different payroll file. All that is mentioned is that the payroll class must have payroll data for "many employees", support adding new employees (input data), compute pay, and display pay. There is no mention of how an employee is represented other than the attributes: name, hours worked, pay rate and total pay. I must admit I am guilty of missing some details. For some reason I got it stuck in my head the employee data file would be a json file when the instructions explicitly say it should be some sort of CSV.

Using the payroll class is defined a little better. A main program creates an instance of the payroll class, uses the class to read employee data from a file, and upon exiting the program uses the payroll class to write employee data to a file.
Reply
#23
Just want to say thank you for all the help....

Got 100 Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Making a Class Person ? Kessie1971 3 1,987 Apr-23-2023, 05:20 PM
Last Post: deanhystad
  Beginning Payroll code help EmRandall 6 10,944 Sep-18-2018, 01:21 AM
Last Post: EmRandall

Forum Jump:

User Panel Messages

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