2016-09-21 116 views
1

本練習假定您已創建用於編程練習4的Employee類。 創建一個將Employee對象存儲在字典中的程序。使用員工ID號 作爲關鍵。該程序應顯示一個菜單,讓用戶執行以下操作: •在字典中查找員工 •將新員工添加到字典 •更改字典中現有員工的姓名,部門和職位 •從字典中刪除一個員工 •退出程序 程序結束時,它應該醃製字典並將其保存到文件中。每次 程序啓動時,它應該嘗試從文件加載pickle字典。如果該文件不存在 ,則該程序應以空字典開頭。Python類:員工管理系統

好了,所以這裏是我的解決方案: -

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 

我救了上面Employee.py一個文件夾中。然後,我開始一個新的文件並保存,作爲員工管理System.py 下面是該文件

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() 

的代碼我的第一個問題是,我試圖使用Employee.py創建attribue。特別是init並添加條目。我知道上面的代碼是不是在最合乎邏輯的論壇,但我想先看看我是否可以添加數據然後醃文件第一。如果我可以在如何完成這兩件事情的情況下,其他一切都很容易理解。

它種讓我想起了

import math 
x = math.pi(3.14) 
x = module.function(3.14) 

但我不能似乎只是做了兩個例子之間的連接。 謝謝

回答

2

你要做的是用給定的參數實例化一個Employee對象。要做到這一點,你只需要調用類名稱就好像它是一個函數並傳入這些參數。在你的情況下,類名是EmployeeEmployee模塊內,所以你可以這樣做:

 entry = Employee.Employee(id, number, department, job_title) 

這將創建一個新的Employee對象,並調用其__init__方法與您傳遞的參數

+0

我求你ght就是這樣,但Employee.Employee對我來說似乎是錯誤的。 – JDDoe

+0

你也可以請給我一些關於如何更新pickle文件的建議。 – JDDoe

+0

我仍然沒有得到它。這是我到目前爲止 http://pastebin.com/QLfKywtN – JDDoe

1

我可以寫一個工作代碼,檢查出來
注EMP代表員工 而emp_dic代表員工詞典

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() 
+1

歡迎來到社區。你能詳細說明你的例子嗎?到底究竟是什麼? – Litty

+0

謝謝,這個例子實際上解決了整個問題。 正在閱讀一本教科書,問的問題是在那裏練習,我能解決它。所以這個例子解決了整個問題 – Rammy