【学习任务】面向对象实现银行账户的资金交易管理
我要参与
【学习任务】面向对象实现银行账户的资金交易管理
学习任务 1.2k
等70人参与
来源: 第7周 / 全能软件测试工程师

小伙伴们,用类和对象实现自己银行账户的资金交易管理吧,包括存款、取款和打印交易详情,交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。
图片描述

小慕提醒:一定要先思考哦,实在没有思路再去看参考答案!!!

图片描述


参考答案

# coding:utf-8

import time

class BankAccount(object):
    def __init__(self, balance):
        self.balance = balance  # 账户初始余额
        print("您的初始余额: {}元".format(self.balance))

    # 存款
    def deposit(self, deposit_time, deposit_money):
        self.balance += deposit_money
        print("存入时间: {}, 存入金额: +{}元, 余额: {}元".format(deposit_time, deposit_money, self.balance))

    # 消费
    def withdraw(self, withdraw_time, withdraw_money):
        self.balance -= withdraw_money
        print("消费时间: {}, 消费金额: -{}元, 余额: {}元".format(withdraw_time, withdraw_money, self.balance))


if __name__ == '__main__':
    xiaomu = BankAccount(800)
    xiaomu.deposit(time.strftime('%Y-%m-%d %H:%M:%S'), 1000)
    time.sleep(1)
    xiaomu.deposit(time.strftime('%Y-%m-%d %H:%M:%S'), 5500)
    time.sleep(2)
    xiaomu.withdraw(time.strftime('%Y-%m-%d %H:%M:%S'), 1200)
去发布

登录后即可发布作业,立即

我的作业

全部作业

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师