用简单的面向对象方法模拟一个银行终端处理器
首先, 先处理管理员登录, 在这里默认管理员登录账号为 admin 密码为 123456
- # 管理员系统
- class Admin:
- # 输入参数 管理员卡号 管理员密码
- def __init__(self, account='admin', password='123456'):
- self.account = account
- self.password = password
- # 欢迎界面
- def welcome(self):
- print('+' + '-' * 30 + '+')
- print('|' + '' * 30 +'|')
- print('|' + '' * 3 +'欢迎使用 xx 银行操作系统'+' '* 5 +'|')
- print('|' + '' * 30 +'|')
- print('+' + '-' * 30 + '+')
- # 操作界面
- def menu(self):
- print('+' + '-' * 30 + '+')
- print('|' + '开 户[1] 销 户[2]' + '|')
- print('|' + '查询余额[3] 取 款[4]' + '|')
- print('|' + '存 款[5] 转 账[6]' + '|')
- print('|' + '修改密码[7] 锁定账户[8]' + '|')
- print('|' + '解锁账户[9] 退 出[0]' + '|')
- print('|' + '查询所有账户信息[10]' + '|')
- print('+' + '-' * 30 + '+')
- # 管理员登录
- def login(self):
- account = input('请输入管理员账号:')
- password = input('请输入管理员密码:')
- if account == 'admin' and password == '123456':
- return True
- return False
- # 这里模拟了管理员的登录以及操作页面, 接下来我们创建一个简单的用户登录的类
- import os
- import pickle
- class User:
- 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 save_user(userinfo):
- pathname = os.path.join(os.getcwd(), 'userinfo.bd')
- with open(pathname, 'wb') as fp:
- pickle.dump(userinfo, fp)
- # 将数据读取
- @staticmethod
- def load_user():
- pathname = os.path.join(os.getcwd(), 'userinfo.bd')
- if os.path.exists(pathname):
- with open(pathname, 'rb') as fp:
- userinfo = pickle.load(fp)
- return userinfo
- else:
- return {}
- # 用户类包含了用户的信息以及用户信息的打印, 与上个代码里的 10: 查看用户信息相对应, 为了更加简单的测试, 后两个类函数使用静态方法, 可以使银行开户时把用户信息进行简单持久化存储 (在这里用的是序列化的方式) 和用户登录使用系统时提取用户数据
大家众所周知当我们去银行的时候, 我们需要插入银行卡, 那么我们就需要一个银行卡类来储存我们的用户密码, 银行卡号, 卡上金额. 当然, 当你信誉不好时, 银行同样也会锁定你的银行卡
- class Card:
- def __init__(self, cid, pwd):
- self.cid = cid
- self.pwd = pwd
- self.money = 0 # 余额
- self.is_lock = False # 是否锁定
当我们把所有的类都创建好后, 我们需要把我们写的类的内容进行一个操作整理
- from user import User
- from admin import Admin
- from operate import Operate
- if __name__ == '__main__':
- # 创建管理员对象
- admin = Admin()
- # 统计管理员登录次数
- count = 0
- while True:
- # 导入欢迎界面
- admin.welcome()
- # 管理员登录
- ret = admin.login()
- if ret:
- print('登录成功')
- # 加载所有用户数据
- userinfo = User.load_user()
- # 创建操作对象
- op = Operate(userinfo)
- while True:
- # 导入操作页面
- admin.menu()
- num = int(input('请输入您的操作:'))
- if num == 0:
- print('彻底退出')
- is_quit = True
- break
- elif num == 1:
- op.new_user()
- elif num == 2:
- op.del_user()
- elif num == 3:
- op.query_money()
- elif num == 4:
- op.get_money()
- elif num == 5:
- op.save_money()
- elif num == 6:
- op.pass_money()
- elif num == 7:
- op.change_pwd()
- elif num == 8:
- op.lock_user()
- elif num == 9:
- op.unlock_user()
- elif num == 10:
- op.show_users()
- else:
- print('操作有误, 请重新输入')
- if is_quit:
- break
- else:
- print('登录失败')
- count += 1
- if count>= 3:
- print('错误已达上限, 禁止登录')
- break
我在这里描写的很清楚, 管理员的登录, 以及系统的退出, 一系列的操作只是为了规划我们的程序, 接下来大家会发现还少了一个类, 那就是操作类, 我们需要把操作的函数归结到一个类里, 那就是操作类, 在这里我们可以实行各种操作的添加:
- from helper import Helper
- from card import Card
- from user import User
- import time
- class Operate:
- def __init__(self, userinfo):
- self.userinfo = userinfo
- # 开户
- def new_user(self):
- name = input('请输入您的姓名:')
- uid = input('请输入您的身份证号码:')
- while True:
- pwd = input('请输入您的密码:')
- pwd2 = input('请再次输入您的密码:')
- if pwd != pwd2:
- print('您所输入的两次密码不一致, 请重新输入:')
- else:
- pwd = Helper.generate_password_hash(pwd)
- cid = Helper.generate_cid()
- card = Card(cid, pwd)
- user = User(name, uid, card)
- self.userinfo[cid] = user
- User.save_user(self.userinfo)
- time.sleep(1)
- print('开户成功')
- break
- # 销户
- def del_user(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo.get(cid)
- if user:
- if user.card.is_lock:
- print('该卡已经锁定, 请先去解锁')
- return
- else:
- count = 0
- while count <3:
- pwd = input('请输入您的密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- del self.userinfo[cid]
- time.sleep(1)
- print('销户成功')
- User.save_user(self.userinfo)
- break
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- print('密码错误已达上限, 该卡已锁定')
- else:
- print('用户不存在')
- # 查钱
- def query_money(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo.get(cid)
- if user:
- if user.card.is_lock:
- print('该卡已经锁定, 请先去解锁')
- return
- else:
- count = 0
- while count < 3:
- pwd = input('请输入您的密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- time.sleep(1)
- print('您的余额为{}元'.format(user.card.money))
- break
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- print('密码错误已达上限, 该卡已锁定')
- else:
- print('卡号不存在')
- # 取钱
- def get_money(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo[cid]
- if user:
- if user.card.is_lock:
- print('该卡已经锁定, 请先去解锁')
- return
- else:
- money = int(input('请输入金额'))
- count = 0
- while count < 3:
- pwd = input('请输入您的密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- if user.card.money> money:
- user.card.money -= money
- User.save_user(self.userinfo)
- time.sleep(1)
- print('取款成功')
- break
- else:
- print('余额不足, 请及时充值')
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- print('密码错误已达上限, 该卡已锁定')
- else:
- print('卡号不存在')
- # 存钱
- def save_money(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo[cid]
- if user:
- if user.card.is_lock:
- print('该卡已经锁定, 请先去解锁')
- return
- else:
- money = int(input('请输入金额'))
- count = 0
- while count <3:
- pwd = input('请输入您的密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- user.card.money += money
- User.save_user(self.userinfo)
- time.sleep(1)
- print('存款成功')
- break
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- print('密码错误已达上限, 该卡已锁定')
- else:
- print('卡号不存在')
- # 转账
- def pass_money(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo[cid]
- if user:
- if user.card.is_lock:
- print('该卡已经锁定, 请先去解锁')
- return
- else:
- money = int(input('请输入金额'))
- count = 0
- while count < 3:
- pwd = input('请输入您的密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- dist_cid = input('请输入收款人的卡号:')
- dist_user = self.userinfo[dist_cid]
- if dist_user:
- if user.card.money>= money:
- user.card.money -= money
- dist_user.card.money += money
- User.save_user(self.userinfo)
- time.sleep(1)
- print('转账成功')
- break
- else:
- print('没钱你转个毛')
- else:
- print('收款人的卡号错误')
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- print('密码错误已达上限, 该卡已锁定')
- else:
- print('卡号不存在')
- # 修改密码
- def change_pwd(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo.get(cid)
- if user:
- count = 0
- while count < 3:
- pwd = input('请输入原密码:')
- if Helper.check_password_hash(pwd, user.card.pwd):
- new_pwd = input('请输入新密码:')
- new_pwd2 = input('请再次输入新密码:')
- if new_pwd == new_pwd2:
- user.card.pwd = Helper.generate_password_hash(new_pwd)
- User.save_user(self.userinfo)
- time.sleep(1)
- print('密码修改成功')
- else:
- print('您所输入的两次密码不一致')
- break
- else:
- count += 1
- print('密码错误, 请重新输入密码')
- else:
- print('卡号不存在')
- # 锁定账号
- def lock_user(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo.get(cid)
- if user:
- uid = input('请出示您的身份证:')
- if uid == user.uid:
- user.card.is_lock = True
- User.save_user(self.userinfo)
- time.sleep(1)
- print('锁定成功')
- else:
- print('身份证是不是带错了 @_@')
- else:
- print('卡是不是折了: 卡号错了')
- # 解锁账号
- def unlock_user(self):
- cid = input('请输入您的卡号:')
- user = self.userinfo.get(cid)
- if user:
- uid = input('请出示您的身份证:')
- if uid == user.uid:
- user.card.is_lock = False
- User.save_user(self.userinfo)
- time.sleep(1)
- print('解锁成功')
- else:
- print('身份证是不是带错了 @_@')
- else:
- print('卡是不是折了: 卡号错了')
- # 查看账户信息
- def show_users(self):
- for user in self.userinfo:
- print(self.userinfo[user])
- time.sleep(1)
在这个类里, 大家不免看到了几个 helper 类的方法调用, 因为这些方法 (随机生成卡号, 登录密码加密存储) 是一些辅助手段, 所以我们放在了 helper 类里面
- from random import randint
- import hashlib
- class Helper:
- # 生成卡号
- @staticmethod
- def generate_cid(lenght=8):
- cid = ''
- for i in range(lenght):
- cid += str(randint(0, 9))
- return cid
- # 生成 md5 加密后的内容
- @staticmethod
- def generate_password_hash(password):
- m = hashlib.md5()
- m.update(password.encode('utf-8'))
- return m.hexdigest()
- # 校验 md5 加密后的密码
- @staticmethod
- def check_password_hash(pwd, pwd_hash):
- m = hashlib.md5()
- m.update(pwd.encode('utf-8'))
- return m.hexdigest() == pwd_hash
我们的密码加密使用的是哈希加密, 而我们的生成卡号也不那么严谨, 采用的是随机数方法
接下来我们就可以尽情的测试代码了, 可以开户, 并根据银行卡号和密码进行存取和转账, 当然如果你忘了密码可是要被锁定的哦, 千万别忘了去解锁账户, 我们也可以更改密码呦!
当然我写的代码里还有很多漏洞, 毕竟是一个初学者, 我才学了两星期了, 期待各位大佬的指教, 谢谢!
来源: http://www.bubuko.com/infodetail-2722761.html