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.
Okay so here is my solution:-
class Employee:
'ID, number, department, job title'
def __init__(self,ID,number,department,job_title):
self.ID = ID
self.number = number
self.department = department
self.job_title = job_title
# Mutators
def set_ID(self,ID):
self.ID = ID
def set_number(self,number):
self.number = number
def set_department(self,department):
self.department = department
def job_title(self,job_title):
self.job_title = job_title
#Accessor Methods
def get_ID(self):
return self.ID
def get_number(self):
return self.number
def get_department(self):
return self.department
def get_job_title(self):
return self.job_title
def get_data(self):
print self.ID, self.number,self.department,self.job_title
I saved the above as Employee.py in a folder. Then i started a new file and saved that as Employee Management System.py Here is the code in that file
import Employee
import pickle
filename = 'contacts.dat'
input_file = open(filename,'rb')
unpickle_input_file = pickle.load(input_file)
def test_system():
user = input('Press 1 to look up employee,\nPress 2 to add employee'
'\n3Press 3 to change an existing employee name, department and job title'
'\n4 Delete an employee from the dictionary'
'\n5 Quit the program'
'\nMake your choice ')
if user == 2:
ID = raw_input('Enter the name ')
number = input('Enter the number')
deparment = raw_input('Enter the department ')
job_title = raw_input('Enter the job title ')
entry = module from Employee??.class name(id,number,department,job_title)??
empty_dictionary = {}
empty_dictionary[number] = entry
input_file.close()
My first problem is that i am trying to use the created attribue in Employee.py . Specifically the init and add entry to it. I know the above code is not in the most logical forum but i am trying to first see if i can add data then pickle the file first. Everything else would be easy to figure out later if i can under how to do those two things.
It kind of reminds me of
import math
x = math.pi(3.14)
x = module.function(3.14)
But i can't just seem to made the connection between the two examples. Thank you