首页 > 代码库 > python实现 --工资管理系统

python实现 --工资管理系统

技术分享
# -*- coding: utf-8 -*-
__author__ = hjianli
# import re
import os

info_message = """Alex 100000
Rain 80000
Egon 50000
Yuan 30000
"""
#序列字典
xulie_dict = {}

#工资字典
gongzi_dict = {}

#本地创建的文件名
file_name = "info.txt"

#创建文件的路径
Path = os.getcwd()
os.chdir(Path)


#创建文件函数
def flush_info_txt(file_name, info=info_message):
    with open(file_name, "wt", encoding="utf8") as f:
        f.write(info + "\n")

#追加新用户信息使用函数
def add_info_txt(file_name, info=None):
    with open(file_name, "a+", encoding="utf8") as f:
        f.writelines(info)

#读取用户和工资信息函数
def read_file_txt(file_name):
    with open(file_name) as f:
        f = f.read()
    return f

#将读取出来的姓名和工资形成字典对应
def info_user():
    with open(file_name) as f:
        file = f.read().strip().split()
        keys = [x for x in range(len(file)) if x % 2 == 0]
        value = [x for x in range(len(file)) if x % 2 == 1]
        for x, y in zip(keys,value):
            gongzi_dict[file[x]] = file[y]
    return gongzi_dict

#检测初始文件是否存在。不存在就创建,存在就忽略
def check_file():
    if not os.path.exists(file_name):
        flush_info_txt(file_name)
    else:
        pass
check_file()

#检测输入是否合理
def check_error(shuru):
    if len(shuru) == 0:
        print("您输入的为空,请检查输入!")
    elif str(shuru).isdigit() == False :
        print("输入了错误的字符,请输入数字")
    else:
        return True

#检测输入的用户名是否在字典的keys中
def check_user(shuru):
    if shuru not in gongzi_dict.keys():
        pass
    elif len(shuru) == 0 or int(len(shuru)) > 6:
        print("用户姓名输入错误!不符合姓名的格式.超过6个字段.")
    else:
        print("您输入的用户出现冲突,用户姓名必须唯一,请核对该用户的信息")

choice_exit = True

def check_choice(choice):
    if (len(choice) != 0) and (int(choice) in list_num):
        for i, q in enumerate(list_choice):
            xulie_dict[i+1] = q

        for key, value in xulie_dict.items():
            if int(choice) == 4:
                print("......程序正在退出..........")
                exit(2)

            elif int(choice) == key:
                    input_user_name = input("您选择的是:({}),请输入想要{}的员工姓名:".format(key, value[0:2]))
                    dict_name = info_user()
                    if input_user_name in dict_name.keys():
                        if key == 1:
                            print("{}的工资是:{}\n******************************".format(input_user_name,dict_name[input_user_name]))
                            break

                        elif key == 2:
                            dict_name = info_user()
                            old_gongzi = dict_name[input_user_name]
                            # print(old_gongzi)
                            update_gongzi = input("请输入修改后的工资金额:")
                            check_error(update_gongzi)
                            dict_name[input_user_name] = update_gongzi
                            update_gongzi = dict_name[input_user_name]
                            str_info = read_file_txt(file_name)
                            str_info = str_info.replace(old_gongzi, update_gongzi)
                            # print(str_info)
                            flush_info_txt(file_name, info=str_info)
                            print("修改成功!")


                        else:
                            pass


                    elif key ==3:
                        new_gongzi = input("请输入他的工资: ")
                        check_user(input_user_name)
                        new_user_info = str(input_user_name) + " " + new_gongzi
                        add_info_txt(file_name, new_user_info)
                        print("新用户增加成功")
                    else:
                        print("您输入的用户名不存在,请检查后重新输入!")
                        break

    else:
        print("您输入了错误的数字,请检查重新输入是否在{}范围内".format(list_num))


while choice_exit:
    list_choice = ["查询员工工资", "修改员工工资", "增加新员工记录", "退出"]
    for i, q in enumerate(list_choice):
        print(str(i+1) + "." + str(q))

    list_num = [x+1 for x in range(len(list_choice))]

    input_number = input(">>>>请输入您要进行的操作: ")
    if str(input_number).isdigit():
        check_choice(input_number)
    else:
        check_error(input_number)
        continue
View Code

 

python实现 --工资管理系统