首页 > 代码库 > python 练习4

python 练习4

题目为信用卡消费管理系统:

 

主程序:main.py

#!usr/bin/env python
# encoding: utf-8
import conf,sys,time,re,os
import json
import login
tmp_dic = {}

def shopping_center(arg):
    print(\033[32mWelcome into Credit card store!\033[0m)
    while True:
        for i,(k,v) in enumerate(conf.shop_list.items(),1):
            print(%s.%s:%s%(i,k,v))
        N = int(input(Please select you want to buy goods(input r:return p:pay):).strip())
        if N == 7:
            print(\033[31myou will return main list!\033[0m)
            time.sleep(1)
            tmp_dic.clear()#返回后如果没有支付则清空购物车
            break

        elif N in range(1,len(conf.shop_list)-1):
            key = list(conf.shop_list.items())[N - 1][0]
            value = list(conf.shop_list.items())[N - 1][1]
            if key in tmp_dic.keys():
                tmp_dic[key] = tmp_dic[key] + value
            else:
                tmp_dic[key] = value
            print(\033[35mYour shopping cart is as follows:\033[0m)
            for x,y in tmp_dic.items():
                print(\033[35m%-5s:%s\033[0m%(x,y))
        elif N == 8:
            print(\033[33mThe total order for you:%s\033[0m%sum(tmp_dic.values()))
            sum_values = sum(tmp_dic.values())
            with open(account.json,r) as f:
                acc = json.load(f)
            if sum_values > acc[arg][2]:
                print(对不起你的信用卡额度不足,请重新购买!!)
                login.log_record(arg, 消费, sum_values, fail)
                tmp_dic.clear()
                break
            else:
                acc[arg][2] = acc[arg][2] - sum_values
                acc[arg][3] = acc[arg][3] + sum_values
                with open(account.json,w) as z:
                    json.dump(acc,z)
                login.log_record(arg,消费,sum_values,success)
                print(恭喜你购物成功即将返回主菜单!!)
                time.sleep(1)
                tmp_dic.clear()
                break

#信用卡管理
def card_center(name):
    if name == admin:
        print(欢迎%s进入信用卡管理系统%name)
        while True:
            for index,i in enumerate(conf.ad_list,1):
                print(%s.%s %(index,i))
            p = int(input(请选择你要的操作的编号:).strip())
            with open(account.json, r+) as f:
                acc = json.load(f)
            if p == 1:#添加用户
                key = input(请输入你的用户名:).strip()
                pawd = input(请输入密码:).strip()
                paws = input(请输入支付密码:).strip()
                acc[key] = [pawd, paws, 15000,0, "unlock"]
                with open(account.json, w) as x:
                    json.dump(acc,x)
                print(用户%s已经添加成功额度为15000%key)

            elif p == 2:#用户额度调整
                n = input(你的账号:)
                limit = int(input(请输入你要的额度:).strip())
                if n in acc.keys():
                    acc[n][2] = limit
                    with open(account.json,w) as y:
                        json.dump(acc,y)
                    continue
            elif p == 3:#冻结账户
                freeze = input(请输入你要冻结的账户:).strip()
                acc[freeze][3] = "lock"
                with open(account.json, w) as z:
                    json.dump(acc, z)
                print(账号%s已冻结%freeze)
                continue
            elif p == 4:#解冻账户
                unfreeze = input(请输入你要解冻的账户:)
                acc[unfreeze][3] = "unlock"
                with open(account.json,w) as g:
                    json.dump(acc,g)
                print(账户%s已解冻%unfreeze)
                continue
            elif p == 5:#用户操作查询
                pass
                continue
            elif p == 6:#返回主菜单
                break
    else:#消费者信用卡管理系统
        print(\033[34m欢迎进入消费者信用卡管理系统!\033[0m)
        with open(account.json, r+) as e:
            acc1 = json.load(e)
        while True:
            for l,i in enumerate(conf.customer_list,1):
                print(%s.%s%(l,i))
            custom_in = int(input(请选择你需要的操作:).strip())
            if custom_in == 1:
                print(# * 20)
                print(账户:%s\n额度:%s\n待还款:%s %(name,acc1[name][2],acc1[name][3]))
                print(# * 20)
                continue
            elif custom_in == 2:
                print(你的还款额:%s%acc1[name][3])
                huank = int(input(你要还多少:).strip())
                acc1[name][2] = acc1[name][2] + huank
                acc1[name][3] = acc1[name][3] - huank
                with open(account.json, w) as g:
                    json.dump(acc1, g)
                print(恭喜你还款成功!)
                continue
            elif custom_in == 3:
                qukuan = int(input(请输入你要取款的金额:).strip())
                acc1[name][2] = acc1[name][2] - qukuan
                acc1[name][3] = acc1[name][3] + qukuan
                with open(account.json, w) as h:
                    json.dump(acc1, h)
                continue
            elif custom_in == 4:
                print(你的消费记录如下)
                ff = open(record.log,r)
                for line in ff:
                    if name in line:
                        print(line)
                ff.close()
                print(-*20)
                continue
            elif custom_in == 5:
                while True:
                    pa = input(请输入你要转账的账户:).strip()
                    pc = int(input(请输入你要转账的金额:).strip())
                    if pa not in acc1.keys():
                        print(你输入的账户不存在,请重新输入!)
                        continue
                    elif pc > acc1[name][2]:
                        print(你的转账额度不够,请重新输入!)
                        continue
                    else:
                        break
                acc1[pa][3] = acc1[pa][3] - pc
                acc1[pa][2] = acc1[pa][2] + pc
                acc1[name][2] = acc1[name][2] - pc
                acc1[name][3] = acc1[name][3] + pc
                with open(account.json,w) as dd:
                    json.dump(acc1,dd)
                print(恭喜你转账成功!)
                continue
            elif custom_in == 6:
                break
            else:
                print(你选择的编号不对,请重新输入!!)
                continue


if __name__ == "__main__":
    while True:
        print(\033[32mWelcome into credit card store!!!\033[0m)
        for index,i in enumerate(conf.main_list,1):
            print(%s.%s%(index,i))
        Num = input(Please choose you to enter the system(input b exit):).strip()
        if Num == b:
            print(1)
            sys.exit()
        elif int(Num) == 1:
            name1 = login.login()
            card_center(name1)
            continue
        elif int(Num) == 2:
            name = login.login()
            shopping_center(name)
            continue

配置文件:conf.py

 

#!usr/bin/env python
# encoding: utf-8
main_list = [Credit card supervise,Credit card store]

shop_list = {Car:100000,
             Iphone:5000,
             Mac Book:8000,
             pan:10,
             T-shirt:100,
             coffee:30,
             return:返回主菜单,
             pay:买单,
             }

ad_list = [添加用户,用户额度调整,冻结账户,解冻账户,用户操作查询,返回上一级]
customer_list = [账户查询,还款,提现,消费查询,转账,返回]

 

 登录系统和日志系统:login.py

#!usr/bin/env python
# encoding: utf-8

import json,sys,time
# account = {‘wyh‘:[‘a123‘,123,15000,‘unlock‘],
#            ‘bat‘:[‘b321‘,456,15000,‘unlock‘],
#            ‘admin‘:[‘admin‘,0,0,‘unlock‘]}
# with open(‘account.json‘,‘w‘) as d:
#     json.dump(account,d)
logfile = record.log

def login():
    with open(account.json, r) as f:
        acc = json.load(f)
    i = 0
    while True:
        name = input(请输入账号:)
        password = input(请输入密码:)
        if acc[name][3] == lock:
            print(你的账户被锁定请联系你的客户经理!!!)
            sys.exit()
        elif name not in acc.keys() or password != acc[name][0] :
            print(你的账户信息请重新输入!!!)
            i += 1
            if i > 2:
                print(你的账户被锁定请联系你的客户经理确认你的账户安全!!)
                acc[name][3] = lock
                with open(account.json, w) as f1:
                    json.dump(acc, f1)
                sys.exit()
            continue
        else:
            print(欢迎%s%name)
            return name

def log_record(name,Status,money,description=Null):
    date = time.strftime("%Y%m%d %H:%M:%S", time.localtime())
    record_line = "%s  %s   %s  %s  %s\n" % (date, name, Status,money, description)
    f = open(logfile, a)
    f.write(record_line)
    f.flush()
    f.close()

 

 账号文件:account.json

{"wyh": ["a123", 123, 14870, 100, "unlock"], "bat": ["b321", 456, 10070, 4930, "unlock"], "admin": ["admin", 0, 0, 0, "unlock"]}

 

python 练习4