模拟银行管理系统
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): # 生成一位0-9之间的随机数 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()
笔者注:
本文章代码来源于CSDN:YANG_xiao_QI博主,附链接: