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

 

转载于:https://www.cnblogs.com/hujianli/p/7236939.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值