Python类:员工管理系统

时间:2016-09-21 01:33:39

标签: python class object pickle

本练习假定您已为编程练习4创建了Employee类。 创建一个将Employee对象存储在字典中的程序。使用员工ID号 作为关键。该程序应提供一个菜单,让用户执行以下操作: •在字典中查找员工 •将新员工添加到词典中 •在字典中更改现有员工的姓名,部门和职位 •从字典中删除员工 •退出程序 当程序结束时,它应该腌制字典并将其保存到文件中。每一次 程序启动时,应尝试从文件中加载pickle字典。如果文件没有 存在,程序应该以空字典开头。

好的,这是我的解决方案: -

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保存在一个文件夹中。然后我启动了一个新文件并将其保存为Employee Management 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 个答案:

答案 0 :(得分:2)

您要做的是使用给定参数实例化Employee对象。要做到这一点,您只需将类名称称为函数并传入这些参数即可。在您的情况下,Employee模块中的类名称为Employee,因此您可以这样做:

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

这将创建一个新的Employee对象,并使用您传入的参数调用其__init__方法。

答案 1 :(得分:1)

我能够编写一个有效的代码,检查出来 note 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()