Answer to Question #331012 in Python for Eren

Question #331012

Write a program that will perform the following:


A. Display Employee List

(Retrieve records from empList.txt, store it in a dictionary, display the employee list arranged by keys)


Note: Refer to example code in dictionary module (bbl) for sorting


B. Add Employee

(

Ask user to input the following data:

- unique employee number

- lastname

- firstname

- department

- rate per day

Write the employee record to the empList.txt

)


C. Add Payroll Record

(Ask user to input the following data:

-employee number

-month

-no of days worked

write the payroll record into the empMR.txt

)


D. Generate pay slip


Ask for employee number and month of the payslip to be generated

Create a payslip like statement written in a text file.


=============================================================

Payslip for the Month of _________________

Employee No.: ______________       Employee Name: _____________

Department: ________________

Rate per Day:_______________       No. of Days Worked: ________

Gross Pay: _________________



1
Expert's answer
2022-04-19T15:28:02-0400
import os

PATH_TO_EMPLOYEER_LIST = ".\\empList.txt"
if os.path.exists(PATH_TO_EMPLOYEER_LIST):
    pass
else:
    emp_lst = open(PATH_TO_EMPLOYEER_LIST, "w")
    emp_lst.close()

PATH_TO_PAY_SLIP = ".\\empMR.txt"
if os.path.exists(PATH_TO_PAY_SLIP):
    pass
else:
    pay_slip = open(PATH_TO_PAY_SLIP, "w")
    pay_slip.close()

INPUT_TEXT = """\
Select the option:
    [A] Display Employee List
    [B] Add Employee
    [C] Add Payroll Record
    [D] Generate pay slip
"""

EMPLOYEER_TEXT = """\
{}:
    Lastname:       {}
    Firstname:      {}
    Departament:    {}
    Rate per day:   {}
"""

PAYSLIP = """\
==================================================
Payslip for the Month of {}

Employee No.: {} \t\t Employee Name: {} {}

Department: {}

Rate per Day: {} \t\t No. of Days Worked: {}

Gross Pay: {}
==================================================
"""


class Employeer():
    unique_employee_number = None
    lastname = None
    firstname = None
    department = None
    rate_per_day = None

    def __str__(self) -> str:
        return EMPLOYEER_TEXT.format(
            self.unique_employee_number, self.lastname,
            self.firstname, self.department, self.rate_per_day
        )


class PayRoll():
    unique_employee_number = None
    month = None
    no_of_days_worked = None


def init_employers():
    employeers = []
    with open(PATH_TO_EMPLOYEER_LIST, "r") as emp_list:
        for line in emp_list.readlines():
            employeer = Employeer()

            employeer.unique_employee_number,\
                employeer.lastname, \
                employeer.firstname, \
                employeer.department, \
                employeer.rate_per_day = line.replace("\n", "").split(",")

            employeers.append(employeer)
    return employeers


def init_payrolls():
    payrolls = []

    with open(PATH_TO_PAY_SLIP, "r") as emp_pay_list:
        for line in emp_pay_list.readlines():
            payroll = PayRoll()

            payroll.unique_employee_number,\
                payroll.month, \
                payroll.no_of_days_worked = line.replace("\n", "").split(",")

            payrolls.append(payroll)

    return payrolls


def display_employee_list(employeers):
    for emp in employeers:
        print(emp)


def add_employee():
    employeer = {
        "unique_employee_number": None,
        "lastname": None,
        "firstname": None,
        "department": None,
        "rate_per_day": None
    }

    for key in employeer:
        employeer[key] = input(f"Please input {key}: ")

    with open(PATH_TO_EMPLOYEER_LIST, "a") as emp_list:
        emp_list.write(",".join(employeer.values()) + "\n")


def add_payroll_record():
    payroll_record = {
        "unique_employee_number": None,
        "month": None,
        "no_of_days_worked": None,
    }

    for key in payroll_record:
        payroll_record[key] = input(f"Please input {key}: ")

    with open(PATH_TO_PAY_SLIP, "a") as empMR:
        empMR.write(",".join(payroll_record.values()) + "\n")


def generate_pay_slip(employeers, payrolls):
    if len(employeers) == 0:
        print("No data on employees!")
        return
    if len(payrolls) == 0:
        print("No data on payrolls!")
        return

    unique_number = input("Please input unique employee number: ")
    month = input("Please input month: ")

    for employeer in employeers:
        if employeer.unique_employee_number == unique_number:
            break

    for payroll in payrolls:
        if payroll.unique_employee_number == unique_number:
            if payroll.month == month:
                break

    with open(f".\\{unique_number}_{month}.txt", "w") as payslip:
        payslip.write(
            PAYSLIP.format(
                month,
                unique_number, employeer.firstname, employeer.lastname,
                employeer.department,
                employeer.rate_per_day, payroll.no_of_days_worked,
                int(employeer.rate_per_day) * int(payroll.no_of_days_worked))
        )


def main():
    employeers = init_employers()
    payrolls = init_payrolls()

    option = input(INPUT_TEXT)
    if option == "A":
        display_employee_list(employeers)
    if option == "B":
        add_employee()
    if option == "C":
        add_payroll_record()
    if option == "D":
        generate_pay_slip(employeers, payrolls)


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS