HR系统程序初步

 

import os
import time
count = 0
while count < 3:
    username = input('请输入管理员账户名>>>').strip()
    password = input('请输入管理员账户密码>>>').strip()
    if username == "admin" and password == '123':
        count = 3
        print('>>>>>>>>>>>欢迎登陆人力资源管理系统<<<<<<<<<<<<<<')
        flag = 1
        f1 = open('emp.db', 'r+', encoding='utf-8')
        while flag:
            choice = input('''
请输入你要选择的操作编号(1-5):
1> 查看员工信息,2> 添加员工信息,3> 修改员工信息,4> 删除员工信息,5> 退出>>>''').strip()
            if choice == '5':
                print('退出系统...')
                break
            elif choice == '1':
                print('以下是所有员工的信息:\n',f1.read())
                f1.seek(0)
                find_n = input('请输入你要查找的员工姓名或id>>>').strip()
                for line in f1:
                    dic1 = eval(line.strip())
                    if dic1['name'] == find_n or dic1['id'] == find_n:
                        print('以下是%s员工的信息\n' % find_n ,line.strip())
                        f1.seek(0)
                        break
                else:
                    print('查无此人,请确认员工姓名或id')
                    f1.seek(0)
                    continue
            elif choice == '2':
                dic2 = {}
                while 1:
                    id_a = input('请输入要添加的员工id号码>>>').strip()
                    f1.seek(0)
                    for line in f1:
                        if id_a == eval(line.strip())['id']:
                            print('该id已被注册,请重新输入...')
                            break
                    else:
                        name_a = input('请输入要添加的员工姓名>>>').strip()
                        birthday_a = input('请输入要添加的员工生日>>>').strip()
                        salary_a = input('请输入要添加的员工工资>>>').strip()
                        time_a = input('请输入要添加的员工入职时间>>>').strip()
                        break
                f1.seek(0,2)
                dic2['id'] = id_a
                dic2['name'] = name_a
                dic2['birthday'] = birthday_a
                dic2['salary'] = salary_a
                dic2['time'] = time_a
                f1.write(str(dic2) + '\n')
                f_log = open('emp.log','a+',encoding= 'utf-8')
                f_log.write('管理员【%s】在%s执行了【添加】员工信息操作,添加的员工信息为%s\n' % \
                            (username,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),dic2))
                f_log.flush()
                f_log.close()
                f1.seek(0)
                continue
            elif choice == '3':
                print('以下是所有员工的信息:\n', f1.read())
                f1.seek(0)
                id_b = input('请输入你要修改的员工id>>>').strip()
                for line in f1:
                    if id_b == eval(line.strip())['id']:
                        d_change = eval(line.strip())
                        salary_b = input('请输入修改后的工资>>>').strip()
                        f1.seek(0)
                        f2 = open('emp_副本','w+',encoding= 'utf-8')
                        for line in f1:
                            dic3 = eval(line.strip())
                            if id_b == dic3['id']:
                                dic3['salary'] = salary_b
                                f2.write(str(dic3)+'\n')
                            else:
                                f2.write(line)
                        f_log2 =  open('emp.log', 'a+', encoding='utf-8')
                        f_log2.write('管理员【%s】在%s执行了【修改】员工信息操作,修改的员工信息为%s\n' % \
                                     (username,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),d_change))
                        f_log2.flush()
                        f_log2.close()
                        f2.flush()
                        f2.close()
                        f1.flush()
                        f1.close()
                        os.remove('emp.db')
                        os.rename('emp_副本','emp.db')
                        flag = 0
                        break
                else:
                    f1.seek(0)
                    print('查无此id,请确认...')
                    continue
            elif choice == '4':
                print('以下是所有员工的信息:\n', f1.read())
                f1.seek(0)
                id_c = input('请输入你要删除的员工id>>>').strip()
                for line in f1:
                    if id_c == eval(line.strip())['id']:
                        f1.seek(0)
                        with open('emp_副本','w',encoding='utf-8') as f3:
                            lis = []
                            for line in f1:
                                if id_c == eval(line.strip())['id']:
                                    lis.append(line.strip())
                                    continue
                                else:
                                    f3.write(line)
                        f1.flush()
                        f1.close()
                        os.remove('emp.db')
                        os.rename('emp_副本', 'emp.db')
                        f_log3 = open('emp.log', 'a+', encoding='utf-8')
                        f_log3.write('管理员【%s】在%s执行了【删除】员工信息操作,删除的员工信息为%s\n' % \
                                     (username,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),lis[0]))
                        f_log3.flush()
                        f_log3.close()
                        flag = 0
                        break
                else:
                    f1.seek(0)
                    print('查无此id,请确认...')
                    continue
            else:
                print('....输入错误.请重新输入....')
    else:
        count += 1
        if count == 3:
            print('登陆失败....')
            break
        else:
            print('用户名或密码错误,请重新登陆......')
            continue

 

以下是该程序设计要求:

 

 

准备emp.db文本文件如图所示:实际操作仅需先填入一行,并换行

 

 

posted on 2018-11-05 21:07  Python_rookie  阅读(158)  评论(0编辑  收藏  举报