ATM小程序

# 在单入口模式下进行

 

# 卡类模块

class Card():
    def __init__(self,cardid,password,money):
        self.cardid = cardid
        self.password = password
        self.money = money
        self.islock = False

  

# 人类模块

class Person():
    def __init__(self,name,userid,phone,card):
        self.name = name
        self.userid = userid
        self.phone = phone
        self.card = card

  

# 初始化视图模块

import time

class View():

    def login():
        name = input("请输入管理员的账户:")
        pwd = input("请输入管理员密码:")
        if name == "1" and pwd == "1":
            View.welcome_view()
            time.sleep(0.2)
            View.operation_view()
            return True
        else:
            print("管理员账号或密码不正确")

    def welcome_view():
        print("*******************************************")
        print("*                                         *")
        print("*                                         *")
        print("*         Welcome To OldBoy Bank          *")
        print("*                                         *")
        print("*                                         *")
        print("*******************************************")

    def operation_view():
        print("*******************************************")
        print("*           开户(1)    查询(2)             *")
        print("*           存钱(3)    取钱(4)             *")
        print("*           转账(5)    改密(6)             *")
        print("*           锁卡(7)    解卡(8)             *")
        print("*           补卡(9)    退出(0)             *")
        print("*******************************************")

  

 

# 操作类模块


import os,pickle,re,time,random
from .card import Card
from .person import Person

