# 主程式運行 import time from guanli import GuanLi from atm import ATM from user import User def main(): guanli = GuanLi()# 創建一個管理對象 guanli.welcome() # 歡迎界... ...
# 主程式運行 import time from guanli import GuanLi from atm import ATM from user import User def main(): guanli = GuanLi()# 創建一個管理對象 guanli.welcome() # 歡迎界面 guanli.caozuo() # 管理員登陸驗證 userinfo = User.load_user() # 載入已有用戶信息 atm = ATM(userinfo=userinfo) # 創建操作對象 while True: guanli.operation() # 管理菜單的操作方法 option = (input("請輸入您的操作:")) # 等待用戶操作 in_quit = False # 定義一個變數,記錄是否退出操作 if option == "1": # print("開戶") atm.kaihu() elif option == "2": # print("查詢") atm.chaxun() elif option == "3": # print("取款") atm.qukuan() elif option == "4": # print("存款") atm.cunkuan() elif option == "5": # print("轉賬") atm.zhuanzhang() elif option == "6": # print("改密") atm.gaimi() elif option == "7": # print("鎖定") atm.suodin() elif option == "8": # print("解鎖") atm.jiesuo() elif option == "0": # print("銷戶") atm.xiaohu() elif option == "t": in_quit = True print("退出成功") break elif option == "9": print("查詢所有用戶信息") atm.show() else: print("操作有誤,請重新操作:") time.sleep(2)
# 設置一個管理類ps:代碼全程無英文,適合英語不好的新手看 import time from atm import ATM from user import User class GuanLi(object): admin = "1" passwd = "1" def welcome(self): print("*" * 30) print(" " * 5+"歡迎登陸使用銀行管理系統") print("*" * 30) def caozuo(self):# 判斷管理員賬號正確 count = 0 while True: inputAdmin = input("請輸入管理員賬號:") inputPasswd = input("請輸入管理員密碼:") if self.admin != inputAdmin or self.passwd != inputPasswd: print("賬號或密碼輸入有誤!!") count +=1 if count >= 3: print("錯誤次數已達上限,鎖定一小時,1小時後再試") break else: continue print("操作成功! 請稍後……") time.sleep(3) return 0 # 走到這裡說明密碼賬號沒錯 def operation(self): print("*******************************************") print("* 開戶(1) 查詢(2) *") print("* 取款(3) 存款(4) *") print("* 轉賬(5) 改密(6) *") print("* 鎖定(7) 解鎖(8) *") print("* 查詢所有用戶信息(9) 銷戶(0) *") print("* 退出(t) *") print("********************************************")
#設置一個卡的類ps:密碼隱藏不太會,哪位大神可評論區留言。蟹蟹 class Card(object): def __init__(self,cid,pwd): self.cid = cid # 卡號 self.pwd = pwd # 密碼 self.money = 0 # 餘額 self.is_lock = False # 是否鎖定,預設不是
#設置一個用戶類ps:包括用戶信息 from os import path import os import pickle class User(object): def __init__(self, name, uid, card): self.name = name # 用戶名 self.uid = uid # 身份證 self.card = card # 銀行卡 def __str__(self): return ("姓名:{},身份證號:{},卡號:{}".format(self.name,self.uid,self.card.cid)) # 將用戶對象保存到文件中 @staticmethod def all_user(userinfo): pathname = path.join(os.getcwd(),"userinfo.db") with open(pathname, "wb") as fp: pickle.dump(userinfo, fp) # 從文件中載入數據重新轉換為用戶對象 @staticmethod def load_user(): pathname = path.join(os.getcwd(), "userinfo.db") if path.exists(pathname): with open(pathname, "rb") as fp: userinfo = pickle.load(fp) return userinfo else: return {}
#設置一個輔助類 from random import randint # 生成隨機的銀行卡號 class BankCard(): def get_cid(length=8): cid = "" for i in range(8): cid += str(randint(0, 9)) return cid
# 創建一個ATM操作類 import time import random from card import Card from helper import BankCard from user import User class ATM(object): def __init__(self,userinfo): self.userinfo = userinfo # 保存所有的用戶信息 def kaihu(self): # 開戶,目標是向用戶列表中添加一對鍵值對(卡號:用戶) name = input("請輸入您的姓名:") uid = input("請輸入身份證號:") pwd = input("請輸入密碼:") cid = BankCard.get_cid() card = Card(cid, pwd) user = User(name, uid, card) print(user) print("開戶成功") self.userinfo[cid] = user User.all_user(self.userinfo) def chaxun(self): # 查詢 cid = input("請輸入您的卡號") user = self.userinfo.get(cid) if user: if user.card.is_lock: print("該卡已鎖定,請先解鎖!") return else: print("正在查詢,請稍後") time.sleep(1.5) print("賬戶餘額:¥{}元".format(user.card.money)) else: print("無效的卡") def qukuan(self): # 取款 cid = input("請輸入您的卡號") user = self.userinfo.get(cid) if user.card.is_lock: print("該卡已鎖定,請先解鎖!") return else: money = (int(input("請輸入您要取款的金額:"))) user.card.money -= money User.all_user(self.userinfo) print("取款成功") def cunkuan(self): # 存款 count = 0 while True: cid = input("請輸入銀行卡號:") user = self.userinfo.get(cid) if user: if user.card.is_lock: print("該卡已鎖定,請先解鎖!") return pwd = input("請輸入您的銀行卡密碼:") if pwd == user.card.pwd: money = input("請輸入您的存款金額:") user.card.money+= int(money) User.all_user(self.userinfo) print("存錢成功") break elif count < 3: print("密碼錯誤") count += 1 if count == 3: user.card.is_lock = True print("該卡被鎖定,請本人攜帶身份證到櫃臺辦理解鎖") user.all_user(self.userinfo) break else: print("無效的卡") def zhuanzhang(self): # 轉賬 count = 0 while count < 3: cid = input("請輸入您的卡號:") user = self.userinfo.get(cid) if user: if user.card.is_lock: print("該卡已鎖定,請先解鎖!") return else: pwd = input("請輸入您的密碼:") if pwd == user.card.pwd: other_cid = input("請輸入收款人的賬戶:") other_user = self.userinfo.get(other_cid) money = int(input("請輸入轉賬的金額:")) if user.card.money >= money: other_user.card.money += money user.card.money -= money user.all_user(self.userinfo) print("轉賬成功") break else: print("密碼錯誤,請重新輸入:") count += 1 continue else: print("無效的卡") def gaimi(self): # 改密 cid = input("請輸入您的卡號:") user = self.userinfo.get(cid) if user: if user.card.is_lock: print("該卡已鎖定,請先解鎖!") return else: pwd = input("請輸入您的銀行卡原密碼:") count = 0 while True : if pwd == user.card.pwd: pwd = input("請輸入您要修改的銀行卡密碼:") pwd1= input("請再次輸入您要修改的銀行卡密碼:") if pwd == pwd1: user.card.pwd = pwd user.all_user(self.userinfo) print("密碼修改成功") break else: print("兩次密碼輸入不一致,請重新輸入:") continue else: print("原密碼輸入有誤!") count += 1 if count >= 3: user.card.is_lock = True print("該卡被鎖定,請本人攜帶身份證到櫃臺辦理解鎖") user.all_user(self.userinfo) break else: print("剩餘輸入次數{}".format(3-count)) def suodin(self): # 鎖定 cid = input("請輸入您的卡號:") user = self.userinfo.get(cid) if user: if user.card.is_lock: print("該卡已被鎖定!") return else: user.card.is_lock = True print("鎖定成功!") user.all_user(self.userinfo) return def jiesuo(self): # 解鎖 cid = input("請輸入您的卡號:") user = self.userinfo.get(cid) if user.card.is_lock: uid = input("請出示您本人的身份證:") if uid == user.uid: user.card.is_lock = False print("解鎖成功") else: print("本銀行暫不支持代解鎖!!!") def show(self): # 查詢所有用戶信息 for u in self.userinfo: print(self.userinfo[u]) def xiaohu(self): # 銷戶 count = 0 while count < 3: cid = input("請輸入您的卡號") user = self.userinfo.get(cid) if user: pwd = input("請輸入您的密碼:") if pwd == user.card.pwd: uid = input("請出示您的身份證:") if uid == user.uid: del self.userinfo[cid] user.all_user(self.userinfo) print("該賬戶已註銷") break else: print("暫不接受代理,ps:你丫逗我玩呢?") else: print("密碼錯誤,請重新輸入:") count += 1 continue else: print("該卡已損壞")