ATM + 购物商城程序
生活随笔
收集整理的這篇文章主要介紹了
ATM + 购物商城程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
模擬實現(xiàn)一個ATM + 購物商城程序
額度 15000或自定義
實現(xiàn)購物商城,買東西加入 購物車,調(diào)用信用卡接口結(jié)賬
可以提現(xiàn),手續(xù)費5%
每月22號出賬單,每月10號為還款日,過期未還,按欠款總額 萬分之5 每日計息
支持多賬戶登錄
支持賬戶間轉(zhuǎn)賬
記錄每月日常消費流水
提供還款接口
ATM記錄操作日志?
提供管理接口,包括添加賬戶、用戶額度,凍結(jié)賬戶等。。。
用戶認證用裝飾器
# import time
import datetime
# time.time() # 當前時間戳
# datetime.datetime.now() # 本地之間
# 數(shù)據(jù)處理=====================================
# 全部用戶數(shù)據(jù)
user_data = []
# 加載數(shù)據(jù)到內(nèi)存
def load_data():
f = open("DB/user_db.txt","rt",encoding="utf-8")
for line in f:
line = line.strip("\n")
if line == "\n":continue
ls = line.split("|")
usr_dic = {"name":ls[0],
"pwd":ls[1],
"max": ls[2],
"money": ls[3],
"lock": ls[4]}
user_data.append(usr_dic)
f.close()
# 根據(jù)賬戶名獲取一個用戶
def get_usr(name):
for usr in user_data:
if usr["name"] == name:
return usr
# 將內(nèi)存中的數(shù)據(jù)持久存儲到文件中
def save_to_file():
f = open("DB/user_db.txt","wt",encoding="utf-8")
for usr in user_data:
text = "|".join(usr.values())
text += "\n"
f.write(text)
f.close()
# 數(shù)據(jù)處理=====================================
# 登錄驗證裝飾器(閉包函數(shù)) (再不改變源代碼的調(diào)用方式基礎(chǔ)上 為這個函數(shù)添加新功能)
def login_auth(func):
def wrapper(*args,**kwargs):
# 驗證是否登陸過了
if current_usr == None:
print("請先登錄!")
atm_login()
if current_usr:
return func(*args,**kwargs)
else:
return func(*args,**kwargs)
return wrapper
# 購物中心========================================
# 商品信息
products = [{"name":"挨糞叉","price":6888},
{"name":"錘子TNT","price":10888},
{"name":"小米mix2","price":2888}]
# 購物車
car = {}
# 購物
def shopping():
while True:
# 打印出所有商品信息
count = 1
for p in products:
print("序號:%-3s 名稱:%-10s 價格:%-6s" % (count,p["name"],p["price"]))
count += 1
select = input("親輸入商品的序號q退出:\n")
if select == "q":
return
if select.isdigit() and int(select) >= 1 and int(select) <= len(products):
pd = products[int(select)-1]
print("%s已成功加入購物車!" % pd["name"])
# 判斷商品是否已經(jīng)存在于購物車
if pd["name"] in car:
car[pd["name"]]["count"] += 1
else:
car[pd["name"]] = {"price":pd["price"],"count":1}
else:
print("輸入有誤請重新輸入!")
# 查看購物車
def show_cars():
if not car:
s = input("你的購物車是空的! 你要買點什么嗎? y/n")
if s == "y":
shopping()
return
else:
return
print("您的購物車信息:")
for p in car:
print("名稱:%-10s 價格:%-8s 數(shù)量:%-3s 總價:%-10s" % (p,
car[p]["price"],
car[p]["count"],
car[p]["price"] * car[p]["count"]))
select = input("輸入y調(diào)整商品數(shù)量!(數(shù)量為0則刪除商品!) 輸入其他退出!")
if select == "y":
modify_count()
# 調(diào)整購物車中的商品數(shù)量
def modify_count():
while True:
name = input("請輸入商品名稱q退出:\n")
if name == "q":
return
if name not in car:
print("輸入不正確請重試!")
continue
while True:
count = input("輸入調(diào)整后數(shù)量:\n")
if not count.isdigit():
print("數(shù)量不正確 必須是整數(shù)!")
continue
count = int(count)
if count == 0:
car.pop(name)
print("%s已刪除" % name)
return
else:
car[name]["count"] = count
print("修改成功!")
return
# 結(jié)算
@login_auth
def pay_cars():
# 計算商品總價格
sum = 0
for p in car:
sum += car[p]["price"] * car[p]["count"]
print("您的訂單總價為:%s元" % sum)
if paymoney(sum):
print("剁手成功! 回家等著吧!")
clear_cars()
# 清空購物車
def clear_cars():
car.clear()
print("購物車已清空!")
def shop_center():
shopfuncs = {"1":shopping,"2":show_cars,"3":pay_cars,"4":clear_cars}
while True:
print("""
1.購物
2.查看購物車
3.結(jié)算
4.清空
q.退出""")
index = input("請選擇:\n").strip()
if index == "q":
return
if index in shopfuncs:
shopfuncs[index]()
else:
print("輸入不正確!請重試!")
# 購物中心========================================
# ATM====================================================
# 用于保存登錄成功的用戶信息
current_usr = None
# 日志裝飾器.
# 日志文件
atmlog_file = open("DB/log.txt","at",encoding="utf-8")
# 用戶流水文件
moneyflow = None
def atm_logger(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
# 記錄日志
atmlog_file.write("[%s] user:%s function:%s\n" % (datetime.datetime.now(),current_usr["name"],func.__name__))
atmlog_file.flush() # 立即刷出緩存區(qū)的數(shù)據(jù)到硬盤 如果每次write都操作一次硬盤
# 效率非常低 所以python內(nèi)置了緩沖區(qū) 只有當緩沖區(qū)滿了才會寫入硬盤 一次來降低硬盤操作次數(shù)
return wrapper
# 登錄
def atm_login():
while True:
name = input("請輸入賬戶名:(q退出)\n").strip()
# 手動記錄日志 因為用戶還沒有登陸
atmlog_file.write("[%s] user:%s function:%s\n" % (datetime.datetime.now(), name, atm_login.__name__))
atmlog_file.flush()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
usr = get_usr(name)
if not usr:
print("用戶名不存在!")
continue
pwd = input("請輸入密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if name == usr["name"] and pwd == usr["pwd"]:
print("登錄成功")
global current_usr,moneyflow
# 打開流水文件
moneyflow = open("DB/%s.txt" % name,"at",encoding="utf-8")
# 記錄當前登錄用戶信息
current_usr = usr
return
else:
print("賬戶名或密碼不正確!請重試!")
# 提現(xiàn)
@login_auth
@atm_logger
def withdraw():
while True:
print("當前余額:%s元" % current_usr["money"])
money = input("請輸入提款金額:(整數(shù) q退出)\n")
if money == "q":return
if not money.isdigit():
print("輸入有誤!請重試!")
continue
money = int(money)
# 手續(xù)費
opt = money * 0.05
print("需要手續(xù)費%s元" % opt)
usr_moeny = float(current_usr["money"])
if money+opt > usr_moeny:
print("余額不足!很尷尬")
continue
current_usr["money"] = str(usr_moeny - money - opt)
save_to_file()
print("請?zhí)崛∧愕拟n票!")
moneyflow.write("[%s] 賬戶:%s 提現(xiàn)%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
current_usr["money"]))
moneyflow.flush()
return
# 轉(zhuǎn)賬
@login_auth
@atm_logger
def transfer():
while True:
account = input("請輸入對方的賬戶名:\n")
to_usr = get_usr(account)
if not to_usr:
print("賬戶不存在 請重新輸入!")
continue
print("當前余額:%s元" % current_usr["money"])
money = input("請輸入轉(zhuǎn)賬金額:(整數(shù) q退出)\n")
if money == "q": return
money = str_to_num(money)
if not money:
print("輸入有誤!請重試!")
continue
usr_moeny = float(current_usr["money"])
if money > usr_moeny:
print("余額不足!很尷尬")
continue
# 原始賬戶減去
current_usr["money"] = str(usr_moeny - money)
# 目標賬戶加上
to_usr["money"] = str(float(to_usr["money"]) + money)
save_to_file()
print("轉(zhuǎn)賬成功!")
moneyflow.write("[%s] 賬戶:%s 轉(zhuǎn)賬%s元 給%s 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
account,
current_usr["money"]))
moneyflow.flush()
return
# 將字符串轉(zhuǎn)成數(shù)字 "11212101.s"
def str_to_num(text):
if text.isdigit():
return int(text)
if "." in text:
ts = text.split(".")
if len(ts) == 2:
if ts[0].isdigit() and ts[1].isdigit():
return float(text)
# 還款
@login_auth
@atm_logger
def repayment():
print("您當前欠款:%s" % (str_to_num(current_usr["max"]) - str_to_num(current_usr["money"])))
while True:
print("repayment")
money = input("請輸入還款金額!q退出:\n")
if money == "q":return
money = str_to_num(money)
if not money:
print("輸入有誤 請重試!")
continue
current_usr["money"] = str(str_to_num(current_usr["money"]) + money)
save_to_file()
print("還款成功!")
moneyflow.write("[%s] 賬戶:%s 還款%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
current_usr["money"]))
moneyflow.flush()
return
# 結(jié)算 需要傳入訂單總價格
@login_auth
@atm_logger
def paymoney(price):
if price > str_to_num(current_usr["money"]):
print("額度不足!又尷尬了")
else:
current_usr["money"] = str(str_to_num(current_usr["money"]) - price)
save_to_file()
print("結(jié)算成功!")
moneyflow.write("[%s] 賬戶:%s 購物消費%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
price,
current_usr["money"]))
moneyflow.flush()
return True
def atm():
atm_funcs = {"1": atm_login, "2": withdraw, "3": transfer, "4": repayment}
while True:
print("""
1.登錄
2.提現(xiàn)
3.轉(zhuǎn)賬
4.還款
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
# 清空當前用戶信息 關(guān)閉流水文件
global current_usr
if current_usr:
current_usr = None
moneyflow.close()
return
if index in atm_funcs:
atm_funcs[index]()
else:
print("輸入有誤請重試!")
# ATM====================================================
# 管理員接口=============================================
# 顯示所有的用戶信息 并從中選擇一個用戶
def show_users_and_select():
while True:
for usr in user_data:
print("%-10s %-10s %-10s %-2s" % (usr["name"],usr["max"],usr["money"],usr["lock"]))
select = input("請選擇一個用戶(輸入用戶名:q退出)\n")
if select == "q":
return
for u in user_data:
if select == u["name"]:
return u
else:
print("輸入不正確 請重新輸入!")
# 管理員登錄
def admin_login():
while True:
name = input("請輸入管理員賬戶名:(q退出)\n").strip()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
pwd = input("請輸入管理員密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if name == "admin" and pwd == "123":
print("登錄成功")
return True
else:
print("賬戶名或密碼不正確!請重試!")
#添加賬戶
def create_user():
while True:
name = input("請輸入賬戶名:(q退出)\n").strip()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
# 獲取用戶 通過賬戶名
usr = get_usr(name)
if usr:
print("用戶名已經(jīng)存在,請重試!")
continue
pwd = input("請輸入密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if pwd != input("請再次輸入密碼:").strip():
print("兩次密碼不相同請重試!")
continue
# 將用戶輸入的信息打包成字典 存到列表中
new_usr = {"name":name,"pwd":pwd,"max":"15000","money":"500","lock":"0"}
user_data.append(new_usr)
print("創(chuàng)建成功!")
# 持久化存儲......
save_to_file()
return
#調(diào)整額度
def modify_amount():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
return
while True:
# 輸入新的額度
new_max = input("請輸入新的額度:(q退出)\n").strip()
if new_max == "q":
return
if not new_max.isdigit():
print("輸入不正確 請重新輸入!")
continue
usr["max"] = new_max
save_to_file()
print("修改成功!")
return
#凍結(jié)賬戶
def lock_user():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
print("取消操作!")
return
usr["lock"] = "1"
save_to_file()
print("凍結(jié)成功!")
#解凍賬戶
def unlock_user():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
print("取消操作!")
return
usr["lock"] = "0"
save_to_file()
print("解凍成功!")
# 管理員接口界面
def admin_view():
# 調(diào)用登錄功能驗證是否有權(quán)限執(zhí)行操作
if not admin_login():
return
admin_funcs = {"1":create_user,"2":modify_amount,"3":lock_user,"4":unlock_user}
while True:
print("""
1.添加賬戶
2.修改額度
3.凍結(jié)賬戶
4.解凍賬戶
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
return
if index in admin_funcs:
admin_funcs[index]()
else:
print("輸入有誤請重試!")
# 管理員接口=============================================
# 程序的入口函數(shù)
def run():
print("welcome to oldboy ATM system!")
# 將函數(shù)裝到字典中 用序號作為key 方便判斷
funcs = {"1": shop_center, "2": atm, "3": admin_view}
while True:
print("""
1.購物中心
2.ATM
3.管理員接口
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
print("下次再見!")
return
if index in funcs:
funcs[index]()
else:
print("輸入有誤請重試!")
# 啟動系統(tǒng)
load_data()
run()
atmlog_file.close()
# print(user_data)
# print(str_to_num("100.s"))
# import time
import datetime
# time.time() # 當前時間戳
# datetime.datetime.now() # 本地之間
# 數(shù)據(jù)處理=====================================
# 全部用戶數(shù)據(jù)
user_data = []
# 加載數(shù)據(jù)到內(nèi)存
def load_data():
f = open("DB/user_db.txt","rt",encoding="utf-8")
for line in f:
line = line.strip("\n")
if line == "\n":continue
ls = line.split("|")
usr_dic = {"name":ls[0],
"pwd":ls[1],
"max": ls[2],
"money": ls[3],
"lock": ls[4]}
user_data.append(usr_dic)
f.close()
# 根據(jù)賬戶名獲取一個用戶
def get_usr(name):
for usr in user_data:
if usr["name"] == name:
return usr
# 將內(nèi)存中的數(shù)據(jù)持久存儲到文件中
def save_to_file():
f = open("DB/user_db.txt","wt",encoding="utf-8")
for usr in user_data:
text = "|".join(usr.values())
text += "\n"
f.write(text)
f.close()
# 數(shù)據(jù)處理=====================================
# 登錄驗證裝飾器(閉包函數(shù)) (再不改變源代碼的調(diào)用方式基礎(chǔ)上 為這個函數(shù)添加新功能)
def login_auth(func):
def wrapper(*args,**kwargs):
# 驗證是否登陸過了
if current_usr == None:
print("請先登錄!")
atm_login()
if current_usr:
return func(*args,**kwargs)
else:
return func(*args,**kwargs)
return wrapper
# 購物中心========================================
# 商品信息
products = [{"name":"挨糞叉","price":6888},
{"name":"錘子TNT","price":10888},
{"name":"小米mix2","price":2888}]
# 購物車
car = {}
# 購物
def shopping():
while True:
# 打印出所有商品信息
count = 1
for p in products:
print("序號:%-3s 名稱:%-10s 價格:%-6s" % (count,p["name"],p["price"]))
count += 1
select = input("親輸入商品的序號q退出:\n")
if select == "q":
return
if select.isdigit() and int(select) >= 1 and int(select) <= len(products):
pd = products[int(select)-1]
print("%s已成功加入購物車!" % pd["name"])
# 判斷商品是否已經(jīng)存在于購物車
if pd["name"] in car:
car[pd["name"]]["count"] += 1
else:
car[pd["name"]] = {"price":pd["price"],"count":1}
else:
print("輸入有誤請重新輸入!")
# 查看購物車
def show_cars():
if not car:
s = input("你的購物車是空的! 你要買點什么嗎? y/n")
if s == "y":
shopping()
return
else:
return
print("您的購物車信息:")
for p in car:
print("名稱:%-10s 價格:%-8s 數(shù)量:%-3s 總價:%-10s" % (p,
car[p]["price"],
car[p]["count"],
car[p]["price"] * car[p]["count"]))
select = input("輸入y調(diào)整商品數(shù)量!(數(shù)量為0則刪除商品!) 輸入其他退出!")
if select == "y":
modify_count()
# 調(diào)整購物車中的商品數(shù)量
def modify_count():
while True:
name = input("請輸入商品名稱q退出:\n")
if name == "q":
return
if name not in car:
print("輸入不正確請重試!")
continue
while True:
count = input("輸入調(diào)整后數(shù)量:\n")
if not count.isdigit():
print("數(shù)量不正確 必須是整數(shù)!")
continue
count = int(count)
if count == 0:
car.pop(name)
print("%s已刪除" % name)
return
else:
car[name]["count"] = count
print("修改成功!")
return
# 結(jié)算
@login_auth
def pay_cars():
# 計算商品總價格
sum = 0
for p in car:
sum += car[p]["price"] * car[p]["count"]
print("您的訂單總價為:%s元" % sum)
if paymoney(sum):
print("剁手成功! 回家等著吧!")
clear_cars()
# 清空購物車
def clear_cars():
car.clear()
print("購物車已清空!")
def shop_center():
shopfuncs = {"1":shopping,"2":show_cars,"3":pay_cars,"4":clear_cars}
while True:
print("""
1.購物
2.查看購物車
3.結(jié)算
4.清空
q.退出""")
index = input("請選擇:\n").strip()
if index == "q":
return
if index in shopfuncs:
shopfuncs[index]()
else:
print("輸入不正確!請重試!")
# 購物中心========================================
# ATM====================================================
# 用于保存登錄成功的用戶信息
current_usr = None
# 日志裝飾器.
# 日志文件
atmlog_file = open("DB/log.txt","at",encoding="utf-8")
# 用戶流水文件
moneyflow = None
def atm_logger(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
# 記錄日志
atmlog_file.write("[%s] user:%s function:%s\n" % (datetime.datetime.now(),current_usr["name"],func.__name__))
atmlog_file.flush() # 立即刷出緩存區(qū)的數(shù)據(jù)到硬盤 如果每次write都操作一次硬盤
# 效率非常低 所以python內(nèi)置了緩沖區(qū) 只有當緩沖區(qū)滿了才會寫入硬盤 一次來降低硬盤操作次數(shù)
return wrapper
# 登錄
def atm_login():
while True:
name = input("請輸入賬戶名:(q退出)\n").strip()
# 手動記錄日志 因為用戶還沒有登陸
atmlog_file.write("[%s] user:%s function:%s\n" % (datetime.datetime.now(), name, atm_login.__name__))
atmlog_file.flush()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
usr = get_usr(name)
if not usr:
print("用戶名不存在!")
continue
pwd = input("請輸入密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if name == usr["name"] and pwd == usr["pwd"]:
print("登錄成功")
global current_usr,moneyflow
# 打開流水文件
moneyflow = open("DB/%s.txt" % name,"at",encoding="utf-8")
# 記錄當前登錄用戶信息
current_usr = usr
return
else:
print("賬戶名或密碼不正確!請重試!")
# 提現(xiàn)
@login_auth
@atm_logger
def withdraw():
while True:
print("當前余額:%s元" % current_usr["money"])
money = input("請輸入提款金額:(整數(shù) q退出)\n")
if money == "q":return
if not money.isdigit():
print("輸入有誤!請重試!")
continue
money = int(money)
# 手續(xù)費
opt = money * 0.05
print("需要手續(xù)費%s元" % opt)
usr_moeny = float(current_usr["money"])
if money+opt > usr_moeny:
print("余額不足!很尷尬")
continue
current_usr["money"] = str(usr_moeny - money - opt)
save_to_file()
print("請?zhí)崛∧愕拟n票!")
moneyflow.write("[%s] 賬戶:%s 提現(xiàn)%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
current_usr["money"]))
moneyflow.flush()
return
# 轉(zhuǎn)賬
@login_auth
@atm_logger
def transfer():
while True:
account = input("請輸入對方的賬戶名:\n")
to_usr = get_usr(account)
if not to_usr:
print("賬戶不存在 請重新輸入!")
continue
print("當前余額:%s元" % current_usr["money"])
money = input("請輸入轉(zhuǎn)賬金額:(整數(shù) q退出)\n")
if money == "q": return
money = str_to_num(money)
if not money:
print("輸入有誤!請重試!")
continue
usr_moeny = float(current_usr["money"])
if money > usr_moeny:
print("余額不足!很尷尬")
continue
# 原始賬戶減去
current_usr["money"] = str(usr_moeny - money)
# 目標賬戶加上
to_usr["money"] = str(float(to_usr["money"]) + money)
save_to_file()
print("轉(zhuǎn)賬成功!")
moneyflow.write("[%s] 賬戶:%s 轉(zhuǎn)賬%s元 給%s 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
account,
current_usr["money"]))
moneyflow.flush()
return
# 將字符串轉(zhuǎn)成數(shù)字 "11212101.s"
def str_to_num(text):
if text.isdigit():
return int(text)
if "." in text:
ts = text.split(".")
if len(ts) == 2:
if ts[0].isdigit() and ts[1].isdigit():
return float(text)
# 還款
@login_auth
@atm_logger
def repayment():
print("您當前欠款:%s" % (str_to_num(current_usr["max"]) - str_to_num(current_usr["money"])))
while True:
print("repayment")
money = input("請輸入還款金額!q退出:\n")
if money == "q":return
money = str_to_num(money)
if not money:
print("輸入有誤 請重試!")
continue
current_usr["money"] = str(str_to_num(current_usr["money"]) + money)
save_to_file()
print("還款成功!")
moneyflow.write("[%s] 賬戶:%s 還款%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
money,
current_usr["money"]))
moneyflow.flush()
return
# 結(jié)算 需要傳入訂單總價格
@login_auth
@atm_logger
def paymoney(price):
if price > str_to_num(current_usr["money"]):
print("額度不足!又尷尬了")
else:
current_usr["money"] = str(str_to_num(current_usr["money"]) - price)
save_to_file()
print("結(jié)算成功!")
moneyflow.write("[%s] 賬戶:%s 購物消費%s元 余額%s元\n" % (datetime.datetime.now(),
current_usr["name"],
price,
current_usr["money"]))
moneyflow.flush()
return True
def atm():
atm_funcs = {"1": atm_login, "2": withdraw, "3": transfer, "4": repayment}
while True:
print("""
1.登錄
2.提現(xiàn)
3.轉(zhuǎn)賬
4.還款
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
# 清空當前用戶信息 關(guān)閉流水文件
global current_usr
if current_usr:
current_usr = None
moneyflow.close()
return
if index in atm_funcs:
atm_funcs[index]()
else:
print("輸入有誤請重試!")
# ATM====================================================
# 管理員接口=============================================
# 顯示所有的用戶信息 并從中選擇一個用戶
def show_users_and_select():
while True:
for usr in user_data:
print("%-10s %-10s %-10s %-2s" % (usr["name"],usr["max"],usr["money"],usr["lock"]))
select = input("請選擇一個用戶(輸入用戶名:q退出)\n")
if select == "q":
return
for u in user_data:
if select == u["name"]:
return u
else:
print("輸入不正確 請重新輸入!")
# 管理員登錄
def admin_login():
while True:
name = input("請輸入管理員賬戶名:(q退出)\n").strip()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
pwd = input("請輸入管理員密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if name == "admin" and pwd == "123":
print("登錄成功")
return True
else:
print("賬戶名或密碼不正確!請重試!")
#添加賬戶
def create_user():
while True:
name = input("請輸入賬戶名:(q退出)\n").strip()
if name == "q":
return
if not name:
print("用戶名不能為空!")
continue
# 獲取用戶 通過賬戶名
usr = get_usr(name)
if usr:
print("用戶名已經(jīng)存在,請重試!")
continue
pwd = input("請輸入密碼:\n").strip()
if not pwd:
print("密碼不能為空!")
continue
if pwd != input("請再次輸入密碼:").strip():
print("兩次密碼不相同請重試!")
continue
# 將用戶輸入的信息打包成字典 存到列表中
new_usr = {"name":name,"pwd":pwd,"max":"15000","money":"500","lock":"0"}
user_data.append(new_usr)
print("創(chuàng)建成功!")
# 持久化存儲......
save_to_file()
return
#調(diào)整額度
def modify_amount():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
return
while True:
# 輸入新的額度
new_max = input("請輸入新的額度:(q退出)\n").strip()
if new_max == "q":
return
if not new_max.isdigit():
print("輸入不正確 請重新輸入!")
continue
usr["max"] = new_max
save_to_file()
print("修改成功!")
return
#凍結(jié)賬戶
def lock_user():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
print("取消操作!")
return
usr["lock"] = "1"
save_to_file()
print("凍結(jié)成功!")
#解凍賬戶
def unlock_user():
# 先選擇一個用戶
usr = show_users_and_select()
if not usr:
print("取消操作!")
return
usr["lock"] = "0"
save_to_file()
print("解凍成功!")
# 管理員接口界面
def admin_view():
# 調(diào)用登錄功能驗證是否有權(quán)限執(zhí)行操作
if not admin_login():
return
admin_funcs = {"1":create_user,"2":modify_amount,"3":lock_user,"4":unlock_user}
while True:
print("""
1.添加賬戶
2.修改額度
3.凍結(jié)賬戶
4.解凍賬戶
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
return
if index in admin_funcs:
admin_funcs[index]()
else:
print("輸入有誤請重試!")
# 管理員接口=============================================
# 程序的入口函數(shù)
def run():
print("welcome to oldboy ATM system!")
# 將函數(shù)裝到字典中 用序號作為key 方便判斷
funcs = {"1": shop_center, "2": atm, "3": admin_view}
while True:
print("""
1.購物中心
2.ATM
3.管理員接口
(q.退出!)""")
index = input("請選擇:\n")
if index == "q":
print("下次再見!")
return
if index in funcs:
funcs[index]()
else:
print("輸入有誤請重試!")
# 啟動系統(tǒng)
load_data()
run()
atmlog_file.close()
# print(user_data)
# print(str_to_num("100.s"))
# 只有程序時從當前文件開始運行時才進入if語句
# if __name__ == '__main__':
# 只有程序時從當前文件開始運行時才進入if語句
# if __name__ == '__main__':
轉(zhuǎn)載于:https://www.cnblogs.com/hua-zhong/p/9768905.html
總結(jié)
以上是生活随笔為你收集整理的ATM + 购物商城程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一天搞懂深度学习—学习笔记4(knowl
- 下一篇: git工作中常用命令