模擬實現一個ATM + 購物商城程序
作業需求:
- 額度 15000或自定義
- 實現購物商城,買東西加入 購物車,調用信用卡接口結賬
- 可以提現,手續費5%
- 每月22號出賬單,每月10號為還款日,過期未還,按欠款總額 萬分之5 每日計息
- 支持多賬戶登錄
- 支持賬戶間轉賬
- 記錄每月日常消費流水
- 提供還款接口
- ATM記錄操作日志
- 提供管理接口,包括添加賬戶、用戶額度,凍結賬戶等。。。
- 用戶認證用裝飾器
-
## ATM信用卡購物模擬程序 ### 作者介紹: * author:高原 ### 功能介紹: 模擬實現一個ATM + 購物商城程序 額度 15000或自定義 實現購物商城,買東西加入 購物車,調用信用卡接口結賬 可以提現,手續費5% 支持多賬戶登錄 支持賬戶間轉賬 記錄每月日常消費流水 提供還款接口 ATM記錄操作日志 提供管理接口,包括添加賬戶、用戶額度,凍結賬戶等。。。 用戶認證用裝飾器 目錄結構: atm作業 ├── README ├── atm #ATM主程目錄 │ ├── bin #ATM 執行文件 目錄 │ │ ├── __init__.py │ │ ├── atm_start.py #ATM 主程序 執行程序 │ │ │ ├── conf #配置文件 │ │ ├── __init__.py │ │ └── settings.py #未用到 │ ├── core #主要程序邏輯都 在這個目錄 里 │ │ ├── __init__.py │ │ ├── auth.py #用戶,信用卡,管理員認證模塊 │ │ ├── log.py #日志記錄模塊 │ │ ├── creditcard.py #信用卡模塊\轉賬\還款\取現等 │ │ ├── shopping.py #購物模塊\商城\購物車\購物結算等 │ │ ├── main.py #主邏輯交互程序 │ │ └── user.py # 用戶模塊\創建\鎖定\解鎖等 │ ├── db #數據庫 │ │ ├── __init__.py │ │ ├── Blacklist # 用戶黑名單文件 │ │ └── user_data #用戶文件,用戶的各種信息 │ │ └── creditcard #信用卡文件,信用卡的各種信息 │ │ └── shopping_car #購物車文件 │ │ └── shopping_list #商品列表文件 │ └── log #日志目錄 │ ├── __init__.py └── └── water_record #所有的用戶,信用卡交易日志
程序簡易流程圖
程序目錄結構:
atm作業 ├── README ├── atm #ATM主程目錄
│ ├── bin #ATM 執行文件 目錄
│ │ ├── __init__.py │ │ ├── atm_start.py #ATM 主程序 執行程序
│ │ │ ├── conf #配置文件
│ │ ├── __init__.py │ │ └── settings.py #未用到
│ ├── core #主要程序邏輯都 在這個目錄 里
│ │ ├── __init__.py │ │ ├── auth.py #用戶,信用卡,管理員認證模塊
│ │ ├── log.py #日志記錄模塊
│ │ ├── creditcard.py #信用卡模塊\轉賬\還款\取現等
│ │ ├── shopping.py #購物模塊\商城\購物車\購物結算等
│ │ ├── main.py #主邏輯交互程序
│ │ └── user.py # 用戶模塊\創建\鎖定\解鎖等
│ ├── db #數據庫
│ │ ├── __init__.py │ │ ├── Blacklist # 用戶黑名單文件
│ │ └── user_data #用戶文件,用戶的各種信息
│ │ └── creditcard #信用卡文件,信用卡的各種信息
│ │ └── shopping_car #購物車文件
│ │ └── shopping_list #商品列表文件
│ └── log #日志目錄
│ ├── __init__.py └── └── water_record #所有的用戶,信用卡交易日志
bin目錄:
atm執行程序

'''atm執行程序''' import sys core_path = r"F:\PycharmProjects\py_fullstack_s4\atm作業\atm\core" sys.path.insert(0,core_path) from main import * if __name__ == '__main__': main()
core目錄:
用戶,信用卡認證模塊

