class Account:
    def __init__(self, name, password, balance=0):
        self.name = name
        self.password = password
        self.balance = balance
        self.status = 'normal'

    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            print(f'您已成功存入 {amount} 元。')
        else:
            print('无效的存款金额。')

    def withdraw(self, amount):
        if amount > 0 and self.balance >= amount:
            self.balance -= amount
            print(f'您已成功取出 {amount} 元。')
        elif amount <= 0:
            print('无效的取款金额。')
        else:
            print('余额不足。')

    def query_balance(self):
        print(f'账户 {self.name} 的余额为 {self.balance} 元。')

    def lose_card(self):
        if self.status == 'normal':
            self.status = 'lost'
            print('您的卡片已挂失。')
        else:
            print('您的卡片已处于挂失状态。')

    def find_card(self):
        if self.status == 'lost':
            self.status = 'normal'
            print('您的卡片已取消挂失。')
        else:
            print('您的卡片未处于挂失状态。')

    def close_account(self):
        if self.status == 'normal':
            self.status = 'closed'
            print('您的账户已关闭。')
        else:
            print('您的账户不能关闭,请先处理卡片状态。')


def main():
    accounts = load_accounts('accounts.txt')  # 从文件中加载账户信息
    while True:
        print('欢迎来到银行')
        if accounts:
            print('1. 登录')
        print('2. 开立账户')
        print('3. 退出')
        choice = input('请输入您的选择: ')
        if choice == '1':
            name = input('请输入您的姓名: ')
            password = input('请输入您的密码: ')
            account = accounts.get(name)
            if account and account.password == password:
                print('登录成功!')
                while True:
                    print('1. 存款')
                    print('2. 取款')
                    print('3. 查询余额')
                    print('4. 挂失')
                    print('5. 取消挂失')
                    print('6. 销户')
                    print('7. 退出账户')
                    operation = input('请输入您的操作: ')
                    if operation == '1':
                        amount = int(input('请输入要存入的金额: '))
                        account.deposit(amount)
                    elif operation == '2':
                        amount = int(input('请输入要取出的金额: '))
                        account.withdraw(amount)
                    elif operation == '3':
                        account.query_balance()
                    elif operation == '4':
                        account.lose_card()
                    elif operation == '5':
                        account.find_card()
                    elif operation == '6':
                        account.close_account()
                        del accounts[name]
                    elif operation == '7':
                        break  # 退出账户
                    else:
                        print('无效的操作')
            else:
                print('用户名或密码错误!')
        elif choice == '2':
            name = input('请输入您的姓名: ')
            password = input('请输入您的密码: ')
            if name not in accounts:
                account = Account(name, password)
                accounts[name] = account
                print('您的账户已开通')
            else:
                print('账户已存在。')
        elif choice == '3':
            save_accounts(accounts, 'accounts.txt')  # 保存账户信息到文件
            break  # 退出程序
        else:
            print('无效的选择')

def load_accounts(filename):
    accounts = {}
    try:
        with open(filename, 'r') as f:
            for line in f:
                name, password, balance, status = line.strip().split(',')
                account = Account(name, password, int(balance))
                account.status = status
                accounts[name] = account
        return accounts
    except FileNotFoundError:
        return {}

def save_accounts(accounts, filename):
    with open(filename, 'w') as f:
        for name, account in accounts.items():
            f.write(f'{name},{account.password},{account.balance},{account.status}
')

if __name__ == '__main__':
    main()
Python 银行账户系统:完善安全性和功能

原文地址: http://www.cveoy.top/t/topic/oSSh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录