user
class User:
"""
用户类
"""
def __init__(self,name,id,phone,card):
"""
构造方法
:param name:用户名
:param id: 身份证
:param phone: 电话
:param card: 银行卡(对象)
"""
self.name =name
self.id = id
self.phone =phone
self.card =card
card
class Card:
"""
银行卡类
"""
def __init__(self,card_num,pwd,money):
"""
银行卡构造方法
:param card_num:卡号
:param pwd: 密码
:param money: 余额
"""
self.card_num=card_num
self.pwd =pwd
self.money =money
self.cardLock =False
bank_UI
class BankUI:
"""
界面辅助类
"""
admin_username ='root'
admin_password ='123'
def print_welcome_ui(self):
"""
打印欢迎界面
:return:
"""
print("*"*45)
print("**** ****")
print("**** 欢迎登录银行管理系统 ****")
print("**** ****")
print("*" * 45)
def print_xt_list(self):
"""
打印系统菜单
:return:
"""
print("*" * 45)
print("**** ****")
print("**** 1.开户(1) 2.查询(2) ****")
print("**** 3.取款(3) 4.存款(4) ****")
print("**** 5.转账(5) 6.锁定(6) ****")
print("**** 7.解锁(7) ****")
print("**** ****")
print("**** 退出系统(Q) ****")
print("**** ****")
print("*" * 45)
def admin_id_yanzheng(self):
"""
验证管理员身份,验证通过方可操作
:return:
"""
username = input("请输入用户名")
if username != BankUI.admin_username:
print("用户名错误!")
return -1
password = input("请输入密码")
if password != BankUI.admin_password:
print("密码错误!")
return -1
print("操作成功!请稍后。。。")
ATM
from card import Card
from user import User
import random
class ATM:
"""
银行业务功能类,里面通过一个字典来模拟其用户数据库,字典的key为卡号,value用户对象
"""
def __init__(self,alluser):
"""
构造方法
:param alluser:
"""
self.alluser =alluser
def random_card_id(self):
"""
随机生成卡号
:return:
"""
str =''
for i in range(6):
ch = chr(random.randrange(ord('0'),ord('9')+1))
str = str+ch
if not self.alluser.get(str):
return str
def create_user(self):
"""
开户
:return:
"""
name = input("请输入用户名:")
userid =input("请输入用户身份证号:")
phone =input("请输入电话:")
init_money =float(input("请输入预存金额:"))
if init_money <0:
print("预存金额有误!")
return -1
pwd1 =input("请输入密码:")
pwd2 =input("请再次输入密码:")
if pwd1 !=pwd2:
print("密码不一致")
return -1
card_num =self.random_card_id()
card =Card(card_num,pwd2,init_money)
user =User(name,userid,phone,card)
self.alluser[card_num] =user
print("开户成功!!,请牢记账号:%s"% card_num)
def check_card_pwd(self,real_pwd):
"""
验证卡的密码
:param real_pwd:
:return:
"""
for i in range(3):
pwd =input("请输入密码:")
if pwd ==real_pwd:
return True
print("三次密码输入错误,系统自动退出。。。")
return False
def lock_card(self):
"""
锁定某张银行卡
:return:
"""
card_num=input("请输入卡号:")
user = self.alluser.get(card_num)
if not user:
print("卡号错误!锁定失败!")
return -1
if not self.check_card_pwd(user.card.pwd):
print("密码错误!锁定失败!")
return -1
user.card.cardLock =True
print("锁定成功!")
def unlock_card(self):
"""
解锁卡
:return:
"""
card_num=input("请输入卡号:")
user =self.alluser.get(card_num)
if not user:
print("卡号错误!解锁失败!")
return -1
elif not user.card.cardLock:
print("此卡未锁定,无需解锁!")
elif not self.check_card_pwd(user.card.pwd):
print("密码输入错误!解锁失败!")
return -1
user.card.cardLock =False
print("解锁成功!")
def look_user(self,base=1):
"""
查询卡的信息
:param base:查询自己的卡的信息(账号,余额),2.转账时,查询转出的卡信息,
3.转账时,查询转入的卡信息
:return: 如果成功则返回user数据,否则返回-1
"""
if base ==1:
card_num =input("请输入卡号:")
elif base ==2:
card_num = input("请输入转出卡卡号:")
elif base ==3:
card_num =input("请输入要转入的卡号:")
user =self.alluser.get(card_num)
if not user:
print("卡号不存在!")
return -1
if user.card.cardLock:
print("此卡被锁定,请解锁后使用!")
if not self.check_card_pwd(user.card.pwd):
print("密码输入次数过多!系统将锁定此卡!请解锁后使用!")
user.card.cardLock = True
return -1
if not base ==3:
print("卡号:%s 余额:%.2f" % (user.card.card_num,user.card.money))
return user
def save_money(self):
"""
存款
:return:
"""
user = self.look_user()
if user !=-1:
in_money =float(input("请输入存款金额:"))
if in_money <0:
print("请输入正确存款金额!")
else:
user.card.money += in_money
print("存款成功!账户:%s 余额:%.2f" % (user.card.card_num, user.card.money))
else:
return -1
def get_money(self):
"""
取款
:return:
"""
user = self.look_user()
if user != -1:
in_money = float(input("请输入取款金额:"))
if in_money < 0:
print("请输入正确取款金额!")
else:
if in_money >= user.card.money:
print("取款金额大于余额!请先查询余额再取款")
return -1
user.card.money -= in_money
print("取款成功!账户:%s 余额:%.2f" %(user.card.card_num,user.card.money))
else:
return -1
def return_money(self):
"""
转账功能
:return:
"""
master_card = self.look_user(base=2)
if not master_card:
return -1
slave_card =self.look_user(base=3)
if not slave_card:
return -1
input_money =float(input("请输入转账金额:"))
if input_money <= master_card.card.money:
master_card.card.money -= input_money
slave_card.card.money += input_money
print("转账成功!账号:%s 余额:%.2f" %(master_card.card.card_num,master_card.card.money))
else:
print("转账失败!余额不足!账号:%s 余额:%.2f" % (master_card.card.card_num, master_card.card.money))
bank
import time
from ATM import ATM
from bank_UI import BankUI
class HomePage:
"""
组装功能,执行银行系统
"""
def __init__(self):
"""
初始化功能,实现模拟数据库字典,初始化ATM和UI
"""
self.alluser = {}
self.atm = ATM(self.alluser)
self.bank_ui = BankUI()
def saveUser(self):
"""
模拟数据存盘
:return:
"""
self.alluser.update(self.atm.alluser)
print("数据存盘成功!")
def main(self):
"""
系统入口
:return:
"""
self.bank_ui.print_welcome_ui()
result = self.bank_ui.admin_id_yanzheng()
if not result:
while True:
self.bank_ui.print_xt_list()
option = input("请选择功能:")
if option not in ('1', '2', '3', '4', '5', '6', '7', 'Q', 'q'):
print("输入有误!")
time.sleep(1)
if option == "1":
self.atm.create_user()
elif option == "2":
self.atm.look_user()
elif option == "3":
self.atm.get_money()
elif option == "4":
self.atm.save_money()
elif option == "5":
self.atm.return_money()
elif option == "6":
self.atm.lock_card()
elif option == "7":
self.atm.unlock_card()
elif option.upper() == "Q":
if not self.bank_ui.print_xt_list():
self.saveUser()
print("正在退出系统···")
return -1
if __name__ == "__main__":
homepage = HomePage()
homepage.main()