day13 员工管理系统 (登录注册部分)

import file_funcs
from employers import Employers
import human_resource_sys

file_employer = 'employers'
# employee = 'employees'
name = 'name'
password = 'password'


# 登录注册界面
def list_login():
    print('''
    +++++++++++++++++++++++++++++++++++++++++++++++++
    +        welcome to human resource system       +
    +                                               +
    +                   1.register                  +
    +                                               +
    +                   2.login                     +
    +                                               +
    +                   0.exit                      +
    +++++++++++++++++++++++++++++++++++++++++++++++++
    ''')
    cmd = input('>>>>>>>')
    if '0' <= cmd <= '2':
        return cmd
    else:
        print('输入内存错误!')
        return False


def register():
    user_name = input('请输入用户名:')
    # 取出说有的用户信息,列表
    all_info = file_funcs.get_content(file_employer)
    for info in all_info:
        # 用户个人信息,对象
        user = Employers.set_employer(info)
        if user_name == user.name:
            print('该用户名已经存在!')
            return False
    else:
        print('用户名可用!')
        pass_word = input('请输入密码:')
        pass_word2 = input('请再次输入密码:')
        if pass_word == pass_word2:
            new_user = {name: user_name, password: pass_word}
            new_employer = Employers.set_employer(new_user)
            file_funcs.set_content(file_employer, new_employer.__dict__)
            print('注册成功!')

            return user_name
        else:
            print('两次密码不一致!')
            return False


def login():
    user_name = input('请输入用户名:')
    all_info = file_funcs.get_content(file_employer)
    print(all_info)
    for info in all_info:
        user = Employers.set_employer(info)
        if user_name == user.name:
            pass_word = input('请输入密码:')
            if pass_word == user.password:
                print('登录成功!')
                return True
            else:
                print('密码错误!')
                return False

    print('用户名不存在!')


# 用户登录
def main():
    while True:
        cmd = list_login()
        if not cmd:
            continue

        if cmd == '1':
            result = register()
            if result:
                human_resource_sys.main(result)

        if cmd == '2':
            login()

        if cmd == '0':
            exit(0)


if __name__ == '__main__':
    main()

employee

class Employees:
    """员工类"""

    def __init__(self):
        self.name = ''
        self._age = 0
        self.id = ''
        self._salary = 0
        self.position = ''
        self.department = ''

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        if not age.isdigit():
            print('输入数据类型错误!')

        elif 16 < age < 70:
            print('不在法定工作年龄内!')

        else:
            self._age = int(age)

    @property
    def salary(self):
        return self._salary

    @salary.setter
    def salary(self, salary):
        if not salary.isdigit():
            print('输入数据类型错误!')
        else:
            self._salary = int(salary)

    @classmethod
    def set_employee(cls, employee):
        new_employee = cls()
        for key in employee:
            if key == 'age':
                new_employee.age = employee[key]

            if key == 'salary':
                new_employee.salary = employee[key]

            new_employee.__setattr__(key, employee[key])

        return new_employee


if __name__ == '__main__':
    pass

employer

class Employers:
    """雇主类"""

    def __init__(self):
        self.name = ''
        self.password = ''
        self.employees = []

    @classmethod
    def set_employer(cls, employer):
        new_employer = cls()
        for key in employer:
            new_employer.__setattr__(key, employer[key])

        return new_employer


if __name__ == '__main__':
    pass

文件操作

import json


def get_content(file_name):
    try:
        with open('./files/' + file_name + '.json', 'r', encoding='utf-8') as f:
            content = json.load(f)
    except FileNotFoundError:
        content = []
    
    return content


def set_content(file_name, new_content):
    content = get_content(file_name)
    with open('./files/' + file_name + '.json', 'w', encoding='utf-8') as f:
        content.append(new_content)
        json.dump(content, f)
        return True


def reset_content(file_name, new_content):
    with open('./files/' + file_name + '.json', 'w', encoding='utf-8') as f:
        json.dump(new_content, f)
        return True


if __name__ == '__main__':
    pass

human resource

import dao
file_employer = 'employers'


def list_human_resource():
    print('''
        +++++++++++++++++++++++++++++++++++++++++++++++++
        +       welcome to human resource system        +
        +       1.add new employee                      +
        +       2.View employee's information by id     +
        +       3.del employee by id                    +
        +       4.view all employees' information       +
        +       5.view employees' average age           +
        +       6.view the most high salary             +
        +       0.exit                                  +
        +++++++++++++++++++++++++++++++++++++++++++++++++
            ''')
    user_cmd = input('>>>>>')

    if '0' <= user_cmd <= '6':
        return user_cmd
    else:
        print('输入内存错误!')
        return False


def human_resource(user_cmd):
    while True:
        if not user_cmd:
            continue

        # 添加员工
        if user_cmd == '1':
            while True:
                new_employee = dao.employee_input()
                dao.add_employee(file_employer, new_employee, employer_name)

                # 判断是否继续或者退出
                print('''
                                    1.继续添加
                                    2.返回上一层
                                    ''')
                user_cmd = input('>>>>>')
                if user_cmd == '1':
                    continue
                elif user_cmd == '2':
                    return
                else:
                    print('输入有误!')
                    return

        # 查看员工个人信息
        if user_cmd == '2':
            pass

        # 删除员工
        if user_cmd == '3':
            pass

        # 查看所有员工
        if user_cmd == '4':
            dao.find_all(employer_name)

        # 查看平均年龄
        if user_cmd == '5':
            pass

        # 查看最高工资
        if user_cmd == '6':
            pass

        # 退出程序
        if user_cmd == '0':
            exit(0)


def main(employer):
    global employer_name
    employer_name = employer

    while True:
        human_resource(list_human_resource())


if __name__ == '__main__':
    pass

你可能感兴趣的:(day13 员工管理系统 (登录注册部分))