面向对象之-------------------永不停机的ATM

import os
class Account:
    def __init__(self, username, password, money=0):
        self.username = username
        self.password = password
        self.money = money

class ATM:
    def __init__(self):
        self.acc = None # 账户信息. 当登陆成功之后。 self。acc = 当前登陆的账户

    def regist(self):
        print("欢迎来到注册环节")
        username = input("请输入你要注册用户名:")
        password = input("请输入你要注册密码:")

        f = open("atm.txt", mode="r", encoding="utf-8")
        for line in f:
            if line.strip().split("$$")[0] == username: # 用户名重复了
                print("对不起, 用户名重复了")
                return

        acc = Account(username, password)
        # 把账户信息写入到文件中
        f = open("atm.txt", mode="a", encoding="utf-8")
        f.write(acc.username+"$$" + acc.password+"$$"+str(acc.money)+"
")
        f.flush()
        f.close()
        print("注册成功!!!!")

    def login(self):
        print("欢迎进入登陆环节!!")
        username = input("请输入你要注册用户名:")
        password = input("请输入你要注册密码:")
        f = open("atm.txt", mode="r", encoding="utf-8")
        for line in f:
            if line.strip().split("$$")[0] == username and line.strip().split("$$")[1] == password:  # 用户名重复了
                print("登陆成功")
                # 把登陆成功的账户放在atm中
                self.acc = Account(line.strip().split("$$")[0], line.strip().split("$$")[1], int(line.strip().split("$$")[2]))
                break
        else:
            print('登陆失败!!!!')

    def write_to_file(self):
        # 把当前账户里的信息写入到atm.txt
        with open("atm.txt", mode="r", encoding="utf-8") as f1, 
                open("atm.txt_副本", mode="w", encoding="utf-8") as f2:
            for line in f1:
                #  找到当前登陆的账户
                if line.strip().split("$$")[0] == self.acc.username:
                    line = self.acc.username + "$$" + self.acc.password + "$$" + str(self.acc.money) + "
"
                f2.write(line)
        os.remove("atm.txt")
        os.rename("atm.txt_副本", "atm.txt")

    def cun(self):
        if self.acc ==None:
            print("还没有登陆呢, 请登陆")
            return
        print("进入存钱环节")
        money = int(input("请输入你要存的钱数:"))
        self.acc.money += money # 存钱了
        self.write_to_file()
        print("存钱成功")

    def qu(self):
        if self.acc ==None:
            print("还没有登陆呢, 请登陆")
            return
        print("进入取钱环节")
        money = int(input("请输入你要取的钱数:"))
        if self.acc.money < money:
            print("余额不足")
            return
        self.acc.money -= money
        self.write_to_file()
        print("取钱成功.")


    def huikuan(self):
        if self.acc ==None:
            print("还没有登陆呢, 请登陆")
            return
        print("进入汇款环节!")
        target = input("请告诉我对方账号:")
        if target == self.acc.username :
            print("自己不能汇款给自己")
            return

        f = open("atm.txt", mode="r", encoding="utf-8")
        for line in f:
            if line.strip().split("$$")[0] == target:  # 找到了我要汇款的账号
                break
        else:
            print("对不起。 账号不存在")
            return
        f.close()
        # 汇款
        money = int(input("请输入你要汇款的钱数:"))
        if self.acc.money < money:
            print("余额不足")
            return
        else:  # 钱够。 账户也对
            # 先给对方账号加钱
            with open("atm.txt", mode="r", encoding="utf-8") as f1, 
                    open("atm.txt_副本", mode="w", encoding="utf-8") as f2:
                for line in f1:
                    if line.strip().split("$$")[0] == target:  # 找到了我要汇款的账号
                        line = line.strip().split("$$")[0] + "$$" + line.strip().split("$$")[1] + "$$" + str(int(line.strip().split("$$")[2]) + money) + "
"
                    f2.write(line)
            os.remove("atm.txt")
            os.rename("atm.txt_副本", "atm.txt")
            # 自己账号减钱
            self.acc.money -= money
            self.write_to_file()
            print("汇款成功!")

    def chaxun(self):
        if self.acc ==None:
            print("还没有登陆呢, 请登陆")
            return
        print("您的余额是:%s" %  self.acc.money)

    def run(self):
        menu = ("注册", "登陆", "存钱", "取钱", "汇款", "查询余额", "退出")
        while 1:
            for i, m in enumerate(menu, 1):
                print(i, m)
            m = input("请输入你要执行的菜单:")
            if m == "1":
                self.regist()
            elif m == '2':
                self.login()
            elif m == '3':
                self.cun()
            elif m == '4':
                self.qu()
            elif m == "5":
                self.huikuan()
            elif m == "6":
                self.chaxun()
            elif m == "7":
                self.acc = None#永不停机的ATM

ATM().run() # 程序的入口
原文地址:https://www.cnblogs.com/tjp40922/p/9975251.html