第三章 作业员工信息增删改查程序

1、readme

作业名称:员工信息增删改查程序

测试环境:win7系统,python3.7.0,工具:pycharm-community-2018.1.4


博客地址:https://www.cnblogs.com/foremostxl/

程序文件:购物车程序.py

程序根据: 创建一个staff_table.txt用于存储原始员工信息
      创建一个restaff_table.txt用于存储更新后的员工信息
        
    文件修改的占硬盘方式:1、对于python3 中原始员工信息表可能因为认为因素产生空行,对于这个问题,我采用的解决办法是读取原始文件信息,删除空格,然后存入新的文本。
         
            

现要求你写一个简单的员工信息增删改查程序,需求如下:

当然此表你在文件存储时可以这样表示

1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02
1.可进行模糊查询,语法至少支持下面3种查询语法:

find name,age from staff_table where age > 22

find * from staff_table where dept = "IT"

find * from staff_table where enroll_date like "2013"
2.可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),staff_id需自增

语法: add staff_table Alex Li,25,134435344,IT,2015-10-29
3.可删除指定员工信息纪录,输入员工id,即可删除

语法: del from staff_table where  id=3
4.可修改员工信息,语法如下:

UPDATE staff_table SET dept="Market" WHERE  dept = "IT" 把所有dept=IT的纪录的dept改成Market
UPDATE staff_table SET age=25 WHERE  name = "Alex Li"  把name=Alex Li的纪录的年龄改成25
5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。

注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!

2、程序如下:

def all_need():
    global file
    file = open('restaff_table.txt', 'r+', encoding='utf-8')
    f = file.readlines()
    global  data_list
    data_list = []
    for i in f:
        i = i.strip()
        data_list.append(i.split(','))

def rewrite():
    file.seek(0)
    file.truncate()
    for line in data_list:
        str = ','.join(line)
        file2 = open('restaff_table.txt', 'a', encoding='utf-8')
        file2.write(str+'\n')
        file2.close()

def find_info():
    while True:
        choice2 = input("""
(请选择查询序号):
1 find name,age from staff_table where age > 22
2 find * from staff_table where dept = "IT"
3 find * from staff_table where enroll_date like "2013"
q 退出
>""").strip()
        all_need()
        count = 0
        if choice2 == 'q':
            file.close()
            break
        if choice2 not  in ['1','2','3']:
            print('\033[31m input wrong \033[0m ')
        for i in range(0, len(data_list)):
            if choice2 == '1':
                if int(data_list[i][2]) > 22:
                    data_str = ' '.join(data_list[i][1:3]).strip('\n')
                    print(data_str)
                    count += 1
                    #file.close()
            if choice2 == '2':
                if data_list[i][4] == 'IT':
                    data_str = ' '.join(data_list[i]).strip('\n')
                    print(data_str)
                    count += 1
                    #file.close()
            if choice2 == '3':
                if '2013' in data_list[i][5]:
                    data_str = ' '.join(data_list[i]).strip('\n')
                    print(data_str)
                    count += 1
                    #file.close()
        print(f"\033[31m 查询结束,符合条件数为{count} \033[0m")

def add_info():
    count1 = 0
    while True:
        count = 0
        choice3 = input("""
(请选择序号):
1 (录入员工信息:)
q 退出
>""").strip()
        if choice3 == '1':
            all_need()
            new_staff_info = input('''
请按如下顺序输入:
name,age,phone,dept,enroll-date
>''').strip()
            new_staff_list = new_staff_info.split(',')
            new_staff_id = str(len(data_list)+1)
            new_staff_list.insert(0,new_staff_id)
            if len(new_staff_list) != 6:
                print('\033[31m 输入信息不完整,请重新输入 \033[0m')
                continue
            else:
                for i in range(0, len(data_list)):
                    if data_list[i][3] == new_staff_list[3]:
                        print('\033[31m 已存入该员工信息!\033[0m')
                        count += 1
                        break
            if count == 0:
                new_staff_str = ','.join(new_staff_list)
                file2 = open('restaff_table.txt', 'a+', encoding='utf-8')
                file2.write('\n'+new_staff_str)
                file2.close()
                count1 += 1
                print(f"\033[31m 新添加的员工信息为:{new_staff_list}\033[0m ")
        if choice3 not in ['1','q']:
            print('\033[31m 输入有误,请选择 1 或者 q \033[0m')
        if choice3 == 'q':
            print(f"\033[31m 添加结束,共添加{count1}个员工信息\033[0m")
            break

def del_info():
    count = 0
    while True:
        all_need()
        id_list = []
        for index in range(0,len(data_list)):
            id_list.append(data_list[index][0])
        print(f'\033[31m id的取值范围为\033[0m {id_list}')
        staff_id = input("""        
\033[31m 请输入需要删除的员工id\033[0m
id:delete from staff_table where staff_id = 3
q:退出
>""").strip()
        if staff_id =='q':
            print(f"\033[31m  删除结束,共删除{count}个员工 \033[0m")
            break
        if staff_id not in id_list:
            print('\033[31m  输入无效id \033[0m')
            continue
        for j in reversed(range(0,len(data_list))):

            if data_list[j][0] == staff_id:
                del data_list[j]
                print(f'\033[31m 已删除id为{staff_id}的员工信息 \033[0m')
                rewrite()
        count += 1
def modify_info():
    while True:
        modify = input("""
请选择下列序号:
1 把所有dept=IT的纪录的dept改成Market
2 把name=Alex Li的纪录的年龄改成25
q 退出
>""").strip()
        all_need()
        if modify == 'q':
            break

        count = 0
        for i in range(0, len(data_list)):
            if modify == '1':
                if data_list[i][4] == 'IT':
                    data_list[i][4] = 'Market'
                    rewrite()
                    count += 1
            if modify == '2':
                if data_list[i][1] == "Alex Li":
                    data_list[i][2] = '25'
                    rewrite()
                    count += 1

        print(f"\033[31m共改动{count}处信息\033[0m")
        if modify not in ['1','2','q']:
            print('输入错误,请重新输入')
            continue

#---------------主函数----------------------
print('-----欢迎登陆员工管理系统------')
file1 = open('staff_table.txt', 'r', encoding='utf-8')  # 要去掉空行的文件
file2 = open('restaff_table.txt', 'w', encoding='utf-8')  # 生成没有空行的文件
try:
    for line in file1.readlines():
        if line == '\n':
            line = line.strip("\n")
        file2.write(line)
finally:
    file1.close()
    file2.close()
while True:
    choice = input("""
请选择下列序号:
1 查询 
2 增
3 删
4 改
q 退出
>""").strip()
    if choice == '1':
        find_info()
    if choice == '2':
        add_info()
    if choice == '3':
        del_info()
    if choice == '4':
        modify_info()
    if choice == 'q':
        exit()

猜你喜欢

转载自www.cnblogs.com/foremostxl/p/9579699.html