小項(xiàng)目----銀行業(yè)務(wù)操作(管理員的操作)
要求實(shí)現(xiàn)十個(gè)功能 編號(hào):0~9
類名: person
屬性: name
userid
phone
card
類名: card
屬性: cardid
password
money
類名: view
方法: login view
方法: oparation view
類名:oparation
開戶(1) 查詢(2)
存錢(3) 取錢(4)
轉(zhuǎn)賬(5) 改密(6)
鎖卡(7) 解卡(8)
補(bǔ)卡(9) 退出(0)
#view.py
class View:
def login(self):
i = 1
while i <= 3:
name = input("請(qǐng)輸入管理員賬號(hào):")
pwd = input("請(qǐng)輸入管理員密碼:")
if name == "admin" and pwd == "admin":
print("歡迎管理員登錄")
i = 4
return True
else :
print("賬號(hào)或密碼輸入不正確!,還有%s次機(jī)會(huì)" % (3 - i))
i += 1
def login_view(self):
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$ $")
print("$ $")
print("$ Welcome To MHG Bank $")
print("$ $")
print("$ $")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
def oparation_view(self):
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$ 開戶(1) 查詢(2) $")
print("$ 存錢(3) 取錢(4) $")
print("$ 轉(zhuǎn)賬(5) 改密(6) $")
print("$ 鎖卡(7) 解卡(8) $")
print("$ 補(bǔ)卡(9) 退出(0) $")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
if __name__ == '__main__':
v = View()
v.login()
#person.py
class Person:
def __init__(self,name,userid,phone,cardid):
self.name = name
self.userid = userid
self.phone = phone
self.cardid = cardid
#card.py
class Card:
def __init__(self,cardid,password,money):
self.cardid = cardid
self.password = password
self.money = money
self.islock = True
#main.py
from day12.bank.view import View #路徑
from day12.bank.operation import Operation
import time
v = View()
def main():
while not v.login():
print("賬號(hào)凍結(jié)!等5秒后再操作!")
time.sleep(5)
v.login_view()
v.oparation_view()
o = Operation()
while True:
choice = input("請(qǐng)選擇需要的服務(wù)項(xiàng)目:")
if choice == "1": # 開戶
o.register()
elif choice == "2": # 查詢
o.query()
elif choice == "3": # 存錢
o.save_money()
elif choice == "4": # 取錢
o.get_money()
elif choice == "5": # 轉(zhuǎn)賬
o.trans_money()
elif choice == "6": # 改密
o.change_password()
elif choice == "7": # 鎖卡
o.lock()
elif choice == "8": # 解鎖
o.unlock()
elif choice == "9": # 補(bǔ)卡
o.mend_card()
elif choice == "0": # 退出
o.exit_Bank()
print("歡迎下次光臨!")
break
if __name__ == '__main__':
main()
#operation.py
from day12.bank.person import Person #路徑
from day12.bank.card import Card
import pickle
import os
import random
class Operation:
def __init__(self):
self.users()
self.cards()
print(self.user_dict)
print(self.card_dict)
# 用戶信息
def users(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 cards(self):
if os.path.exists("card.txt"):
with open("card.txt", "rb") as f:
self.card_dict = pickle.load(f)
else:
self.card_dict = {}
# 設(shè)置密碼
def get_password(self):
while True:
password = input("請(qǐng)輸入您的六位數(shù)字密碼:")
if password.isdigit() and len(password) == 6:
rpassword = input("請(qǐng)確認(rèn)您的六位數(shù)字密碼:")
if password == rpassword:
return password
else:
print("您輸入的兩次密碼不一致,請(qǐng)重新輸入!")
else:
print("密碼格式錯(cuò)誤,請(qǐng)重新輸入!")
# 設(shè)置手機(jī)號(hào)
def get_phone(self):
while True:
phone = input("請(qǐng)輸入您的十一位手機(jī)號(hào):")
if phone.isdigit() and len(phone) == 11 and phone.startswith("1"):
return phone
else:
print("請(qǐng)輸入正確手機(jī)號(hào)!")
# 設(shè)置身份證號(hào)
def get_userid(self):
while True:
userid = input("請(qǐng)輸入您的十八位身份證號(hào):")
if userid.isdigit() and len(userid) == 18 and not userid.startswith("0"):
return userid
else:
print("請(qǐng)輸入正確身份證號(hào)")
# 設(shè)置初始存款
def get_initial_money(self):
while True:
money = input("請(qǐng)輸入初始存款金額:")
if money.isdigit() :
return money
else:
print("請(qǐng)輸入正確存款金額!")
# 生成六位卡號(hào)
def get_cardid(self):
while True:
# 隨機(jī)生成一個(gè)卡號(hào)
cardid = random.randint(100000,999999)
if cardid not in self.user_dict:
return cardid
# 獲取卡信息
def get_card_info(self):
# 用戶先輸入卡號(hào)
while True :
try:
cardid = int(input("請(qǐng)輸入您的六位數(shù)字卡號(hào):"))
break
except Exception:
print("請(qǐng)輸入您的“六位數(shù)字”卡號(hào)!")
# 判斷卡號(hào)是否存在
if cardid not in self.user_dict:
return False
else:
# 通過卡號(hào)找到用戶 存儲(chǔ)的格式為:self.user_dict[cardid] = user
user = self.user_dict[cardid]
# 然后通過用戶找到卡
card = self.card_dict[user.userid]
# 返回用戶的卡
return card
# 檢測(cè)密碼
def check_password(self,card):
i = 1
while i < 4:
# 讓用戶輸入卡密碼
while True:
try:
password = int(input("請(qǐng)輸入你的密碼:"))
break
except Exception:
print("請(qǐng)輸入您的“六位數(shù)字”密碼!")
# 如果用戶輸入的密碼和卡的密碼一致
if card.password == str(password):
return True
else:
print("賬號(hào)或密碼輸入不正確!,還有%s次機(jī)會(huì)" % (3 - i))
i += 1
if i == 4:
card.islock = True
print("密碼輸入三次失敗,卡已經(jīng)被鎖定!")
# 鎖卡
def lock(self):
card = self.get_card_info()
if card:
if card.islock:
user = self.user_dict[card.cardid]
print("解鎖需要核對(duì)您的身份信息!")
print("@".join(user.userid))
while True:
userid = input("請(qǐng)輸入您的身份證號(hào):")
if user.userid == userid:
lock = input("確認(rèn)鎖卡請(qǐng)按 1:")
if lock == "1":
card.islock = False
print("鎖卡成功!")
break
else:
print("卡已被鎖定!")
else:
print("卡號(hào)錯(cuò)誤!")
# 解鎖
def unlock(self):
card = self.get_card_info()
if not card:
print("卡號(hào)錯(cuò)誤!")
else:
if card.islock:
print("卡未鎖定!")
else:
if self.check_password(card):
card.islock = True
print("解卡成功!")
else:
user = self.user_dict[card.cardid]
print("解鎖需要核對(duì)您的身份信息!")
print("@".join(user.userid))
while True:
userid = input("請(qǐng)輸入您的身份證號(hào):")
if user.userid == userid:
print("請(qǐng)輸入新的密碼!")
password = self.get_password()
card.password = str(password)
card.islock = True
print("解卡成功!")
break
else:
print("身份證號(hào)碼不正確,請(qǐng)重新輸入!")
# 開卡功能
def register(self):
name = input("請(qǐng)輸入您的姓名:")
password = self.get_password()
phone = self.get_phone()
userid = self.get_userid()
cardid = self.get_cardid()
money = self.get_initial_money()
card = Card(cardid,password,money)
self.card_dict[userid] = card
user = Person(name,userid,phone,cardid)
self.user_dict[cardid] = user
print("恭喜%s開卡成功,卡號(hào)%s,卡內(nèi)余額%s元"%(user.name,cardid,card.money))
self.exit_Bank()
# 余額查詢
def query(self):
card = self.get_card_info()
if card:
if card.islock:
lock = self.check_password(card)
if lock:
print("卡內(nèi)余額為%s元"%(card.money))
else:
pass
else:
print("您的卡被鎖定了!")
else:
print("卡號(hào)錯(cuò)誤!")
# 改密
def change_password(self):
card = self.get_card_info()
if card:
if card.islock:
if self.check_password(card):
while True:
while True:
try:
password = int(input("請(qǐng)輸入六位數(shù)字新密碼:"))
break
except Exception:
print("請(qǐng)輸入您的“六位數(shù)字”密碼!")
while True:
try:
rpassword = int(input("請(qǐng)確認(rèn)六位數(shù)字新密碼:"))
break
except Exception:
print("請(qǐng)確認(rèn)您的“六位數(shù)字”密碼!")
if password == rpassword:
card.password = str(password)
print("修改成功!")
break
else:
print("兩次密碼不一致!")
else:
print("您的卡已經(jīng)被鎖定,請(qǐng)先解卡!")
else:
print("卡號(hào)錯(cuò)誤!")
# 存錢
def save_money(self):
card = self.get_card_info()
if card:
if card.islock:
money = int(input("請(qǐng)輸入存款金額:"))
if money < 0:
print("輸入的金額不正確!")
else:
initial_money = int(card.money)
initial_money += money
card.money = str(initial_money)
print("卡內(nèi)余額為%s元" % (card.money))
else:
print("您的卡已被鎖定,請(qǐng)先解卡")
else:
print("卡號(hào)錯(cuò)誤!")
# 取錢
def get_money(self):
card = self.get_card_info()
if card:
if card.islock:
if self.check_password(card):
money = int(input("請(qǐng)輸入取款金額:"))
if money < int(card.money) and money > 0:
initial_money = int(card.money)
initial_money -= money
card.money = str(initial_money)
print("卡內(nèi)余額為%s元" % (card.money))
else:
print("輸入的金額有誤,請(qǐng)重新輸入!")
else:
pass
else:
print("您的卡已被鎖定,請(qǐng)先解卡")
else:
print("卡號(hào)錯(cuò)誤!")
# 轉(zhuǎn)賬
def trans_money(self):
card = self.get_card_info()
user = self.user_dict[card.cardid]
if card:
if card.islock:
if self.check_password(card):
# 輸入收款方的賬號(hào)
otherid = int(input("請(qǐng)輸入對(duì)方賬號(hào):"))
if otherid not in self.user_dict:
print("卡號(hào)不存在!")
else:
# 通過收款方的賬號(hào)找到收款人
other_user = self.user_dict[otherid]
# 通過收款人找到卡
other_card = self.card_dict[other_user.userid]
if not other_card.islock:
print("對(duì)方卡已經(jīng)被鎖定,請(qǐng)先解卡!")
else:
money = int(input("請(qǐng)輸入轉(zhuǎn)賬金額:"))
if money > 0 and money <= int(card.money):
initial_money = int(card.money)
initial_money -= money
card.money = str(initial_money)
other_initial_money = int(other_card.money)
other_initial_money += money
other_card.money = str(other_initial_money)
print("%s向%s成功轉(zhuǎn)賬%s元"%(user.name,other_user.name,money))
else:
print("轉(zhuǎn)賬金額不正確!")
else:
print("您的卡已經(jīng)被鎖定,請(qǐng)先解卡!")
else:
print("卡號(hào)錯(cuò)誤!")
# 補(bǔ)卡
def mend_card(self):
userid = input("請(qǐng)輸入身份證號(hào):")
if userid in self.card_dict:
card = self.card_dict[userid]
# 生成一個(gè)新的卡號(hào)
new_cardid = self.get_cardid()
# 修改老用戶對(duì)應(yīng)的卡號(hào)
self.user_dict[new_cardid] = self.user_dict.pop(card.cardid)
# 修改卡號(hào)
card.cardid = new_cardid
print("新卡已開出,請(qǐng)輸入新卡密碼!")
new_password = self.get_password()
card.password = new_password
# 通過新卡號(hào)找到新用戶
new_user = self.user_dict[new_cardid]
new_user.cardid = new_cardid
print("尊敬的用戶%s,新卡號(hào)為%s,手機(jī)號(hào):%s,身份證號(hào):%s,卡內(nèi)余額:%s"%(new_user.name,card.cardid,new_user.phone,new_user.userid,card.money))
else:
print("身份證號(hào)不存在!")
# 退出保存
def exit_Bank(self):
with open("user.txt","wb") as f:
pickle.dump(self.user_dict,f)
with open("card.txt","wb") as f:
pickle.dump(self.card_dict,f)
if __name__ == '__main__':
o = Operation()
# o.register()
# o.exit_Bank()
# o.save_money()
# o.query()
# o.get_money()
# o.change_password()
# o.mend_card()
# o.trans_money()
# o.lock()