千锋python教学视频中的银行系统实战

零基础自学python第24天,目前在看千锋的python教学视频,,下面补全了sunck老师讲的银行系统,问题很多,但至少功能全都能跑通了。

主程序 bankSystem.py

'''
人
类名:User
属性:姓名 身份证号 电话号 卡
行为:

卡:
类名:Card
属性:卡号  密码 余额  锁定
行为:

管理员
类名:Admin
属性:管理员界面  功能界面   管理员账户
行为:

提款机
类名:Atm
属性:用户列表
行为:开户 查询 取款 存款 转账 改密 锁定 解锁 补卡 销户
'''
from admin import Admin
from atm import Atm
import time
import os
import pickle

def main():
    admin = Admin()
    #开机页面
    admin.sysAdmin()
    if admin.optionAdmin():
        return -1
    try:
        with open('allUsers.txt','rb') as f:
            allUsers = pickle.load(f)
        atm = Atm(allUsers)
    except FileNotFoundError as e:
        allUsers = {}
        atm = Atm(allUsers)
    while True:
        admin.sysFunAdmin()
        option = input('请选择您的操作:')
        if option == '1':
            atm.createUser()
        if option == '2':
            atm.searchUserInfo()
        if option == '3':
            atm.getMoney()
        if option == '4':
            atm.saveMoney()
        if option == '5':
            atm.transferMoney()
        if option == '6':
            atm.changePasswd()
        if option == '7':
            atm.lockUser()
        if option == '8':
            atm.unlockUser()
        if option == '9':
            atm.newCard()
        if option == '0':
            atm.killUser()
        if option == 't':
            #退出银行系统时保存所有用户信息
            if not admin.optionAdmin():
                filePath = os.path.join(os.getcwd(),'allUsers.txt')
                with open(filePath, 'wb') as f1:
                    pickle.dump(atm.allUsers,f1)
                print('您已退出银行系统...')
                return -1
        time.sleep(2)


if __name__ == '__main__':
    main()

管理员模块 admin.py

import time
class Admin(object):
    admin = '1'
    passwd = '1'

    #开机页面
    def sysAdmin(self):
        print('*********************************************')
        print('**                                         **')
        print('**              欢迎登录龙哥银行             **')
        print('**  用龙哥银行                   保倾家荡产  **')
        print('**                                         **')
        print('*********************************************')

    def sysFunAdmin(self):
        print('**********************************************')
        print('*     开户(1)                   查询(2)    *')
        print('*     取款(3)                   存款(4)    *')
        print('*     转账(5)                   改密(6)    *')
        print('*     锁定(7)                   解锁(8)    *')
        print('*     补卡(9)                   销户(0)    *')
        print('*                  退出(t)                  *')
        print('**********************************************')

    def optionAdmin(self):
        inputAdmin = input('请输入管理员账号: ')
        if inputAdmin != self.admin:
            print('管理员账号输入错误...')
            return -1
        inputPasswd = input('请输入管理员密码: ')
        if inputPasswd != self.passwd:
            print('管理员密码输入错误...')
            return -1
        print('操作成功,请稍后...')
        time.sleep(2)
        return 0

卡模块 card.py

class Card(object):
    def __init__(self,cardId,passwd,money):
        self.cardId = cardId
        self.passwd = passwd
        self.money = money
        self.lock = False

人模块 user.py

class User(object):
    def __init__(self,name,idCard,phoneNumber,card):
        self.name = name
        self.idCard = idCard
        self.phoneNumber = phoneNumber
        self.card = card

取款机模块 atm.py

