import decimal
import datetime

class Account:
    def __init__(self, name, password, initial_balance=decimal.Decimal('0.00')):
        self.name = name
        self.password = password
        self.balance = initial_balance
        self.status = 'normal'
        self.transactions = []
        self.interest_rate = decimal.Decimal('0.005')  # 年利率 0.5%
        self.last_interest_calculation = datetime.datetime.now()

    def deposit(self, amount, password):
        if self.check_password(password):
            self.balance += amount
            self.transactions.append({
                'type': 'deposit', 
                'amount': amount, 
                'datetime': datetime.datetime.now()
            })
            print('存款成功!')
        else:
            print('密码错误!')

    def withdraw(self, amount, password):
        if self.check_password(password):
            if self.balance < amount:
                print('余额不足!')
            else:
                self.balance -= amount
                self.transactions.append({
                    'type': 'withdraw', 
                    'amount': amount, 
                    'datetime': datetime.datetime.now()
                })
                print('取款成功!')
        else:
            print('密码错误!')

    def query_balance(self, password):
        if self.check_password(password):
            print('您的余额是:', self.balance)
        else:
            print('密码错误!')

    def lose_card(self, password):
        if self.check_password(password):
            self.status = 'lost'
            print('您的卡片已丢失,请尽快联系客服!')
        else:
            print('密码错误!')

    def find_card(self, password):
        if self.check_password(password):
            self.status = 'normal'
            print('您的卡片已找到!')
        else:
            print('密码错误!')

    def close_account(self, password):
        if self.check_password(password):
            self.status = 'closed'
            print('您的账户已关闭!')
        else:
            print('密码错误!')

    def check_password(self, password):
        return self.password == password

    def calculate_interest(self):
        now = datetime.datetime.now()
        days_passed = (now - self.last_interest_calculation).days
        if days_passed >= 30:  # 每月计算一次利息
            interest = self.balance * self.interest_rate * (days_passed / 365)
            self.balance += interest
            self.transactions.append({
                'type': 'interest', 
                'amount': interest, 
                'datetime': datetime.datetime.now()
            })
            self.last_interest_calculation = now

    def get_transaction_history(self):
        return self.transactions

    def change_password(self, old_password, new_password):
        if self.check_password(old_password):
            self.password = new_password
            print('密码修改成功!')
        else:
            print('旧密码错误!')

    def get_account_info(self):
        return {
            'name': self.name,
            'balance': self.balance,
            'status': self.status,
            'transactions': self.transactions,
            'interest_rate': self.interest_rate
        }


def main():
    accounts = {}
    while True:
        print('欢迎来到银行')
        print('1. 开立账户')
        print('2. 存款')
        print('3. 取款')
        print('4. 查询余额')
        print('5. 挂失')
        print('6. 取消挂失')
        print('7. 销户')
        print('8. 修改密码')
        print('9. 查询交易记录')
        print('10. 查询账户信息')
        print('11. 退出')
        choice = input('请输入您的选择: ')

        if choice == '1':
            name = input('请输入您的姓名: ')
            password = input('请输入您的密码: ')
            account = Account(name, password)
            accounts[name] = account
            print('您的账户已开通!')
        elif choice == '2':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                amount = decimal.Decimal(input('输入要存入的金额: '))
                password = input('请输入您的密码: ')
                account.deposit(amount, password)
            else:
                print('账户不存在!')
        elif choice == '3':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                amount = decimal.Decimal(input('请输入提取的金额: '))
                password = input('请输入您的密码: ')
                account.withdraw(amount, password)
            else:
                print('账户不存在!')
        elif choice == '4':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                password = input('请输入您的密码: ')
                account.query_balance(password)
            else:
                print('账户不存在!')
        elif choice == '5':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                password = input('请输入您的密码: ')
                account.lose_card(password)
            else:
                print('账户不存在!')
        elif choice == '6':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                password = input('请输入您的密码: ')
                account.find_card(password)
            else:
                print('账户不存在!')
        elif choice == '7':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                password = input('请输入您的密码: ')
                account.close_account(password)
                del accounts[name]
                print('您的账户已关闭!')
            else:
                print('账户不存在!')
        elif choice == '8':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                old_password = input('请输入旧密码: ')
                new_password = input('请输入新密码: ')
                account.change_password(old_password, new_password)
            else:
                print('账户不存在!')
        elif choice == '9':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                transactions = account.get_transaction_history()
                if transactions:
                    for transaction in transactions:
                        print(f'类型: {transaction['type']}, 金额: {transaction['amount']}, 时间: {transaction['datetime']}')
                else:
                    print('您还没有进行任何交易!')
            else:
                print('账户不存在!')
        elif choice == '10':
            name = input('请输入您的姓名: ')
            account = accounts.get(name)
            if account:
                account_info = account.get_account_info()
                print(f'姓名: {account_info['name']}')
                print(f'余额: {account_info['balance']}')
                print(f'状态: {account_info['status']}')
                print(f'利息率: {account_info['interest_rate']}')
            else:
                print('账户不存在!')
        elif choice == '11':
            break  # 退出程序
        else:
            print('无效的选择!')

        # 定期计算利息
        for account in accounts.values():
            account.calculate_interest()

if __name__ == '__main__':
    main()
Python 实现银行账户系统 -  安全、可靠、功能丰富

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

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