Python类:员工管理系统 [英] Python class: Employee management system

查看:418
本文介绍了Python类:员工管理系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我将以上内容保存为Employee.py到一个文件夹中.然后,我启动了一个新文件,并将其另存为Employee Management System.py 这是该文件中的代码

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

我的第一个问题是我正在尝试使用Employee.py中创建的属性.具体来说, init 并向其中添加条目.我知道上面的代码不在最合乎逻辑的论坛中,但是我试图首先查看我是否可以添加数据,然后首先对文件进行腌制.如果我能做这两件事,那么以后其他所有内容都将很容易弄清楚.

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.

有点让我想起

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

推荐答案

您要执行的操作是使用给定的参数实例化Employee对象.为此,您只需将类名当作函数来调用,然后传入这些参数即可.在您的情况下,类名在Employee模块中是Employee,因此您可以这样做:

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)

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

This will create a new Employee object and call its __init__ method with the parameters you passed in.

这篇关于Python类:员工管理系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