美文网首页
学习python之路--Day2:工资管理系统

学习python之路--Day2:工资管理系统

作者: houyizhong | 来源:发表于2017-10-21 16:17 被阅读0次

需求

工资管理系统
Alex 100000
Rain 80000
Egon 50000
Yuan 30000
-----以上是info.txt文件-----
实现效果:
从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件。
效果演示:

  1. 查询员工工资
  2. 修改员工工资
  3. 增加新员工记录
  4. 退出
    >>:1
    请输入要查询的员工姓名(例如:Alex):Alex
    Alex的工资是:100000。
    \1. 查询员工工资
    \2. 修改员工工资
    \3. 增加新员工记录
    \4. 退出

:2
请输入要修改的员工姓名和工资,用空格分隔(例如:Alex 10):Alex 10
修改成功!

  1. 查询员工工资
  2. 修改员工工资
  3. 增加新员工记录
  4. 退出
    >>:3
    请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):Eric 100000
    增加成功!
    \1. 查询员工工资
    \2. 修改员工工资
    \3. 增加新员工记录
    \4. 退出
    >>:4
    再见!

这个作业和上周我们做的购物车商家接口,管理商品列表的模块十分类似,可以借鉴一下。
流程图我们可以先画:

salary_manage

实现

根据这个操作流程,我们可以先写出一个demo:

#owner:houyizhong
#version:1.0

staff_dict={}

f=open("info.txt","r")
lines=f.readlines()
f.close()

#read staff
for i in lines:
      raw=i.split(" ")
      name=raw[0]
      salary=raw[1].strip()
      staff_dict[name]=salary

#print staff
f=open("info.txt","r")
for line in f:
      print line,
f.close()

#write file
def write_file():
      f=open("info.txt","w")
      for name in staff_dict:
              f.write("{0} {1}\n".format(name,staff_dict[name]))
      f.close()

def search():
      choice_name=input("请输入要查询的员工姓名(例如:Alex):")
      if choice_name in staff_dict:
              salary=staff_dict[choice_name]
              print("{0}的工资是:{1}".format(choice_name,salary))
      else:
              print("Sorry,your enter is not a staff!")

def modify():
      enter=input("请输入要修改的员工姓名和工资,用空格分隔(例如:Alex 10):")
      list=enter.split(" ")
      name=list[0]
      salary=list[1]
      if name in staff_dict:
              staff_dict[name]=salary
              print("修改成功!")
      else:
              print("Sorry,your enter is not a staff!")

def add():
      enter=input("请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):")
      list=enter.split(" ")
      name=list[0]
      salary=list[1]
      staff_dict[name]=salary
      print("操作成功!")

while True:
      print("1. 查询员工工资\n2. 修改员工工资\n3. 增加新员工记录\n4. 退出")
      choice=input(">>")
      if choice == "1":
              search()
      elif choice == "2":
              modify()
      elif choice == "3":
              add()
      elif choice == "4":
              write_file()
              exit("再见!")
      else:
              print("Your enter invalid!Try again.")

这里用字典来盛放员工信息,每次修改只是对字典的修改,只有退出的时候才会将字典写入文件里,这么做的道理是,我修改员工信息或者新增员工时就不用重复的打开、写入文件了,每次运行程序只需一次写入文件操作,大大降低了文件操作的负担,同时避免了修改完文件之后,字典里还是显示启动时读取的内容,没有跟上后续内容的变化,而我们这样写就可以实时看到、修改自己刚才增改的内容了。

按照这个程序运行结果来看,确实实现了需求所述的内容。现在我们来对一些细枝末节的东西做些优化。
我们注意到这个程序还有一个bug,就是修改新增操作的时候如果只写了员工名,没有加工资是会有问题的,所以我们也需要对这一部分内容做个判断,优化之后的代码如下:

#-*- coding:utf-8 -*-
#owner:houyizhong
#version:2.0

staff_dict={}

f=open("info.txt","r")
lines=f.readlines()
f.close()

#read staff
for i in lines:
        raw=i.split(" ")
        name=raw[0]
        salary=raw[1].strip()
        staff_dict[name]=salary

#print staff
f=open("info.txt","r")
for line in f:
        print (line,end='')
f.close()

#write file
def write_file():
        f=open("info.txt","w")
        for name in staff_dict:
                f.write("{0} {1}\n".format(name,staff_dict[name]))
        f.close()

def search():
        choice_name=input("请输入要查询的员工姓名(例如:Alex):")
        if choice_name in staff_dict:
                salary=staff_dict[choice_name]
                print("{0}的工资是:{1}".format(choice_name,salary))
        else:
                print("Sorry,your enter is not a staff!")

def modify():
        enter=input("请输入要修改的员工姓名和工资,用空格分隔(例如:Alex 10):")
        list=enter.split(" ")
        if len(list) == 2:
            name=list[0]
            salary=list[1]
            if salary.isdigit():
                if name in staff_dict:
                        staff_dict[name]=salary
                        print("修改成功!")
                else:
                        print("Sorry,your enter is not a staff!")
            else:
                    print("Please enter correct salary!")
        else:
                print("Please enter correct information!")

def add():
        enter=input("请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):")
        list=enter.split(" ")
        if len(list) == 2:
            name=list[0]
            salary=list[1]
            if salary.isdigit():
                staff_dict[name]=salary
                print("操作成功!")
            else:
                print("Please enter correct salary!")
        else:
                print("Please enter correct information!")

while True:
        print("1. 查询员工工资\n2. 修改员工工资\n3. 增加新员工记录\n4. 退出")
        choice=input(">>")
        if choice == "1":
                search()
        elif choice == "2":
                modify()
        elif choice == "3":
                add()
        elif choice == "4":
                write_file()
                exit("再见!")
        else:
                print("Your enter invalid!Try again.")

我们在modify(),add()这两个函数的主逻辑里,加入了if len(list) == 2: 这句话,这样就能判断用户输入的信息是否是两个,是否符合要求,同时也加入了if salary.isdigit():这句,用来判断输入的最后一项是不是个数字。

结语

我们利用了字典,函数和读取写入文件方面的知识,为了避免操作的复杂性,和为了代码的易读性,我们将所有的修改操作都先写入字典中,再由字典写入文件,文件即时修改方面可能不够快,但是保证了信息的一致性和完整性。这个在以后的程序设计中,是一个可以借鉴和考虑的地方。

相关文章

网友评论

      本文标题:学习python之路--Day2:工资管理系统

      本文链接:https://www.haomeiwen.com/subject/pwiiuxtx.html