#用戶認證,信用卡認證,管理員認證模塊 import os import json BASE_DIR = os.path.dirname(os.path.dirname(__file__)) user_dic = BASE_DIR + r"/db/user_data" creditcard_dic = BASE_DIR + r"/db/creditcard_data" '''認證裝飾器''' def auth(auth_type): def out_wrapper(func): if auth_type == "user_auth": #用戶認證 def wrapper(): res = func user_name = input("請輸入登錄用戶名 :").strip() user_pwd = input("請輸入登錄密碼 :").strip() if len(user_name)>0: with open(user_dic,"r") as f: user_data = json.loads(f.read()) if user_name in user_data.keys() and user_pwd == user_data[user_name]["password"]: if user_data[user_name]["locked"] == False: print("[ %s ] 用戶認證成功"%(user_name)) return res,user_name else: print("[ %s ] 用戶已經被鎖定,認證失敗"%(user_name)) else: print("[ %s ] 用戶或密碼錯誤,認證失敗"%(user_name)) else: print("[ %s ] 用戶輸入不能為空"%(user_name)) return wrapper if auth_type == "creditcard_auth": #信用卡認證 def wrapper(): res = func() creditcard = input("請輸入信用卡卡號(6位數字):").strip() password = input("請輸入信用卡密碼 : ").strip() if creditcard: with open(creditcard_dic,"r") as f: creditcard_data = json.loads(f.read()) if creditcard in creditcard_data.keys() and password == creditcard_data[creditcard]["password"]: if creditcard_data[creditcard]["locked"] == False: print("信用卡 [ %s ] 驗證成功"%(creditcard)) return res,creditcard else: print("信用卡 [ %s ]已經被凍結,請使用其他信用卡"%(creditcard)) else: print("信用卡卡賬號或密碼輸入錯誤") else: print("信用卡賬號輸入不能為空") return wrapper if auth_type == "admin_auth": #管理員認證 def wrapper(): res = func() admin_dic = {"admin":"admin","passwrod":"123"} admin_name = input("請輸入管理員賬號 :").strip() admin_pwd = input("請輸入密碼 :").strip() if admin_name: if admin_name in admin_dic and admin_pwd == admin_dic["passwrod"]: print("管理員賬號[%s] 登陸成功。"%(admin_name)) return res,admin_name else: print("賬號或密碼錯誤") else: print("管理員賬號輸入不能為空") return wrapper return out_wrapper @auth(auth_type="user_auth") def user_auth(): print("用戶登錄認證".center(40,"-")) return "True" @auth(auth_type="creditcard_auth") def creditcard_auth(): print("信用卡登錄認證".center(40,"-")) return "True" @auth(auth_type="admin_auth") def admin_auth(): print("管理員登錄認證".center(40,"-")) return "True"
日志記錄模塊

# 日志記錄模塊 import logging,os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) user_Water = BASE_DIR + r"/log/water_record" '''日志模塊''' def get_logger(): #有循環的時候 要放到循環外面 fh = logging.FileHandler(user_Water) # 創建一個文件流,需要給一個文件參數 logger = logging.getLogger() # 獲得一個logger對象 #sh = logging.StreamHandler() # 創建一個屏幕流, logger.setLevel(logging.DEBUG) # 設定最低等級debug # 寫入文件的中的格式 fm = logging.Formatter("%(asctime)s --- %(message)s") logger.addHandler(fh) # 把文件流添加進來,流向文件 #logger.addHandler(sh) # 把屏幕流添加進來,流向屏幕 fh.setFormatter(fm) # 在文件流添加寫入格式 #sh.setFormatter(fm) # 在屏幕流添加寫入格式 return logger
用戶功能模塊,創建用戶,鎖定用戶,解鎖用戶,登陸失敗三次鎖定用戶