class Operation():

    # 初始化读取数据
    def __init__(self):
        self.read_user()
        self.resd_userid()
    def read_user(self):
        # 卡号:用户信息
        if os.path.exists("user.txt"):
            with open("user.txt",mode="rb") as f1:
                self.user_dict = pickle.load(f1)
        else:self.user_dict = {}
    def resd_userid(self):

        # 身份证号:卡号
        if os.path.exists("userid.txt"):
            with open("userid.txt",mode="rb") as f2:
                self.userid_dict = pickle.load(f2)
        else:self.userid_dict = {}
    # 开户
    def register(self):
        print(self.user_dict)
        print(self.userid_dict)
        name = self.name_func()
        # 获取身份证号
        userid = self.uesrid_func()
        # 获取手机号
        phone = self.phone_func()
        # 获取密码
        password = self.pwd_func()
        # 获取卡号
        cardid = self.quth_code()
        c = Card(cardid, password, 10)
        p = Person(name, userid, phone, c)
        self.save()
        print("恭喜{}开卡成功,卡号为:{},余额为:{}".format(name,cardid,c.money))

        self.user_dict[cardid] = p
        self.userid_dict[userid] = cardid

    def name_func(self):
        while True:
            data = input("请输入用户名:")
            res = re.findall(r"\W",data)
            if not res :
                if data.strip() and (not (" " in data)):
                    return data
                else:print("存在非法字符")
            else:
                print("存在非法字符")
    def uesrid_func(self):
        while True:
            data = input("请输入身份证号:")
            if re.search("[1-9]\d{5}(?:19\d{2}|20[0-2][0-9])(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\d|3[01])\d{3}[\dx]",data):
                if data not in self.userid_dict:
                    return data
                else:print("该身份证号在我行已办理过银行卡")
            else:
                print("身份证号不符")
    def phone_func(self):
        while True:
            data = input("请输入手机号:")
            if re.search("0?(13|14|15|17|18|19)[0-9]{9}",data):
                return data
            else:
                print("手机号不符")
    def pwd_func(self):
        while True:
            data1 = input("请输入新的密码(六位):")
            if len(data1) == 6 :
                res = re.search(r"^[\da-zA-Z\_]{6}$",data1)
                if res:
                    data2 = input("请再次确认密码:")
                    if data1 == data2:
                        return data2
                    else:
                        print("俩次密码不一致,请重新输入!")
                else:print("存在非法字符")
            else:print("输入错误!")
    def quth_code(self):
        while True:
            code = str(random.randrange(100000, 1000000))
            if code not in self.user_dict:
                return code

    # 公共方法,查看卡号有没有被锁,核对密码
    def name_pwd(self,data):
        if data in self.user_dict:
            if not(self.user_dict[data].card.islock):
                n = 0
                while n <= 2:
                    pwd = input("请输入密码:")
                    if self.user_dict[data].card.password == pwd :
                        return True
                    elif n == 2 :
                        self.user_dict[data].card.islock = True
                        print("错误次数太多,此卡号已被锁定")
                        sign = False
                        return sign
                    else:
                        n += 1
                        print("密码错误")
            else:print("卡号已被锁定,请联系管理员")
        else:
            print("卡号不存在")
            return False
    # 查询
    def query(self):
        while True :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" : break
            if self.name_pwd(data):
                if not(self.user_dict[data].card.islock):
                    self.save()
                    print("您的余额为:{}".format(self.user_dict[data].card.money))
                    break
                else:print("卡号已被锁定")
    # 充值
    def save_money(self):
        sign = True
        while sign :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" : break
            while True:
                rmb = input("请输入充值人民币数量:")
                if rmb.isdecimal():
                    self.user_dict[data].card.money += int(rmb)
                    self.save()
                    print("您的卡号:{},充值成功:{}元,余额为:{}".format(self.user_dict[data].card.cardid,rmb, self.user_dict[data].card.money))
                    sign = False
                    break
                else:
                    print("输入有误,请重新输入")
    # 取钱
    def get_money(self):
        sign = True
        while sign :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" : break
            if self.name_pwd(data):
                if not(self.user_dict[data].card.islock):
                    while True:
                        rmb = input("请输入取出人民币数量:")
                        if rmb.isdecimal():
                            if self.user_dict[data].card.money >= int(rmb):
                                self.user_dict[data].card.money -= int(rmb)
                                self.save()
                                print("您的卡号:{},取款成功:{}元,余额为:{}".format(self.user_dict[data].card.cardid,rmb, self.user_dict[data].card.money))

                                sign = False
                                break
                            else:
                                print("余额不足")
                        else:
                            print("输入有误,请重新输入")
                else:print("卡号已被锁定")
    # 转账
    def trans_money(self):
        sign = True
        while sign :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" : break
            if self.name_pwd(data):
                if not (self.user_dict[data].card.islock):
                    while sign:
                        cd = input("请输入转账人卡号:")
                        if self.user_dict[data].card.cardid == cd:
                            print("不能给自己转账的哦")
                            continue
                        elif cd in self.user_dict:
                            rmb = input("请输入转账人民币数量:")
                            if self.user_dict[data].card.money >= int(rmb):
                                self.user_dict[data].card.money -= int(rmb)
                                self.user_dict[cd].card.money += int(rmb)
                                self.save()
                                print("您的卡号:{},给{}的卡号:{}转账{}元成功,您的余额为:{}".format(self.user_dict[data].card.cardid,self.user_dict[cd].name,self.user_dict[cd].card.cardid,rmb, self.user_dict[data].card.money))

                                break
                            else:print("余额不足")
                        else:
                            data = input("转账人卡号不存在,返回请按1,继续请按任意键!")
                            if data == "1" :
                                sign = False

                else:print("卡号已被锁定")
    # 修改密码
    def change_pwd(self):
        sign = True
        while sign :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" :
                sign = False
                break
            if self.name_pwd(data):
                if not(self.user_dict[data].card.islock):
                    while True:
                        choice = input("通过何种方式来修改密码\n1:身份证号码  2:密码")
                        if choice == "1":
                            sfz = input("请输入身份证号码来修改密码:")
                            if self.user_dict[data].userid == sfz:
                                res = self.pwd_func()
                                self.user_dict[data].card.password = res
                                self.save()
                                print("您的卡号{}修改密码成功,请退出保存后重新登录!".format(self.user_dict[data].card.cardid))

                                sign = False
                                break
                            else:
                                print("身份证号码错误")
                        elif choice == "2":
                            pwd = input("请输入卡密码来修改密码:")
                            if self.user_dict[data].card.password == pwd:
                                res = self.pwd_func()
                                self.user_dict[data].card.password = res
                                self.save()
                                print("您的卡号{}修改密码成功,请退出保存后重新登录!".format(self.user_dict[data].card.cardid))
                                sign = False
                                break
                        else:print("输入有误,重新选择!")
                else:print("卡号已被锁定")
    # 锁卡
    def lock(self):
        sign = True
        while sign:
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1":
                sign = False
                break
            elif data in self.user_dict:
                if self.user_dict[data].card.islock:print("此卡已冻结!")
                elif not(self.user_dict[data].card.islock):
                    while True:
                        num = input("通过何种方式来冻结卡片\n1:身份证号码  2:密码")
                        if num == "1":
                            sfz = input("请输入身份证号码来冻结卡片:")
                            if self.user_dict[data].userid == sfz:
                                self.user_dict[data].card.islock = True
                                self.save()
                                print("您的卡号{}冻结成功,请退出保存后继续操作!".format(self.user_dict[data].card.cardid))
                                sign = False
                                break
                            else:
                                print("身份证号码错误")
                        elif num == "2":
                            pwd = input("请输入卡密码来冻结卡片:")
                            if self.user_dict[data].card.password == pwd:
                                self.user_dict[data].card.islock = True
                                self.save()
                                print("您的卡号{}冻结成功,请退出保存后继续操作!".format(self.user_dict[data].card.cardid))

                                sign = False
                                break
                        else:
                            print("输入有误,重新选择!")
            else:
                print("输入有误,重新选择!")
    # 解卡
    def unlock(self):
        sign = True
        while sign :
            data = input("请输入卡号(返回上一层请按1):")
            if data == "1" :break
            elif data in self.user_dict:
                if not(self.user_dict[data].card.islock):print("此卡并没有冻结!")
                elif self.user_dict[data].card.islock:
                    while True:
                        num = input("通过何种方式来解锁卡片\n1:身份证号码  2:密码")
                        if num == "1":
                            sfz = input("请输入身份证号码来解锁卡片:")
                            if self.user_dict[data].userid == sfz:
                                self.user_dict[data].card.islock = False
                                self.save()
                                print("您的卡号{}解冻成功,请退出保存后重新登录!".format(self.user_dict[data].card.cardid))

                                sign = False
                                break
                            else:
                                print("身份证号码错误")
                        elif num == "2":
                            pwd = input("请输入卡密码来解锁卡片:")
                            if self.user_dict[data].card.password == pwd:
                                self.user_dict[data].card.islock = False
                                self.save()
                                print("您的卡号{}解冻成功,请退出保存后重新登录!".format(self.user_dict[data].card.cardid))

                                sign = False
                                break
                        else:print("输入有误,重新选择!")
            else:print("输入有误,重新选择!")
    # 补卡
    def new_card (self):
        while True:
            sfz = input("请输入身份证号码来补卡(返回上一层请按1):")
            if sfz == "1": break
            if sfz in self.userid_dict:
                old_cardid = self.userid_dict[sfz]
                new_cardid = self.quth_code()

                self.user_dict[new_cardid] = self.user_dict[old_cardid]

                self.userid_dict[sfz] = new_cardid
                self.user_dict.pop(old_cardid)
                self.save()
                print("补卡成功,新的卡号为{}".format(new_cardid))
                break
            else:
                print("身份证号码错误,请重新输入")
    # 保存退出
    def save(self):
        self.jindutiao()
        print()
        with open("user.txt",mode="wb") as f1:
            pickle.dump(self.user_dict,f1)

        with open("userid.txt",mode="wb") as f2:
            pickle.dump(self.userid_dict,f2)
    # 进度条
    def jindutiao(self):
        n = 0
        print("请稍后......")
        while n <= 50:
            time.sleep(0.01)
            print("\r[%-50s]%2d%%" % ("*" * n, 2 * n), end="")
            n += 1

  

 

# 主程序模块


from package.view import View
from package.card import Card
from package.person import Person
from package.operation import Operation

class Main:
    @staticmethod
    def run():
        if View.login():
            o = Operation()
            while True:
                num = input("请选择需要办理的业务:")
                if num == "1" :
                    o.register()
                elif num == "2" :
                    o.query()
                elif num == "3" :
                    o.save_money()
                elif num == "4" :
                    o.get_money()
                elif num == "5":
                    o.trans_money()
                elif num == "6":
                    o.change_pwd()
                elif num == "7":
                    o.lock()
                elif num == "8":
                    o.unlock()
                elif num == "9":
                    o.new_card()
                elif num == "0":
                    o.save()
                    break

if __name__ == "__main__" :
    Main.run()

  

posted @ 2020-08-12 07:58  bajie_new  阅读(101)  评论(0编辑  收藏  举报