模拟銀行業務
print("*********************************************")
print("* 1.開戶 2.查詢 *")
print("* 3.存款 4.取款 *")
print("* 5.轉賬 6.改密 *")
print("* 7.鎖卡 8.解卡 *")
print("* 9.補卡 0.退出 *")
print("*********************************************")
導包
import time
import os
import pickle
import random
定義類
'''
類:
card #卡
person #使用者
view #界面
ATM #ATM機功能-
class Card:
def __init__(self,cardid,password,money):
self.cardid = cardid
self.password = password
self.money = money
self.isclock = False #是否上鎖
class Person:
def __init__(self,name,userid,phone,card):
self.name = name
self.userid = userid
self.phone = phone
self.card = card
class View:
def login(self):
name = input("請輸入管理者賬号:")
password = input("請輸入管理者密碼:")
if name =="admin" and password == "hong": #初始化了賬号和密碼
self.show()
time.sleep(2)
return True
else:
print("管理者賬号或密碼不正确")
return False
def show(self):
print("*********************************************")
print("* *")
print("* *")
print("* 歡迎進入銀行系統 *")
print("* *")
print("* *")
print("*********************************************")
def operation_show(self):
print("*********************************************")
print("* 1.開戶 2.查詢 *")
print("* 3.存款 4.取款 *")
print("* 5.轉賬 6.改密 *")
print("* 7.鎖卡 8.解卡 *")
print("* 9.補卡 0.退出 *")
print("*********************************************")
'''
功能類:
class Operation:
def __init__(self):
self.load_user()
print(self.user_dict)
以下所有函數都包含在Operation類中:
0.首先使用文本用來存儲資訊以及通路資訊
def load_user(self):
#加載檔案之前判斷檔案是否存在
if os.path.exists("user.txt"):
with open("user.txt","rb") as f:
self.user_dict = pickle.load(f)
else:
self.user_dict = {}
def save(self):
'''儲存使用者'''
with open("user.txt","wb") as f:
pickle.dump(self.user_dict,f)
1.開卡
def create(self):
'''開戶'''
name = input("請輸入持卡人姓名:")
user_id = input("請輸入身份證号碼:")
phone = input("請設定預留手機号:")
password = self.get_password()
cardid = self.get_cardid()
#有資訊之後就可以生成一張卡
card = Card(cardid,password,0)
#通過卡找到使用者
user = Person(name,user_id,phone,card)
self.user_dict[cardid] = user
print("開卡成功!您的卡号為:%s,卡内餘額為:%d元" % (cardid,card.money))
self.save()
time.sleep(2)
def get_password(self):
'''設定密碼'''
while True:
pwd1 = input("請設定銀行卡密碼:")
pwd2 = input("請重新輸入密碼:")
if pwd1 == pwd2:
return pwd1
else:
print("密碼輸入不一緻!")
time.sleep(2)
def get_cardid(self):
'''生成卡号'''
while True:
cardid = random.randint(100000,999999)
if cardid not in self.user_dict:
return cardid
2.查詢
def select(self):
''' 查詢功能'''
id = int(input("請輸入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
password = input("請輸入該銀行卡密碼:")
if password == self.user_dict[id].card.password:
print("卡号:%s 持卡人:%s 卡内餘額:%d" % (id, self.user_dict[id].name, self.user_dict[id].card.money))
time.sleep(3)
else:
print("密碼錯誤!")
time.sleep(1)
3.存款
def in_money(self):
'''存錢'''
id = int(input("請輸入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
password = input("請輸入該銀行卡密碼:")
if password == self.user_dict[id].card.password:
money = int(input("請選擇存儲款數:"))
self.user_dict[id].card.money += money
print("存款成功!卡号:%s 持卡人:%s 卡内餘額:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
time.sleep(3)
self.save()
else:
print("密碼錯誤!")
time.sleep(1)
4.取款
def out_money(self):
'''取款'''
id = int(input("請輸入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
password = input("請輸入該銀行卡密碼:")
if password == self.user_dict[id].card.password:
money = int(input("請選擇取款數:"))
if money>self.user_dict[id].card.money:
print("銀行卡餘額不足,無法取款!")
else:
self.user_dict[id].card.money -= money
print("取款成功!卡号:%s 持卡人:%s 卡内餘額:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
time.sleep(3)
self.save()
else:
print("密碼錯誤!")
time.sleep(1)
5.轉賬
def changge(self):
'''轉賬'''
id = int(input("請輸入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
id2 = int(input("請輸入轉賬接收者的卡号"))
if id2 not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id2].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
password = input("請輸入您的銀行卡密碼:")
if password == self.user_dict[id].card.password:
money = int(input("請選轉賬數:"))
if money>self.user_dict[id].card.money:
print("銀行卡餘額不足,無法轉賬!")
else:
self.user_dict[id].card.money -= money
print("轉賬成功!卡号:%s 持卡人:%s 卡内餘額:%d元" % (id,self.user_dict[id].name,self.user_dict[id].card.money))
self.user_dict[id2].card.money += money
time.sleep(3)
self.save()
else:
print("密碼錯誤!")
time.sleep(1)
6.改密
def chang_password(self):
'''修改密碼'''
id = int(input("請輸入卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
print("卡被鎖定,無法使用!")
time.sleep(1)
else:
password = input("請輸入該原銀行卡密碼:")
if password == self.user_dict[id].card.password:
self.user_dict[id].card.password = self.get_password()
print("密碼修改成功!")
time.sleep(3)
self.save()
else:
print("密碼錯誤!")
time.sleep(1)
7.鎖卡
def lock(self):
'''當機卡'''
id = int(input("請輸入要鎖定(當機)的卡号:"))
if self.user_dict[id].card.isclock:
print("該卡号已被鎖定,無需重複鎖定!")
time.sleep(1)
else:
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
phone = input("請輸入預留手機号:") #此處也可以要擷取驗證碼
if phone == self.user_dict[id].phone:
self.user_dict[id].card.isclock = True
print("卡号鎖定(當機)完成!")
self.save()
time.sleep(1)
else:
print("預留手機号碼錯誤!")
time.sleep(1)
8.解卡
def unlock(self):
'''解凍卡'''
id = int(input("請輸入要解鎖的卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
if self.user_dict[id].card.isclock:
phone = input("請輸入預留手機号:") #此處也可以要擷取驗證碼
if phone == self.user_dict[id].phone:
self.user_dict[id].card.isclock = False
print("卡号解鎖完成!")
self.save()
time.sleep(1)
else:
print("預留手機号碼錯誤!")
time.sleep(1)
else:
print("該卡号未被鎖定,無需解鎖!")
time.sleep(1)
9.補卡
注:補卡後原來的卡将會删除,并且原來卡裡的錢需轉移到新卡
def add_card(self):
'''補卡'''
id = int(input("請輸入丢失的卡的卡号:"))
if id not in self.user_dict:
print("卡号不存在!")
time.sleep(1)
else:
name = input("請輸入持卡人姓名:")
user_id = input("請輸入身份證号碼:")
phone = input("請設定預留手機号:")
password = self.get_password()
cardid = self.get_cardid()
# 有資訊之後就可以生成一張卡
card = Card(cardid, password, self.user_dict[id].card.money)
#删除原來的卡
del self.user_dict[id]
# 通過卡找到使用者
user = Person(name, user_id, phone, card)
self.user_dict[cardid] = user
print("補卡卡成功!您的卡号為:%s,卡内餘額為:%d元" % (cardid, card.money))
self.save()
time.sleep(2)
10.主架構
def main():
v = View()
if v.login() == True:
while True:
o = Operation()
v.operation_show()
choice = input("請輸入業務序号:")
if choice == "1": #開戶
o.create()
elif choice == "2": #查詢
o.select()
elif choice == "3": #存款
o.in_money()
elif choice == "4": #取款
o.out_money()
elif choice == "5": #轉賬
o.changge()
elif choice == "6": #改密
o.chang_password()
elif choice == "7": #鎖卡
o.lock()
elif choice == "8": #解卡
o.unlock()
elif choice == "9": #補卡
o.add_card()
elif choice == "0": #退出
o.save()
break
if __name__ == '__main__':
main()