人力資源管理系統1.0

weixin_34041003發表於2018-08-02
"""__author__= xxx"""
"""
物件導向的人力資源管理系統:
功能:
    1.能存多個員工資訊(姓名、年齡、工號、薪資、職位、部門)
    2.新員工入職:新增員工
    3.員工離職:刪除員工
    4.檢視某個員工資訊
    5.查詢薪資最高的員工
    6.查詢指定部門中所有員工的平均薪資
    7.求整個公司的員工的平均年齡

"""
import json


def read_json():
    """獲取json檔案"""
    try:
        with open('./company.json', 'r', encoding='utf-8') as f:
            return json.load(f)
    except (FileNotFoundError, json.decoder.JSONDecodeError):
        with open('./company.json', 'w', encoding='utf-8') as f:
            json.dump({}, f)
        with open('./company.json', 'r', encoding='utf-8') as f:
            return json.load(f)


def write_json(content):
    """寫json檔案"""
    with open('./company.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


class Staff:
    """員工類"""

    def __init__(self, name='', age=0, number='', salary=0, position='', sect=''):
        self.name = name
        self.age = age
        self.number = number
        self.salary = salary
        self.position = position
        self.sect = sect

    def __str__(self):
        return '姓名:%s, 年齡:%d, 工號:%s, 薪資:%d, 職位:%s, 部門:%s' % \
               (self.name, self.age, self.number, self.salary, self.position, self.sect)


# class Sect:
#     """部門類"""
#     def __init__(self, name=''):
#         self.name = name
#         self.count = 0
#         self.staffs = []
#
#     def add_staffs(self):
#         """新增員工"""
#         self.count += 1
#         count = self.count
#         print('請輸入員工的資訊')
#         add_name = input('姓名:')
#         add_age = int(input('年齡:'))
#         add_number = self.name + str(count).rjust(3, '0')
#         add_salary = int(input('薪資:'))
#         add_position = input('職位:')
#         sta = Staff(add_name, add_age, add_number, add_salary, add_position, self.name)
#         self.staffs.append(sta)
#
#     def del_staffs(self):
#         """刪除學生"""

class Company:
    """公司類"""

    def __init__(self, name='', boss=''):
        self.com_name = name
        self.com_boss = boss
        self.sects = []
        # self.staffs = []

    def add_staff(self):
        """新增員工"""
        sect = input('請輸入新增員工所屬部門:')
        count = 0
        flag = True
        # 判斷部門是否存在
        for item in self.sects[:]:
            if sect in item['sect_name']:
                flag = False
                count = item['count']
                break
        if flag:
            sect_list = {'sect_name': sect, 'staffs': [], 'count': 0}
            self.sects.append(sect_list)
            count = 0

        print('請輸入員工的資訊')
        add_name = input('姓名:')
        add_age = int(input('年齡:'))
        count += 1
        add_number = sect + str(count).rjust(3, '0')
        add_salary = int(input('薪資:'))
        add_position = input('職位:')
        sta = {'name': add_name, 'age': add_age, 'number': add_number, 'salary': add_salary, 'position': add_position,
               'sect': sect}
        # sta = Staff(add_name, add_age, add_number, add_salary, add_position, sect)

        # 將員工新增進去
        for item in self.sects[:]:
            if sect == item['sect_name']:
                item['staffs'].append(sta)
                item['count'] = count

        return self.sects

    def del_staff(self):
        """刪除員工"""
        # 根據姓名刪除員工
        del_name = input('請輸入要刪除的員工姓名:')
        flag = True
        del_list = []
        index = 0
        for index1 in range(0, len(self.sects)):
            staffs = self.sects[index1]['staffs']
            for index2 in range(0, len(staffs)):
                if del_name == staffs[index2]['name']:
                    flag = False
                    del_list.append([index1, index2])
                    print(index, staffs[index2])
                    index += 1
        if flag:
            print('本公司沒有這個人!')
        else:
            index_input = int(input('請輸入要刪除的員工下標:'))
            if 0 <= index_input <= index-1:
                del self.sects[del_list[index_input][0]]['staffs'][del_list[index_input][1]]
                print('刪除成功')
            else:
                print('下標輸入錯誤!')

        return self.sects

    def find_staff(self):
        """檢視員工資訊"""
        # 根據姓名檢視員工資訊
        find_name = input('請輸入要檢視的員工姓名:')
        flag = True
        for index1 in range(0, len(self.sects)):
            staffs = self.sects[index1]['staffs']
            for index2 in range(0, len(staffs)):
                if find_name == staffs[index2]['name']:
                    print(self.sects[index1]['staffs'][index2])
                    flag = False
        if flag:
            print('本公司沒有這個人!')

    def find_max_salary(self):
        """檢視公司薪資最高的員工"""
        max_salary = 0
        staff = {}
        for item in self.sects[:]:
            item1 = item['staffs']
            for item2 in item1:
                if max_salary <= item2['salary']:
                    max_salary = item2['salary']
                    staff = item2
        print(staff)

    def avg_salary(self):
        """檢視公司某部門的平均薪資"""
        avg1 = None
        sum1 = 0
        sect_name_input = input('請輸入部門的名字:')
        for item in self.sects[:]:
            if sect_name_input == item['sect_name']:
                for item1 in item['staffs']:
                    sum1 += item1['salary']
                avg1 = sum1 / len(item['staffs'])
        print('該部門的平均薪資為:%d' % avg1)

    def avg_age(self):
        """檢視公司所有員工的平均年齡"""
        avg2 = None
        sum2 = 0
        index2 = 0
        for item in self.sects[:]:
            for item1 in item['staffs']:
                sum2 += item1['age']
                index2 += 1
        avg2 = sum2 / index2
        print('該公司的平均年齡為:%d' % avg2)


def print_main():
    print('人力資源管理系統')
    print('1.新增員工')
    print('2.刪除員工')
    print('3.檢視員工資訊')
    print('4.檢視公司薪資最高的員工')
    print('5.檢視公司某部門的平均薪資')
    print('6.檢視公司平均年齡')
    print('0.退出')


if __name__ == '__main__':
    content = read_json()
    com1 = Company('qianfeng', 'xxx')
    content['com_name'] = com1.com_name
    content['com_boss'] = com1.com_boss
    write_json(content)
    while True:
        print_main()
        num = input('>>>')
        if num == '0':
            break
        if num == '1':
            content = read_json()
            if not content.get('sects'):
                content['sects'] = []
            com1.sects = content['sects']
            print(com1.sects)
            sects = com1.add_staff()
            content['sects'] = sects
            write_json(content)
            print('新增成功')
            continue
        if num == '2':
            content = read_json()
            com1.sects = content['sects']
            sects = com1.del_staff()
            content['sects'] = sects
            write_json(content)
            continue
        if num == '3':
            content = read_json()
            com1.sects = content['sects']
            com1.find_staff()
            continue
        if num == '4':
            content = read_json()
            com1.sects = content['sects']
            com1.find_max_salary()
            continue
        if num == '5':
            content = read_json()
            com1.sects = content['sects']
            com1.avg_salary()
            continue
        if num == '6':
            content = read_json()
            com1.sects = content['sects']
            com1.avg_age()
            continue

相關文章