利用面向对象的思想设计一个员工管理系统。

创建员工类:属性有:员工号,员工姓名,员工工资;

创建用户类:属性有:用户名,密码;

1:初始化5个员工对象。(可以使用列表、元组、字典存储) 持久化存储数据

初始化3个用户对象。(可以使用列表、元组、字典存储)

2:功能菜单:

一级菜单:登录(需要验证身份); (验证用户名和密码)

二级菜单:登录成功进入二级菜单,

(1)、查询所有员工信息

(2)、根据工号查询员工信息

(3)、添加员工

(4)、修改员工工资

(5)、删除员工

(6)、退出

要求:菜单可循环,除非选择6退出

请逐个实现菜单中6个功能

数据库结构

employee

员工管理系统python程序 python 员工管理系统_员工管理系统python程序


user

员工管理系统python程序 python 员工管理系统_员工管理系统python程序_02


1)数据库连接conn.py

import pymysql

# 连接数据库
conn = pymysql.connect(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    db="test",
    charset="utf8"
)
cur = conn.cursor()

2)功能实现operation.py

import conn

cur = conn.cur


def login():
    """
    一级目录,用户登录
    :return:
    """
    a = 0
    for i in range(0, 3):
        username = input("请输入用户名:")
        password = input("请输入密码:")
        try:
            sql = "select password from user where username = '%s'" % username
            cur.execute(sql)
            res = cur.fetchone()
            if res is None:
                print("用户名错误,登录失败")
            else:
                my_password = res[0]
                print("-"*30)
                if my_password == password:
                    print("登录成功!")
                    break
                else:
                    print("密码错误,登录失败。")
        except Exception as e:
            print(e)


def query_all():
    """
    查询所有员工信息
    :return:
    """
    try:
        sql = "select * from employee"
        cur.execute(sql)
        res = cur.fetchall()
        for i in ["员工号", "员工姓名", "员工工资"]:
            print(i, end="\t\t")
        print("")
        for row in res:
            id = row[0]
            name = row[1]
            salary = row[2]
            print("%s\t\t%s\t\t%s\t\t" %
                  (id, name, salary))
        print("所有员工信息显示完毕!")
        print("-" * 50)
        print("")
    except Exception as e:
        print(e)
    pass


def query_id():
    """
    根据工号查询员工信息
    :return:
    """
    print("-" * 50)
    print("根据工号查询员工信息")
    id = input("请输入要查询员工的工号:")
    try:
        sql = "select * from employee where id = '%s'" % id
        cur.execute(sql)
        res = cur.fetchone()
        for i in ["员工号", "员工姓名", "员工工资"]:
            print(i, end="\t\t")
        print("")
        id = res[0]
        name = res[1]
        salary = res[2]
        print("%s\t\t%s\t\t%s\t\t" %
              (id, name, salary))
        print("该员工信息显示完毕!")
        print("-" * 50)
        print("")
    except Exception as e:
        print(e)
    pass


def add_staff():
    """
    添加员工
    :return:
    """
    print("-" * 50)
    print("添加新员工信息")
    id = input("员工号:")
    name = input("员工姓名:")
    salary = input("员工工资:")
    try:
        sql = "insert into employee values('%s','%s','%s')"
        data = (id, name, salary)
        cur.execute(sql % data)
        conn.conn.commit()
        print("添加新员工信息成功!")
        print("-" * 50)
        print("")
    except Exception as e:
        print(e)
    pass


def update_staff():
    """
    修改员工工资
    :return:
    """
    print("-" * 50)
    print("修改员工工资")
    id = input("请输入要修改工资的员工的员工号:")
    salary = input("请输入该员工的新工资:")
    try:
        sql = "update employee set salary = '%s' where id = '%s' "
        data = (salary, id)
        cur.execute(sql % data)
        conn.conn.commit()
        print("修改该员工的薪水成功!")
        print("-" * 50)
        print("")
    except Exception as e:
        print(e)
    pass


def del_staff():
    """
    删除员工
    :return:
    """
    print("-" * 50)
    print("删除员工")
    id = input("请输入要删除员工的员工号:")
    try:
        sql = "delete from employee where id = '%s'"
        data = (id)
        cur.execute(sql % data)
        conn.conn.commit()
        print("删除员工信息成功!")
        print("-" * 50)
        print("")
    except Exception as e:
        print(e)
    pass


def close():
    """
    关闭数据库
    :return:
    """
    cur.close()
    conn.conn.close()

3)主函数employee_system.py

import conn
import operation as op


class Staff:
    def __init__(self, staff_id, staff_name, staff_salary):
        """
        初始化
        :param staff_id: 员工号
        :param staff_name: 员工姓名
        :param staff_salary: 员工工资
        """
        self.staff_id = staff_id
        self.staff_name = staff_name
        self.staff_salary = staff_salary


class User:
    def __init__(self, user_name, user_password):
        """
        用户类初始化
        :param user_name: 用户名
        :param user_password: 密码
        """
        self.user_name = user_name
        self.user_password = user_password


def menu():
    print("")
    print("*" * 50)
    print("【员工管理系统】V1.0")
    print("")
    print("1.查询所有员工信息")
    print("2.根据工号查询员工信息")
    print("3.添加员工")
    print("4.修改员工工资")
    print("5.删除员工")
    print("")
    print("6.退出")
    print("*" * 50)
    print("")


def choose():
    """
    将要选择的操作
    :return:
    """
    while True:
        temp = input("请输入想要执行的操作:")
        print("您选择的操作是%s:" % temp)
        if temp in ["1", "2", "3", "4", "5"]:
            if temp == "1":
                op.query_all()
            if temp == "2":
                op.query_id()
            if temp == "3":
                op.add_staff()
            if temp == "4":
                op.update_staff()
            if temp == "5":
                op.del_staff()
        elif temp == "6":
            print("退出程序,谢谢使用!")
            break
        else:
            print("您输入的信息不正确,请重新输入!")


def main():
    op.login()
    menu()
    choose()
    op.close()


if __name__ == '__main__':
    main()