import json,os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''數據庫文件相對路徑''' user_dic = BASE_DIR + r"/db/user_data" user_Blacklist = BASE_DIR + r"/db/Blacklist_user" '''創建用戶''' def new_user(address="None", locked=False, creditcard=False): while True: print("開始創建用戶".center(50, "-")) with open(user_dic, "r+") as f: user_data = json.loads(f.read()) for key in user_data: print("系統已有用戶 [ %s ]" % (key)) choice = input("是否創建新的用戶 確定'y'/返回'q':") if choice == "q": break if choice == "y": user_name = input("請輸入要創建的用戶名:").strip() user_pwd = input("請輸入創建用戶的密碼 :").strip() if user_name not in user_data.keys(): if len(user_name) > 0 and len(user_pwd) > 0: user_data[user_name] = {"username": user_name, "password": user_pwd, "creditcard": creditcard, "address": address,"status":False,"locked": locked} dic = json.dumps(user_data) f.seek(0) f.truncate(0) f.write(dic) print("用戶 %s 創建成功\n" % (user_name)) else: print("輸入的用戶或密碼不能密碼為空") else: print("用戶 %s 已經存在\n" % (user_name)) '''解鎖用戶''' def unlock_user(): while True: print("解鎖用戶".center(50, "-")) with open(user_dic, "r+") as f: user_data = json.loads(f.read()) for key in user_data: if user_data[key]["locked"] == False: print("用戶 [ %s ]\t\t鎖定狀態:[未鎖定]" % (key)) else: print("用戶 [ %s ]\t\t鎖定狀態:[已鎖定]" % (key)) choice = input("是否進行用戶解鎖 : 確定 'y' 返回 'q' :").strip() if choice == "q":break if choice == "y": unlock_user = input("請輸入要解鎖的用戶名:").strip() if unlock_user in user_data.keys(): if user_data[unlock_user]["locked"] == True: user_data[unlock_user]["locked"] = False dict = json.dumps(user_data) f.seek(0) f.truncate(0) f.write(dict) print("\33[31;1m用戶 %s 解鎖成功\33[0m\n" % (unlock_user)) else: print("用戶 %s 解鎖失敗 用戶未被鎖定" % (unlock_user)) else: print("用戶 %s 不存在"%(unlock_user)) '''鎖定用戶''' def lock_user(): while True: print("鎖定用戶".center(50, "-")) with open(user_dic, "r+") as f: user_data = json.loads(f.read()) for key in user_data: if user_data[key]["locked"] == False: print("用戶 [ %s ]\t\t鎖定狀態:[未鎖定]"%(key)) else: print("用戶 [ %s ]\t\t鎖定狀態:[已鎖定]" % (key)) choice = input("是否進行用戶鎖定 : 確定'y' 返回'q' :") if choice == "q":break if choice == "y": lock_user = input("請輸入要鎖定的用戶名 :") if lock_user in user_data.keys(): if user_data[lock_user]["locked"] == False: user_data[lock_user]["locked"] = True dic = json.dumps(user_data) f.seek(0) f.truncate(0) f.write(dic) print("\33[31;1m用戶 %s 鎖定成功\33[0m\n" % (lock_user)) else: print("用戶 %s 已經鎖定\n" % (lock_user)) else: print("用戶 %s 不存在\n"%(lock_user)) '''三次鎖定''' def lock(): while True: with open(user_Blacklist,"r+") as f: blacklist_data = json.loads(f.read()) #print(blacklist_data) user_name = input("請輸入登錄的用戶名 : ").strip() if user_name in blacklist_data.keys(): if blacklist_data[user_name] == 3 : print("您的 %s 用戶已經在黑名單中 !!" %(user_name)) continue with open(user_dic,"r+") as f1: user_data = json.loads(f1.read()) if user_name in user_data.keys(): if user_data[user_name]["status"] == False: if blacklist_data[user_name] != 3: user_pwd = input("請輸入用戶 [ %s ] 的登錄密碼: "%(user_name)).strip() if user_pwd and user_pwd == user_data[user_name]["password"]: print("用戶 [ %s ] 登陸成功"%(user_name)) user_data[user_name]["status"] = True data = json.dumps(user_data) f1.seek(0) f1.truncate(0) f1.write(data) break else: print("用戶 [ %s ] 密碼輸入錯誤:"%(user_name)) blacklist_data[user_name] += 1 data = json.dumps(blacklist_data) #print(blacklist_data) f.seek(0) f.truncate(0) f.write(data) else: print("用戶 [ %s ] 已被加入黑名單" % (user_name)) data = json.dumps(blacklist_data) f.seek(0) f.truncate(0) f.write(data) else: print("用戶 [ %s ] 已經在登錄狀態" % (user_name)) else: print("用戶 [ %s ] 不存在,請重新輸入:" % (user_name))
購物商城模塊,購物,購物車,購物結算,查看購物記錄,清空購物車

import json,os from creditcard import link_creditcard from auth import creditcard_auth from log import get_logger BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''數據庫文件相對路徑''' shopping_dic = BASE_DIR + r"/db/shopping_record" shopping_lis = BASE_DIR + r"/db/shopping_list" shopping_car = BASE_DIR + r"/db/shopping_car" creditcard_dic = BASE_DIR + r"/db/creditcard_data" user_dic = BASE_DIR + r"/db/user_data" user_Water = BASE_DIR + r"/log/water_record" logger = get_logger() #日志模塊實例化對象 '''購物商城''' def shopping_mall(): shopping_list,pro_list = [],[] with open(shopping_lis, "r", encoding="utf-8") as f: for item in f: pro_list.append(item.strip("\n").split()) def pro_inf(): print("\t\t\t\t編號\t商品\t\t價格") for index, item in enumerate(pro_list): print("\t\t\t\t%s\t\t%s\t\t%s" % (index+1, item[0], item[1])) while True: print(("目前商城在售的商品信息").center(50, "-")) pro_inf() choice_id = input("選擇要購買的商品編號 '購買 ID' 返回 'q':") if choice_id.isdigit(): choice_id = int(choice_id) if choice_id <= len(pro_list) and choice_id >=0: pro_item = pro_list[choice_id-1] print("商品 [ %s ] 加入購物車 價格 [ ¥%s ] "%(pro_item[0],pro_item[1])) shopping_list.append(pro_item) shopp_data = ["商品",str(pro_item[0]), "價格", str(pro_item[1])] msg = "---".join(shopp_data) logger.debug(msg) else: print("沒有相應的編號 請重新輸入:") elif choice_id == "q": with open(shopping_car, "r+") as f: list = json.loads(f.read()) list.extend(shopping_list) f.seek(0) f.truncate(0) list = json.dumps(list) f.write(list) break else: print("沒有相應的編號 請重新輸入:") '''購物車''' def Shopping_car(): while True: with open(shopping_car, "r+") as f: list = json.loads(f.read()) sum = 0 print("購物車信息清單".center(40,"-")) print("id\t商品\t價格") for index,item in enumerate(list): print("%s\t%s\t%s"%(index+1,item[0],item[1])) sum +=int(item[1]) print("商品總額共計: ¥%s"%(sum)) choice = input("請選擇要進行的操作 返回 'q' 清空'f':").strip() if choice == "q" :break if choice == "f": del_shopping_car() break '''清空購物車''' def del_shopping_car(): while True: with open(shopping_car, "r+") as f: res = json.loads(f.read()) if res != []: choice = input("是否清空購物車 確定 'y' 返回 'q' :").strip() print("購物車里的商品".center(50, "-")) print(res, "\n") if choice == "q":break if choice == "y": list = json.dumps([]) f.seek(0) f.truncate(0) f.write(list) print("購物車已清空") else: print("請輸入正確的指令: ‘y' 或 ’q' ") else: print("您還沒有消費過,去商城花點錢把") break '''購物結算''' def shopping_pay(): while True: print("購物結算".center(50, "-"),"\n") with open(shopping_car, "r+") as f: data = json.loads(f.read()) if data != []: print("購物車信息清單".center(50, "-")) print("\t\t\t\t\tid\t商品\t價格") for index, item in enumerate(data): print("\t\t\t\t\t%s\t%s\t%s" % (index + 1, item[0], item[1])) money = sum([int(i[1]) for i in data]) else: print("您還沒有消費過,去商城花點錢把") break choice = input("當前商品總額:[ ¥%s ] 是否進行支付 :確定 'y' 返回 'q':" % (money)) if choice == "q": break if choice == "y": creditcard_auth() # 信用卡認證模塊 #break user_name = input("請輸入結算的用戶賬號:").strip() with open(user_dic, "r+") as f1: user_data = json.loads(f1.read()) if user_name in user_data.keys(): user_creditcard = user_data[user_name]["creditcard"] if user_creditcard == False: print("賬號 %s 未綁定信用卡,請先綁定信用卡" % (user_name)) link_creditcard() #信用卡綁定模塊 else: with open(creditcard_dic, "r+") as f2: creditcard_data = json.loads(f2.read()) pwd = input("請輸入 信用卡[ %s ]支付密碼 :" % (creditcard_data[user_creditcard]["creditcard"])) if pwd == creditcard_data[user_creditcard]["password"]: limit = creditcard_data[user_creditcard]["limit"] limit_new = limit - money limit_not = creditcard_data[user_creditcard]["limitcash"] - money // 2 if limit_new >= 0: creditcard_data[user_creditcard]["limit"] = limit_new creditcard_data[user_creditcard]["limitcash"] = limit_not shop_data = [user_name,str(creditcard_data[user_creditcard]["creditcard"]), "信用卡結賬", str(money)] msg = "---".join(shop_data) dict = json.dumps(creditcard_data) f2.seek(0) f2.truncate(0) f2.write(dict) logger.debug(msg) print("支付成功-->>>\t購物支付:[ ¥%s ]\t當前額度還剩 : [ ¥%s ]\n" % (money, limit_new)) break else: print("當前信用卡額度 %s元 不足矣支付購物款 可綁定其他信用卡支付\n" % (limit)) else: print("密碼錯誤,請重新輸入!!!") continue else: print("您輸入的用戶不存在") '''查看購物記錄''' def cat_shopp_record(): while True: with open(user_dic) as f: user_data = json.loads(f.read()) choice = input("請輸入要查看購物記錄的用戶名:").strip() if choice in user_data.keys(): print("用戶 %s 購物記錄".center(50, "-")%(choice)) with open(user_Water) as f1: for i in f1: if choice in i: print(i.strip()) #print("\33[31;0m用戶 %s 還沒有進行過消費\33[0m\n" % (choice)) choice1 = input("返回 'q':") if choice1 == "q": break else: print("您輸入的用戶名 [ %s ] 不存在"%(choice))
信用卡模塊,取現,還款,轉賬等等............

