问题
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
回答1:
What you're trying to do is to instantiate an Employee object with the given parameters. To do this, you just call the class name as if it were a function and pass in those parameters. In your case, the class name is Employee within the Employee module, so you would do this:
entry = Employee.Employee(id, number, department, job_title)
This will create a new Employee object and call its __init__ method with the parameters you passed in.
回答2:
I was able to write a working code, check it out
note emp represents Employee
while emp_dic represent employee dictionary
class Employee():
def __init__(self, name, number, department, job):
self.name = name
self.number = number
self.department = department
self.job = job
def set_name(self, name):
self.name = name
def set_number(self, number):
self.number = number
def set_department (self, department):
self.department = department
def set_job (self, job):
self.job = job
def get_name(self):
return self.name
def get_number(self):
return self.number
def get_department(self):
return self.department
def get_job(self):
return self.job
def __str__(self):
return "Name: " +self.name+ "\nID Number "+self.number+"\nDepartment "+self.department+"\nJob "+self.job
import pickle
def main():
emp_dic = load_emp()
menu()
choice = input('Enter your choice')
while choice!=5:
if choice == 1:
lookup(emp_dic)
elif choice == 2:
add_(emp_dic)
elif choice == 3:
change(emp_dic)
elif choice ==4:
dele_(emp_dic)
elif choice <= 0 or choice > 5:
print 'You made a wrong selection'
elif choice == 5:
print "The program would quit now..."
print''
print''
menu()
choice = input('Enter your choice')
save_emp(emp_dic)
def load_emp():
try:
load_dic = open('employee.dat' , 'rb')
emp_details = pickle.load(load_dic)
load_dic.close()
except IOError:
emp_details = {}
return emp_details
def save_emp(emp_dic):
save_file = open('employee.dat','wb')
pickle.dump(emp_dic , save_file)
save_file.close()
def lookup(emp_dic):
search = raw_input("Enter your search query")
search_result = emp_dic.get(search, "Entry not found")
print search_result
def add_(emp_dic):
again = 'y'
while again.lower() == 'y':
name_ = raw_input("Enter employee name")
number = raw_input("Enter the ID number")
depart = raw_input("Enter Department")
job = raw_input("Enter Job title")
if name_ not in emp_dic:
entry = Employee(name_ ,number, depart, job)
emp_dic[name_] = entry
print name_, "has been successfully added"
else:
print name_, "already exists!"
again = raw_input("Enter 'y' to continue or any other alphabet to quit")
def change(emp_dic):
search = raw_input("Enter the name you want to change the details")
if search in emp_dic:
name_ = raw_input("Enter new employee name")
number = raw_input("Enter new the ID number")
depart = raw_input("Enter new Department")
job = raw_input("Enter new Job title")
entry = Employee(name_,number, depart, job)
emp_dic[name_] = entry
print name_, "has been successfully updated"
else:
print "Entry not found"
def delete_ (emp_dic):
search = raw_input("Enter the name you want to change the details")
if search in emp_dic:
del emp_dic[search]
print search, " has been deleted successfully"
else:
print "Entry not found"
def menu():
print 'Choose your Option below'
print "Look-up Employee Details = 1"
print 'Add new Employee Details = 2'
print 'Change an existing Employee Details = 3'
print 'Delete an Employee Details = 4'
print 'Quit the program = 5'
main()
来源:https://stackoverflow.com/questions/39606186/python-class-employee-management-system