python练习之银行模拟系统
创建 atm.py from card import Card from user import User import random class Atm(object): def __init__(self): self.user_dict = {} def print_ui(self): print("".center(50, "*")) print("*".ljust(16, " "), "开户(1) 查询(2)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "取款(3) 存款(4)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "转账(5) 改密(6)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "锁定(7) 解锁(8)", " ".rjust(13, " "), "*") print("*".ljust(16, " "), "销户(9) 退出(10)", " ".rjust(12, " "), "*") print("".center(50, "*")) def openAcount(self): name=input("请输入姓名:") phonenum=int(input("请输入电话号码:")) identycard=input("请输入身份证号码:") passwd=input("请输入密码:") passwd1=input("请再输入密码:") if passwd!=passwd1: print("两次密码不一致,开户失败") return money=float(input("请输入存款金额:")) if money<10: print("存款金额少于10元,开户失败") return cardid=random.randrange(1000000,9999999) print("您的卡号为:",cardid) card=Card(cardid,passwd,money) user=User(name,identycard,phonenum,card) print("开户成功") self.user_dict[cardid]=user def Search(self): cardid=int(input("请输入卡号:")) user=self.user_dict.get(cardid) if user==None: print("卡号不存在") return if user.card.islock==True: print('卡被锁定,请先解锁') return for i in range(3): passwd=input("请输入密码:") if user.card.passwd!=passwd: print("密码错误") else: break else: print("机会已用完,卡被锁了") user.card.islock = True return print("用户名:%s,身份证号%s:,电话号%d:,卡号:%s,余额:%.2f" %(user.name,user.identycard,user.phonenum,user.card.cardid,user.card.money )) def Savemoney(self): cardid=int(input("请输入卡号:")) user=self.user_dict.get(cardid) if user==None: print("卡号不存在") return if user.card.islock==True: print('卡被锁定,请先解锁') return for i in range(3): passwd=input("请输入密码:") if user.card.passwd!=passwd: print("密码错误") else: break else: print("机会已用完,卡被锁了") user.card.islock = True return money=float(input("请输入存款金额:")) user.card.money+=money print("存款成功,当前余额为:",user.card.money) def Takemoney(self): cardid=int(input("请输入卡号:")) user=self.user_dict.get(cardid) if user==None: print("卡号不存在") return if user.card.islock==True: print('卡被锁定,请先解锁') return for i in range(3): passwd=input("请输入密码:") if user.card.passwd!=passwd: print("密码错误") else: break else: print("机会已用完,卡被锁了") user.card.islock = True return money=float(input("请输入取款金额:")) if money>user.card.money: print("输入金额超出卡内余额,请重新输入:") return money user.card.money-=money print("取款成功,当前余额为:",user.card.money) def Changemoney(self): cardid=int(input("请输入卡号:")) user=self.user_dict.get(cardid) if user==None: print("卡号不存在") return if user.card.islock==True: print('卡被锁定,请先解锁') return for i in range(3): passwd=input("请输入密码:") if user.card.passwd!=passwd: print("密码错误") else: break else: user.card.islock = True print("机会已用完,卡被锁了") return othercard=int(input("请输入对方卡号:")) otheruser = self.user_dict.get(othercard) if otheruser == None: print("卡号不存在") return money=float(input("请输入转账金额:")) if money>user.card.money: print("输入金额超出卡内余额,请重新输入:") return money user.card.money-=money print("转账成功,当前余额为:",user.card.money) def Changepasswd(self): cardid = int(input("请输入卡号:")) user = self.user_dict.get(cardid) if user == None: print("卡号不存在") return if user.card.islock == True: print('卡被锁定,请先解锁') return for i in range(3): passwd = input("请输入密码:") if user.card.passwd != passwd: print("密码错误") else: break else: user.card.islock = True print("机会已用完,卡被锁了") return if user == None: print("卡号不存在") return identycard=input("请输入身份证号码:") if user.identycard!=identycard: print("身份信息验证错误,请重新输入") return passwd = input("请输入新的密码:") user.card.passwd=passwd print("密码更改成功") print("用户名:%s,身份证号%s:,电话号%d:,卡号:%s,余额:%.2f" % (user.name, user.identycard, user.phonenum, user.card.cardid, user.card.money)) # 锁卡 def Lockcard(self): cardid = int(input("请输入卡号:")) user = self.user_dict.get(cardid) if user == None: print("卡号不存在") return if user.card.islock == True: print('卡已被锁定') return print("是否确认锁卡,如果锁卡请输入1,退出请输入0:") num=int(input()) if num==1: for i in range(3): passwd = input("请输入密码:") if user.card.passwd != passwd: print("密码错误") else: print("锁卡成功") user.card.islock =True break else: print("锁卡失败") return else: exit() # 解锁 def solveLock(self): cardid = int(input("请输入卡号:")) user = self.user_dict.get(cardid) if user == None: print("卡号不存在") return if user.card.islock == False: print('卡未被锁定') return print("是否确认解卡,如果解卡请输入1,退出请输入0:") num = int(input()) if num == 1: for i in range(3): identycard = input("请输入身份证号:") if user.identycard != identycard: print("卡和姓名不匹配,解卡失败") else: print("解卡成功") user.card.islock = False break else: print("解卡失败") return else: exit() # 销户(删除用户) def Popuser(self): cardid = int(input("请输入卡号:")) user = self.user_dict.get(cardid) if user == None: print("卡号不存在") return if user.card.islock == True: print('卡被锁定,请先解锁') return print("是否确认销户,如果销户请输入1,退出请输入0:") num = int(input()) if num == 1: for i in range(3): passwd = input("请输入密码:") if user.card.passwd != passwd: print("密码错误,销户失败") else: print("销户成功") self.user_dict.pop(cardid) break else: exit()
创建user.py class User: def __init__(self,name,identycard,phonenum,card): self.name=name self.identycard=identycard self.phonenum=int(phonenum) self.card = card
card.py class Card(object): def __init__(self,cardid,passwd,money): self.cardid=cardid self.passwd=passwd self.money=money self.islock=False #未锁定 #状态值一般用布尔表示
创建 Admin.py
class Admin:
def __init__(self,name,passwd):
self.name=name
self.passwd = passwd
def login(self,):
name=input("请输入管理员账户:")
passwd=input("请输入管理员密码:")
if self.name==name and self.passwd==passwd:
print("登录成功")
return True
else:
print("登录失败")
return False
创建 main.py """ 银行模拟 卡类: 属性: 账号,密码,余额 用户类: 属性: 姓名,身份证,手机号,银行卡 ATM类: 属性:列表或者字典保村用户信息 行为:打印界面,开户,查询,转账,取款,存款,锁卡,解锁,改密,销户,退出 管理员类: 属性:管理员的账号和密码 行为:登录 """ #先创建管理员类 from admin import Admin from atm import Atm def main (): ad=Admin("zhangsan","123") #调用管理员的登录 for i in range(3): isok=ad.login() if isok==True: #创建Atm对象 atm=Atm() while True: # 创建打印界面 atm.print_ui() bh=int(input("请输入功能编号:")) if bh==1: atm.openAcount() if bh==2: atm.Search() if bh==3: atm.Takemoney() if bh==4: atm.Savemoney() if bh==5: atm.Changemoney() if bh==6: atm.Changepasswd() if bh==7: atm.Lockcard() if bh==8: atm.solveLock() if bh==9: atm.Popuser() if bh==10: exit() break if __name__=="__main__": main()
基本功能已经实现,需要的可以自己研究