'''信用卡信息,''' from log import get_logger import json,os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''數據庫文件相對路徑''' user_dic = BASE_DIR + r"/db/user_data" creditcard_dic = BASE_DIR + r"/db/creditcard_data" creditcard_record = BASE_DIR + r"/db/creditcard_record" user_Water = BASE_DIR + r"/log/water_record" logger = get_logger() #日志實例化對象 '''信用卡信息''' def creditcard_data(): while True: with open(creditcard_dic,"r") as f: creditcard_data = json.loads(f.read()) choice = input("請輸入要查看信息的信用卡賬號 '6位數字' :").strip() if choice in creditcard_data.keys(): print("我的信用卡信息".center(50,"-")) print("持卡人:\t[ %s ]\n卡號:\t[ %s ]\n額度:\t[ ¥%s ]\n可用額度:\t[ ¥%s ]\n取現額度:\t[ ¥%s ]" %(creditcard_data[choice]["personinfo"], choice, creditcard_data[choice]["deflimit"], creditcard_data[choice]["limit"], creditcard_data[choice]["limitcash"])) else: print("您輸入的信用卡,不存在。") choice = input("返回輸入’q':") if choice == "q": break '''查看信用卡流水''' def cat_cred_record(): while True: choice = input("請輸入要查看流水記錄的信用卡賬號:").strip() with open(creditcard_dic) as f1: cred_record = json.loads(f1.read()) if choice: if choice in cred_record.keys(): print("信用卡 [ %s ] 流水記錄".center(50, "-") % (choice)) with open(user_Water) as f: for i in f: if choice in i: #print("\33[31;0m信用卡 [ %s ] 還沒有進行過消費,去商城買點東西吧\33[0m\n" % (choice)) print(i.strip()) else: print("您輸入的信用卡 [ %s ] 不存在"%(choice)) else: print("您輸入的信用卡 [ %s ] 不存在"%(choice)) choice = input("返回 'q':") if choice == "q": break '''信用卡還款''' def repayment(): while True: print(" 還款 ".center(40, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) choice = input("請輸入還款的信用卡賬號, 返回'q' :").strip() if choice == 'q': break if choice in creditcard_data: money = input("請輸入還款金額:").strip() pwd = input("請輸入還款密碼 :").strip() if pwd == creditcard_data[choice]["password"]: if money.isdigit(): money = int(money) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) limit = creditcard_data[choice]["limit"] limitcash = creditcard_data[choice]["limitcash"] limit += money limitcash += (money//2) creditcard_data[choice]["limit"]=limit creditcard_data[choice]["limitcash"] = limitcash dic = json.dumps(creditcard_data) rapayment_data = [str(choice), "信用卡還款", str(money)] msg = "---".join(rapayment_data) f.seek(0) f.truncate(0) f.write(dic) f.flush() logger.debug(msg) #日志模塊 print("信用卡 [ %s ] 還款金額 [ ¥%s ] 還款成功" % (choice, money)) print("信用卡\t[ %s ]\n可用額度:\t[ ¥%s ]\n取現額度:\t[ ¥%s ] " %(choice, creditcard_data[choice]["limit"], creditcard_data[choice]["limitcash"])) else: print("輸入金額格式有誤") else: print("密碼輸入錯誤") else: print("您輸入的信用卡不存在") '''信用卡取現''' def takenow(): while True: print(" 取現 ".center(40, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) choice = input("請輸入取現的信用卡賬號, 返回'q' :").strip() if choice == 'q': break if choice in creditcard_data: #print(creditcard_data) limit = creditcard_data[choice]["limit"] limitcash = creditcard_data[choice]["limitcash"] takenow = limit // 2 print("信用卡卡號:\t[ %s ]\n信用卡額度:\t[ ¥ %s ]\n取現額度:\t[ ¥ %s ]" % (choice,limit,takenow)) if limit >= limitcash: print("可取現金額為:\t [ ¥%s ]\n" % (limitcash)) cash = input("請輸入要取現的金額,收取%5手續費 :").strip() if cash.isdigit(): cash = int(cash) if cash <= limitcash: if cash > 0 : password = input("請輸入信用卡[ %s ] 的密碼 :" % (choice)).strip() if password and password == creditcard_data[choice]["password"]: limitcash = int(limitcash - (cash * 0.05 + cash)) limit = int(limit - (cash * 0.05 + cash)) creditcard_data[choice]["limit"] = limit creditcard_data[choice]["limitcash"] = limitcash f.seek(0) f.truncate(0) dic = json.dumps(creditcard_data) f.write(dic) takenow_data = [str(choice),"信用卡取現",str(cash),"手續費",str(int(cash*0.05))] msg = "---".join(takenow_data) logger.debug(msg) print("取現成功".center(40,"-")) print("取現金額:\t[%s]\n手續費:\t[%s]" % (cash, cash * 0.05)) else: print("密碼輸入錯誤\n") else: print("金額不能為0") else: print("您的取現金額已經超出取現額度了。") else: print("您的信用額度已經小於取現額度,不能取現了") else: print("您輸入的信用卡賬號 [ %s ] 錯誤"%(choice)) '''信用卡轉賬''' def transfer(): while True: print(" 轉賬 ".center(40, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) choice = input("請輸入信用卡賬號, 返回'q' :").strip() if choice == 'q': break if choice in creditcard_data: current_limit = creditcard_data[choice]["limit"] transfer_account = input("請輸入轉賬賬號:").strip() if transfer_account.isdigit(): #print("----------") if len(transfer_account) == 6 : if transfer_account in creditcard_data.keys(): money = input("請輸入轉賬金額:").strip() if money.isdigit(): money = int(money) creditcard_pwd = input("請輸入信用卡賬號密碼:") if creditcard_pwd == creditcard_data[choice]["password"]: if money <= current_limit: creditcard_data[choice]["limit"] -= money creditcard_data[choice]["limitcash"] -= money//2 creditcard_data[transfer_account]["limit"] += money creditcard_data[transfer_account]["limitcash"] += money//2 print("轉賬成功".center(40,"-")) print("轉賬卡號:\t[ %s ]\n轉賬金額:\t[ ¥%s ]"%(transfer_account,money)) print("信用卡:\t[ %s ]\t可用額度還剩:\t[ ¥%s ]\n"%(creditcard_data[choice]["creditcard"], creditcard_data[choice]["limit"])) transfer_data = [str(choice), "信用卡轉賬", str(money)] msg = "---".join(transfer_data) logger.debug(msg) f.seek(0) f.truncate(0) dic = json.dumps(creditcard_data) f.write(dic) else: print("轉賬金額不能超過信用額度") else: print("密碼輸入錯誤") else: print("請輸入數字的金額") else: print("您輸入的卡號不存在") else: print("您輸入的卡號不存在") else: print("請輸入正確的卡號") else: print("您輸入的信用卡不存在") '''凍結信用卡''' def lock_creditcard(): while True: print("凍結信用卡".center(50, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) for key in creditcard_data: if creditcard_data[key]["locked"] == False: print("信用卡 [ %s ]\t\t凍結狀態:[未凍結]" % (key)) else: print("信用卡 [ %s ]\t\t凍結狀態:[已凍結]" % (key)) choice = input("是否進行信用卡凍結 : 確定 'y' 返回 'q' :").strip() if choice == "q":break if choice == "y": lock_creditcard = input("請輸入要凍結的信用卡卡號:").strip() if lock_creditcard in creditcard_data.keys(): if creditcard_data[lock_creditcard]["locked"] == False: creditcard_data[lock_creditcard]["locked"] = True dic = json.dumps(creditcard_data) f.seek(0) f.truncate(0) f.write(dic) print("信用卡 %s 凍結成功\n" % (lock_creditcard)) else: print("信用卡 %s 已經被凍結\n" % (lock_creditcard)) else: print("信用卡 %s 不存在\n" %(lock_creditcard)) '''解除凍結信用卡''' def unlock_creditcard(): while True: print("解凍結信用卡".center(50, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) for key in creditcard_data: if creditcard_data[key]["locked"] == False: print("信用卡 [ %s ]\t\t凍結狀態:[未凍結]" % (key)) else: print("信用卡 [ %s ]\t\t凍結狀態:[已凍結]" % (key)) choice = input("是否進行解除信用卡凍結 : 確定 'y' 返回 'q' :").strip() if choice == "q":break if choice == "y": lock_creditcard = input("請輸入要凍結的信用卡卡號:").strip() if lock_creditcard in creditcard_data.keys(): if creditcard_data[lock_creditcard]["locked"] == True: creditcard_data[lock_creditcard]["locked"] = False dic = json.dumps(creditcard_data) f.seek(0) f.truncate(0) f.write(dic) print("信用卡 %s 解除凍結成功\n" % (lock_creditcard)) else: print("信用卡 %s 已經解除凍結\n" % (lock_creditcard)) else: print("信用卡 %s 不存在\n" %(lock_creditcard)) '''申請信用卡''' def new_creditcard(limit=15000,locked=False): while True: print("申請信用卡".center(50, "-")) with open(creditcard_dic, "r+") as f: creditcard_data = json.loads(f.read()) for key in creditcard_data: print("系統已有信用卡 【%s】 \t持卡人 【%s】" % (key,creditcard_data[key]["personinfo"])) choice = input("\n\33[34;0m是否申請新的信用卡 確定'y' 返回'q'\33[0m:").strip() if choice == "q":break if choice == "y": creditcard = input("\33[34;0m輸入要申請的信用卡卡號(6位數字):\33[0m").strip() if creditcard not in creditcard_data.keys(): if creditcard.isdigit() and len(creditcard) == 6: password = input("\33[34;0m請輸入申請的信用卡密碼:\33[0m").strip() if len(password) > 0: personinfo = input("\33[34;0m請輸入信用卡申請人:\33[0m").strip() if len(personinfo) > 0: creditcard_data[creditcard] = {"creditcard":creditcard, "password":password, "personinfo":personinfo, "limit":limit,"limitcash":limit//2,"locked":locked,"deflimit":limit,} dict = json.dumps(creditcard_data) f.seek(0) f.truncate(0) f.write(dict) print("信用卡:\t[ %s ] 申請成功\n持卡人:\t[ %s ]\n額度:\t[ ¥%s ]\n取現額度:\t[ ¥%s ]"%(creditcard, personinfo, limit, creditcard_data[creditcard]["limitcash"])) else: print("信用卡申請人不能為空\n") else: print("輸入的密碼不正確\n") else: print("信用卡 %s 卡號不符合規范\n" % (creditcard)) else: print("信用卡 %s 已經存在\n" % (creditcard)) '''信用卡綁定''' def link_creditcard(): while True: print("\33[32;0m修改信用卡綁定\33[0m".center(40, "-")) with open(user_dic, "r+") as f: user_data = json.loads(f.read()) user_name = input("請輸入綁定信用卡的用戶名 返回 'q' :").strip() if user_name == "q":break if user_name in user_data.keys(): creditcard = user_data[user_name]["creditcard"] if creditcard == 0 : print("當前賬號: \t%s"%(user_name)) print("信用卡綁定:\33[31;0m未綁定\33[0m\n") else: print("當前賬號: \t%s" %(user_name)) print("綁定的信用卡: %s\n"%(creditcard)) choice = input("\33[34;0m是否要修改信用卡綁定 確定 'y' 返回'q' \33[0m:") if choice == "q":break if choice == "y": creditcard_new = input("\33[34;0m輸入新的信用卡卡號(6位數字)\33[0m:").strip() if creditcard_new.isdigit() and len(creditcard_new) ==6: with open(creditcard_dic, "r+") as f1: creditcard_data = json.loads(f1.read()) if creditcard_new in creditcard_data.keys(): user_data[user_name]["creditcard"]=creditcard_new dict = json.dumps(user_data) f.seek(0) f.truncate(0) f.write(dict) print("\33[31;1m信用卡綁定成功\33[0m\n") else: print("\33[31;0m輸入信用卡卡號不存在(未發行)\33[0m\n") else: print("\33[31;0m輸入信用卡格式錯誤\33[0m\n") else: print("請選擇 ’y‘ 或 ’q‘ ") else: print("您輸入的用戶 [ %s ] 不存在 ")
主邏輯模塊,調度各種模塊

'''主邏輯交互程序''' from auth import * from user import * from creditcard import * from shopping import * '''主頁面列表''' def main_list(): msg = [" ATM ", "購物商城", "管理系統", "退出程序 輸入 q",] index = 0 for i in msg: print("\t\t\t\t",index+1,"\t ", i) index += 1 '''ATM頁面列表''' def atm_list(): msg = ["信用卡信息", "信用卡取現", "信用卡轉賬", "信用卡還款", "申請信用卡", "信用卡綁定", "信用卡流水", "返回上一層 輸入 q", "退出程序 輸入 exit"] index = 0 for i in msg: print("\t\t\t\t", index + 1, "\t ", i) index += 1 '''購物車頁面列表''' def shopp_list(): msg = ["購物", "購物車", "購物結算", "購物記錄", "清空購物車", "返回上一層 輸入 q", "退出程序 輸入 exit"] index = 0 for i in msg: print("\t\t\t\t", index + 1, "\t ", i) index += 1 '''管理員頁面列表''' def admin_list(): msg = ["凍結信用卡", "解凍信用卡", "創建用戶", "鎖定用戶", "解鎖用戶", "返回上一層 輸入 q", "退出程序 輸入 exit"] index = 0 for i in msg: print("\t\t\t\t", index + 1, "\t ", i) index += 1 '''主函數''' def main(): print("購物商城ATM系統".center(40, "-")) lock() # 三次鎖定模塊 while True: print("歡迎來到購物商城ATM系統".center(40, "-")) print(" \t\t\t\t ID\t\t信息") main_list() choice = input("請選擇 ID :").strip() if choice == "q": print(" bye bye ".center(50, "-")) exit() if choice.isdigit(): choice = int(choice) if choice >= 1 and choice <= 4 : if choice == "q":break while True: if choice == 1: print("歡迎來到信用卡中心".center(50, "-")) print(" \t\t\t\t ID\t\tATM信息") atm_list() #信用卡列表 atm_choice = input("請選擇 ATM ID :").strip() if atm_choice == "q": break if atm_choice == "exit":exit() if atm_choice.isdigit(): atm_choice = int(atm_choice) if atm_choice >= 1 and atm_choice <= 7: while True: if atm_choice == 1: creditcard_data() #信用卡信息模塊 break elif atm_choice == 2: creditcard_auth() # 信用卡認證模塊 takenow() #信用卡取現模塊 break elif atm_choice == 3: creditcard_auth() # 信用卡認證模塊 transfer() #信用卡轉賬模塊 break elif atm_choice == 4: creditcard_auth() # 信用卡認證模塊 repayment() #信用卡還款模塊 break elif atm_choice == 5: new_creditcard(limit=15000, locked=False) #申請信用卡模塊 break elif atm_choice == 6: link_creditcard() #用戶綁定信用卡模塊 break elif atm_choice ==7: cat_cred_record() #查看信用卡流水模塊 break else: print("請輸入正確的 ID ") else: print("請輸入正確的 ID ") elif choice == 2: print("歡迎來到購物中心".center(50, "-")) print(" \t\t\t\t ID\t\t商城信息") shopp_list() # 商城列表 shopp_choice = input("請選擇 商城 ID :").strip() if shopp_choice == "q":break if shopp_choice == "exit":exit() if shopp_choice.isdigit(): shopp_choice = int(shopp_choice) if shopp_choice >= 1 and shopp_choice <= 5: while True: if shopp_choice == 1: shopping_mall() #購物商城模塊 break elif shopp_choice == 2: Shopping_car() #購物車模塊 break elif shopp_choice == 3: shopping_pay() #購物結算模塊 break elif shopp_choice == 4: cat_shopp_record() #查看購物記錄模塊 break elif shopp_choice == 5: del_shopping_car() #清空購物車模塊 break else: print("請輸入正確的 ID ") else: print("請輸入正確的 ID ") elif choice == 3: print("歡迎來到管理中心".center(50, "-")) print(" \t\t\t\t ID\t\t操作信息") admin_list() # 管理中心列表 admin_choice = input("請選擇 信息 ID :").strip() if admin_choice == "q": break if admin_choice == "exit":exit() if admin_choice.isdigit(): admin_choice = int(admin_choice) if admin_choice >= 1 and admin_choice <= 5: while True: if admin_choice == 1: admin_auth() #管理員用戶驗證模塊 lock_creditcard() #凍結信用卡模塊 break elif admin_choice == 2: admin_auth() # 管理員用戶驗證模塊 unlock_creditcard() #解凍信用卡模塊 break elif admin_choice == 3: admin_auth() # 管理員用戶驗證模塊 # 創建用戶模塊 new_user(address="None", locked=False, creditcard=False) break elif admin_choice == 4: admin_auth() # 管理員用戶驗證模塊 lock_user() #鎖定用戶模塊 break elif admin_choice == 5: admin_auth() # 管理員用戶驗證模塊 unlock_user() #解鎖用戶模塊 break elif choice == 4: exit()
db數據庫目錄,都是txt格式的
黑名單文件:

{"zhangsan": 1, "zhaosi": 0, "wangwu": 3}
信用卡信息文件:

{"123456": {"creditcard": "123456", "password": "123", "personinfo": "\u5510\u50e7", "limit": 3950, "limitcash": 1450, "locked": false, "deflimit": 15000}, "222222": {"creditcard": "222222", "password": "123", "personinfo": "\u732a\u516b\u6212", "limit": 14879, "limitcash": 7439, "locked": true, "deflimit": 15000}, "111111": {"creditcard": "111111", "password": "123", "personinfo": "\u767d\u9f99\u9a6c", "limit": 14006, "limitcash": 7002, "locked": true, "deflimit": 15000}}
購物車文件:

[["iPhone", "3999"], ["iPhone", "3999"]]
商品列表文件:

iPhone 3999 iPad 2199 bike 1999 dell 1199 l-TV 999 Book 99
用戶信息文件:

{"zhaosi": {"status": true, "username": "zhaosi", "password": "123", "creditcard": "111111", "address": "None", "locked": false}, "zhangsan": {"username": "zhangsan", "password": "123", "creditcard": "222222", "address": "None", "locked": false, "status": true}, "wangwu": {"username": "wangwu", "password": "123", "creditcard": false, "address": "None", "status": false, "locked": false}}
log目錄:
記錄用戶購物信息,信用卡消費,轉賬等流水信息:

2017-05-10 23:43:12,840 --- 123456---信用卡還款---1000 2017-05-10 23:43:12,840 --- 123456---信用卡還款---1000 2017-05-10 23:43:46,817 --- 123456---信用卡轉賬---10000 2017-05-10 23:43:46,817 --- 123456---信用卡轉賬---10000 2017-05-10 23:48:17,708 --- 商品---iPhone---價格---3999 2017-05-10 23:48:17,708 --- 商品---iPhone---價格---3999 2017-05-10 23:48:43,242 --- zhaosi---111111---信用卡結賬---7998 2017-05-10 23:48:43,242 --- zhaosi---111111---信用卡結賬---7998