import random
from user import User
from card import Card
class Atm(object):
    def __init__(self,allUsers):
        self.allUsers = allUsers

    #开户
    def createUser(self):
        name = input('请输入您的姓名:')
        idCard = input('请输入您的身份证号:')
        phoneNumber = input('请输入您的手机号:')
        moneyCount = int(input('请输入存款金额:'))
        passwd = input('请设置您的密码:')
        if not self.checkPasswd(passwd):
            print('密码连续三次输入错误,开户失败...')
            return -1
        cardId = self.randomcardId()
        print('开户成功!账号为:%s  余额为:%d'%(cardId,moneyCount))
        card = Card(cardId,passwd,moneyCount)
        user = User(name,idCard,phoneNumber,card)
        self.allUsers[cardId] = user

    #查询
    def searchUserInfo(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            #查询
            print('当前账号%s,余额%d'%(user.card.cardId,user.card.money))

    #取款
    def getMoney(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            #取款
            money = int(input("请输入取款金额:"))
            if money > user.card.money:
                print('余额不足...')
                return -1
            if money < 0:
                print('取款金额不可为负...')
                return -1
            user.card.money -= money
            print('账户余额为:%d'%(user.card.money))

    #存款
    def saveMoney(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            # 存款
            money = int(input("请输入存款金额:"))
            if money < 0:
                print('存款金额不可为负...')
                return -1
            user.card.money += money
            print('账户余额为:%d' % (user.card.money))

    #转账
    def transferMoney(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            # 转账
            while True:
                cardId = input('请输入对方的账号:')
                user2 = self.allUsers.get(cardId)
                # 验证账号是否存在
                if not user2:
                    print('该账号不存在,请重新输入...')
                else:
                    break
            money = int(input("请输入转账金额:"))
            if money > user.card.money:
                print('余额不足...')
                return -1
            if money < 0:
                print('转账金额不可为负...')
                return -1
            print('对方账户信息--->卡号:%s, 姓名:%s, 转账金额:%d'%(cardId,user2.name,money))
            receiveInfo = input('请确认信息是否正确,正确输入“Y”,错误输入“N”: ')
            if receiveInfo != "Y":
                print('信息错误,停止转账...')
                return -1
            user.card.money -= money
            user2.card.money += money
            print('转账成功,账户余额为:%d' % (user.card.money))

    #改密
    def changePasswd(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            passwd = input('请设置您的新密码:')
            checkPasswd = input('请确认您的新密码:')
            if passwd != checkPasswd:
                print("两次密码不一致,新密码设置失败...")
                return -1
            user.card.passwd = passwd
            print('新密码设置成功,请牢记新密码...')

    #锁定
    def lockUser(self):
        cardId = input('请输入您的账号:')
        user = self.allUsers.get(cardId)
        # 验证账号是否存在
        if not user:
            print('该账号不存在...')
            return -1
        if user.card.lock:
            print('该卡已被锁定,请解锁后再使用...')
            return -1
        # 验证密码是否正确
        if not self.checkPasswd(user.card.passwd):
            print('密码输入错误,操作失败...')
            return -1
        #锁定
        user.card.lock = True
        print('该卡已被锁定,请解锁后再使用...')



    #解锁
    def unlockUser(self):
        cardId = input('请输入您的账号:')
        user = self.allUsers.get(cardId)
        # 验证账号是否存在
        if not user:
            print('该账号不存在...')
            return -1
        # 判断账户是否被锁定
        if not user.card.lock:
            print('该卡未被锁定,无需解锁...')
            return -1
        # 验证密码是否正确
        if not self.checkPasswd(user.card.passwd):
            print('密码输入错误,操作失败...')
            return -1
        #解锁
        user.card.lock = False
        print("解锁成功...")

    #补卡
    def newCard(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            name = input("请输入您的姓名:")
            if name != user.name:
                print('姓名错误,补卡失败...')
                return -1
            idCard =input('请输入您的身份证号:')
            if idCard != user.idCard:
                print('身份证号错误,补卡失败...')
                return -1
            newCardId = self.randomcardId()
            #修改Card中卡号为新卡号
            user.card.cardId = newCardId
            #替换字典中的键——卡号,保留值
            self.allUsers[newCardId] = self.allUsers.pop(cardId)
            print('补卡成功,新卡号为%s...'%(newCardId))

    #销户
    def killUser(self):
        cardId = self.checkIdAndWd()
        if cardId:
            user = self.allUsers[cardId]
            name = input("请输入您的姓名:")
            if name != user.name:
                print('姓名错误,销户失败...')
                return -1
            idCard = input('请输入您的身份证号:')
            if idCard != user.idCard:
                print('身份证号错误,销户失败...')
                return -1
            self.allUsers.pop(cardId)
            print('销户成功......')


    #验证密码
    def checkPasswd(self,realPasswd):
        for i in range(3):
            passwd = input('请输入您的密码:')
            if realPasswd == passwd:
                return True
        return False
    #生成不会和已有账户重复的6位账号
    def randomcardId(self):
        while True:
            st = ''
            for i in range(6):
                ch = str(random.randrange(0,10))
                st += ch
            if not self.allUsers.get(st):
                return st

    #验证账号和密码是否正确
    def checkIdAndWd(self):
        cardId = input('请输入您的账号:')
        user = self.allUsers.get(cardId)
        # 验证账号是否存在
        if not user:
            print('该账号不存在...')
        #判断账户是否被锁定
        elif user.card.lock:
            print('该卡已被锁定,请解锁后再使用...')
        # 验证密码是否正确
        elif not self.checkPasswd(user.card.passwd):
            user.card.lock = True
            print('该卡已被锁定,请解锁后再使用...')
        else:
            return cardId

 

你可能感兴趣的:(千锋python教学视频中的银行系统实战)