• 工资管理系统(初级实现)


    1、前言:代码引入函数后,能够对重复代码进行缩略,大大减轻程序的臃肿,所以说,函数是个好东西。下面程序简单的实现了一个工资管理系统,提供增、改、查、退出四个简单功能,每个功能由一个独立的函数实现。用户数据是保存在文件当中,所以以下代码的实现需要在掌握python中文件操作的基本技能后在进行阅读,下述代码较为简陋,后期会逐渐进行多多校正,请多提宝贵意见,谢谢!!!

    注意:若实现功能,以下两个文件必须存在(1、保存用户数据  2、主程序)

    info.txt      salary_management_system.py

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    #author:sunwei
    #data:2017-9-22
    import os,time
    info = '''
    -----------------------------------------
    -         工资管理系统                  -
    -    1、查询工资                        -
    -    2、修改员工工资                    -
    -    3、增加新员工记录                  -
    -    4、退出程序                        -
    -----------------------------------------
    '''
    print("33[31;40m欢迎进入工资管理系统.....")
    time.sleep(2)
    os.system("clear")
    def print_info(info):
        print("33[33;40m%s33[0m"%info)
    
    #查询
    def search_salary(salary_info,search_name,salary):
        for i  in salary_info:
            i = i.strip().split()
            if i[0] == search_name:
                salary = i[1]
        return salary
            
        
    
    #修改
    def change_salary(change_user_salary,salary_info):
        with open("info_bak.txt","a+",encoding="utf-8") as salary_info_bak:
            change_flag = False
            for i in salary_info:
                i1 = i.strip().split()
                if change_user_salary.startswith(i1[0]):
                    change_flag = True
                    salary_info_bak.write(change_user_salary+"
    ")
                else:
                    salary_info_bak.write(i)
            if change_flag:
                os.remove("info.txt")
                os.rename("info_bak.txt","info.txt")        
                print("33[5;31;40m修改成功!!!33[0m")
            else:
                os.remove("info_bak.txt")
                print("33[5;31;40m员工不存在!!!33[0m")
    
    #增加
    def add_info(add_user,salary_info):
        with open("info_bak.txt","w+",encoding="utf-8") as salary_info_bak:
            user_exit_flag = False
            i1 = add_user.strip().split()
            for i in salary_info:
                i2 = i.strip().split()
                if i1[0] == i2[0]:
                    user_exit_flag = True
                else:
                    salary_info_bak.write(i)
            if user_exit_flag:
                print("33[5;31;40m用户已存在!!!33[0m")
                os.remove("info_bak.txt")
            else:
                salary_info_bak.write(add_user+"
    ")
                os.remove("info.txt")
                os.rename("info_bak.txt","info.txt")        
                print("33[5;31;40m增加成功!!!33[0m")
    
    while True:
        with open("info.txt","r",encoding="utf-8") as salary_info:
            print_info(info)
            user_input = input("输入选择>>:").strip()
            if len(user_input) == 0:continue
            if user_input == "1": #查询
                search_name = input("请输入要查询的员工姓名(例如:test):").strip()
                salary = ""
                salary = search_salary(salary_info,search_name,salary)
                if salary:
                    salary = int(salary)
                    print("33[31;40m"+search_name+"的工资是:%d33[31;40m"%salary)
                else:
                    print("33[5;31;40m查询的员工不存在!!!33[0m")
            elif user_input == "2": #修改
                change_user_salary = input("请输入要修改的员工姓名和工资,用空格分隔(例如:test 10):")
                change_salary(change_user_salary,salary_info)    
            elif user_input == "3": #增加
                add_user = input("请输入要增加的员工姓名和工资,共空格分隔(例如:test 1000):")
                if add_user:
                    add_info(add_user,salary_info)
                else:
                    print("33[5;31;40m员工不存在!!!33[0m")
            elif user_input == "4": #退出
                while True:
                    exit_flag = input("33[31;40m确认要退出程序?(y/n): 33[0m").strip()
                    if len(exit_flag) == 0:continue
                    if exit_flag == "y" or exit_flag == "Y":
                        exit()
                    elif exit_flag == "n" or exit_flag == "N":
                        break
    salary_management_system.py
    在你说话之前,先听;在你回应之前,先想;在你消费之前,先挣;在你退出之前,先试
  • 相关阅读:
    Linked List Cycle
    黑白多帧相机设置白平衡原理
    cefsharp+elementplus
    Vue3(1)
    [Leetcode Weekly Contest]272
    [Leetcode Weekly Contest]273
    [Leetcode Weekly Contest]283
    ikbc104键键盘的win键失效的解决办法
    pytest接口自动化学习
    阿凡达项目的严重问题,质量过程改进,敏捷开发,
  • 原文地址:https://www.cnblogs.com/sunweigogogo/p/7589746.html
Copyright © 2020-2023  润新知