SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:6 | ISBN:9780132576376 | Edition: 2

Question

Employee Management System
This exercise assumes that you have created the Employee class for Programming Exercise 4.
Create a program that stores Employee objects in a dictionary. Use the employee ID number as the key. The program should present a menu that lets the user perform the following actions:
• Look up an employee in the dictionary
• Add a new employee to the dictionary
• Change an existing employee’s name, department, and job title in the dictionary
• Delete an employee from the dictionary
• Quit the program
When the program ends, it should pickle the dictionary and save it to a file. Each time the program starts, it should try to load the pickled dictionary from the file. If the file does not exist, the program should start with an empty dictionary.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

 

The following program helps to develop the employee management system:

# import statement
import pickle
import os

# Create a class named Employee
class Employee:
    def __init__(self, emp_id, name, department, job_title):
        self.emp_id = emp_id
        self.name = name
        self.department = department
        self.job_title = job_title

# Create a function named load_employees to store the employee data in pickle file
def load_employees(filename):
    if os.path.exists(filename):
        with open(filename, 'rb') as file:
            return pickle.load(file)
    return {}

def save_employees(filename, employees):
    with open(filename, 'wb') as file:
        pickle.dump(employees, file)

# Create a function named lookup_employee().
# This function helps to check the employee details present in the object or not.
def lookup_employee(employees):
    emp_id = input("Enter the Employee ID to look up: ")
    employee = employees.get(emp_id)
    if employee:
        print(f"Employee ID: {employee.emp_id}")
        print(f"Name: {employee.name}")
        print(f"Department: {employee.department}")
        print(f"Job Title: {employee.job_title}")
    else:
        print("Employee not found.")

# Create a function named lookup_employee().
# This function helps to add employee in the list
def add_employee(employees):
    emp_id = input("Enter the new Employee ID: ")
    name = input("Enter the new Employee's Name: ")
    department = input("Enter the new Employee's Department: ")
    job_title = input("Enter the new Employee's Job Title: ")

    new_employee = Employee(emp_id, name, department, job_title)
    employees[emp_id] = new_employee
    print("Employee added successfully.")

# Create a function named change_employee().
# This function helps to edit the employee details
def change_employee(employees):
    emp_id = input("Enter the Employee ID to update: ")
    if emp_id not in employees:
        print("Employee not found.")
        return

    name = input("Enter the updated Name: ")
    department = input("Enter the updated Department: ")
    job_title = input("Enter the updated Job Title: ")

    employee = Employee(emp_id, name, department, job_title)
    employees[emp_id] = employee
    print("Employee details updated successfully.")

# Create a function named delete_employee().
# This function helps to delete the employee details.
def delete_employee(employees):
    emp_id = input("Enter the Employee ID to delete: ")
    if emp_id not in employees:
        print("Employee not found.")
        return

    del employees[emp_id]
    print("Employee deleted successfully.")

# Create a menu
def display_menu():
    print("\nEmployee Management System Menu:")
    print("1. Look up an employee")
    print("2. Add a new employee")
    print("3. Change an existing employee's details")
    print("4. Delete an employee")
    print("5. Quit")

# Create a main function
def main():
    filename = "employees.pickle"
    employees = load_employees(filename)

    while True:
        display_menu()
        choice = input("Enter your choice (1-5): ")

        if choice == '1':
            lookup_employee(employees)
        elif choice == '2':
            add_employee(employees)
        elif choice == '3':
            change_employee(employees)
        elif choice == '4':
            delete_employee(employees)
        elif choice == '5':
            save_employees(filename, employees)
            print("Employee data saved. Exiting...")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

 

Sample Output:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

===================== RESTART: C:/Users/user/Desktop/MWQ.py ====================

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 1
Enter the Employee ID to look up: 2525
Employee not found.

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 2
Enter the new Employee ID: 1001
Enter the new Employee's Name: Alex
Enter the new Employee's Department: Service
Enter the new Employee's Job Title: Lift Operator
Employee added successfully.

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 1
Enter the Employee ID to look up: 1
Employee not found.

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 1
Enter the Employee ID to look up: 1001
Employee ID: 1001
Name: Alex
Department: Service
Job Title: Lift Operator

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 3
Enter the Employee ID to update: 1001
Enter the updated Name: Michel
Enter the updated Department: IT
Enter the updated Job Title: Software
Employee details updated successfully.

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 1
Enter the Employee ID to look up: 1001
Employee ID: 1001
Name: Michel
Department: IT
Job Title: Software

Employee Management System Menu:
1. Look up an employee
2. Add a new employee
3. Change an existing employee's details
4. Delete an employee
5. Quit
Enter your choice (1-5): 5
Employee data saved. Exiting...

 

1 0

Discussions

Post the discussion to improve the above solution.