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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM