Python 3 - Please complete from Phase 1 to 3. Please use pickle on Phase 2. PLEA
ID: 3846802 • Letter: P
Question
Python 3 - Please complete from Phase 1 to 3. Please use pickle on Phase 2. PLEASE DO NOT ANSWER IF YOU ARE JUST GOING TO COPY PASTE FROM PREVIOUS ANSWERS OR FROM OTHER WEBSITE. IF YOU CANT DO IT BY YOUR OWN DO NOT ANSWER!!!!!!!!!!!!!!!!!
Please answer with PHASE 1 and then PHASE 2 and lastly PHASE 3
Employee Management System
Each subsequent phase will depend on previous assignment result, so you should make an effort to complete each phase on time and ensure that the program is functional before proceeding with the next phase.
Phase 1
Write an employee management system in Python that allows users to add, search, and delete employees. It should store and display five data fields about each employee, which are:
1.Name
2.Employee ID
3.Department
4.Title
5.Salary
The program should display a menu with the following options:
1.Add an Employee
If an employee with the same name already exists, then do not allow adding a new one. Having two employees with the same name is not allowed. While adding an employee you should allow the user to either cancel the operation or re-enter the name again.
2.Find an Employee (By Name)
Implement find an employee by name function. Name search should be case insensitive.
3.Find an Employee (By EID)
Not functional in phase 1. This function will only display a message that this option is functional yet and will be implemented soon.
4.Delete an Employee
Implement delete an employee by name function.
5.Display Statistics
Not functional in phase 1. This function will only display a message that this option is functional yet and will be implemented soon.
6.Display All Employees
Display all employees and their relevant information on the console.
7.Exit
Exit the program.
The memory data structure that holds the information is a dictionary with employee names as key and the rest of the fields as a value. The data fields about each employee should also be organized into a dictionary using key/value pairs (see whiteboard diagram from the classroom discussion).
Your program will go through the following steps:
a)Initialize an empty dictionary in the memory,
b)Create a loop, display the menu, and interact with the user (each menu option will call the appropriate function corresponding that menu option,
c)Continue displaying the menu (b) until the user selects “Exit” option,
d)Exit
Phase 2
1.Implement data persistence:
a.The program should load the data from a specific file (‘employee.dat’) at startup and should save the data to the same file before shutdown.
b.At startup, if the file does not exist, then the program should initialize an empty dictionary in the memory and should not create a data file. If the file is read into memory, it should be immediately closed.
c.On exit, the dictionary, empty or not, should be written to the data file. If the file already exists, it should overwrite it, thus saving the latest state of the dictionary. The file should be immediately closed.
d.For reading and writing a binary file, you should use pickle module described in chapter 9.
e.Your program should have a function called load() that takes a string parameter with the filename and returns a dictionary unpickled from that file. If the file does not exist, then it should return an empty dictionary. The file should be closed before returning from this function.
f.Your program should have a function called save() that takes two parameters, a dictionary and a file. It should write the dictionary into this file using pickle module and close the file.
2.Implement find an employee by EID (option 3)
3.Implement Display Statistics option (option 5)
a.Statistics will include the number of employees in each department, as well as the total number of employees in the company.
Phase 3
1.Implement employee class and employee objects:
a.You should create a class called Employee. The main dictionary now will contain employee objects as values.
b.The file from Phase 2 will not work with this version, so you should delete the file and start from a new data file.
2.Add more information to Display Statistics option (option 5)
a.Statistics for each department should include the maximum, minimum, and average salary for that department. The same should be calculated and displayed for the entire company.
====================Please answer with PHASE 1 and then PHASE 2 and lastly PHASE 3====================================
====================DEFINE WHICH IS PHASE 1, PHASE 2, PHASE 3 ====================================================
THANK YOU
Explanation / Answer
def add_emp(
name, id, dep,
title, salary, emp_arr ):
"""Adds an employee to the array."""
try:
emp_arr[name]
return None
except KeyError:
employee = {
'employee_id' : id,
'department' : dep,
'title' : title,
'salary' : salary
}
emp_arr[name] = employee
return True
def find_by_name(name, emp_arr):
"""Finds an employee by name."""
for key in emp_arr:
if key == name:
return emp_arr[key]
else:
# print("name {} not same as key {}".format(name, key))
pass
return None
def find_by_id(id, emp_arr):
"""Finds an employee by id."""
for key in emp_arr:
if emp_arr[key]['employee_id'] == id:
return emp_arr[key]
else:
# print("id {} not same as key {}".format(id, emp_arr[key]['employee_id']))
pass
return None
def delete_by_name(name, emp_arr):
"""Finds an employee by name and deletes the entry."""
for key in emp_arr:
if key == name:
del emp_arr[name]
return True
else:
# print("name {} not same as key {}".format(name, key))
pass
return False
def display_stats():
pass
def display_all(emp_arr):
print(emp_arr)
def display_menu():
"""Displays our menu."""
print("1. Add an Employee")
print("2. Find an Employee by name")
print("3. Find an Employee by id")
print("4. Delete Employee")
print("5. Display stats")
print("6. Display all Employees")
print("7. Exit")
return input("Enter your choice: ")
# MAIN
# ====
employees = {}
while True:
res = display_menu()
if res == "7":
break
elif res == "6":
display_all(employees)
elif res == "5":
display_stats()
elif res == "4":
delete_by_name(
input(" Enter employee name to delete: "), employees )
elif res == "3":
print(find_by_id(
input(" Enter id of employee to find: "), employees ))
elif res == "2":
print(find_by_name(
input(" Enter name of employee to find: "), employees ))
elif res == "1":
add_emp(
input(" Enter employee name: "),
input("Enter employee id: "),
input("Enter employee department: "),
input("Enter employee title: "),
input("Enter employee salary: "),
employees )
print("Added an employee! ")
else:
print(" Please select one of the 7 options. ")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.