接口练习之模拟银行存取款

接口练习——模拟银行存取款
要求:
1、 创建基接口BankAccount,包含 存款方法playIn(),取款方法withdraw(),查询余额方法getBalance()。
2、 创建接口ITransferBankAccount(继承基接口BankAccount),包含转账方法transferTo().
3、 创建类CurrentAccount(实现基接口ITransferBankAccount),类中包含私有数据成员:名字name和余额balance
,并实现存款方法:playIn(),取款方法:withdraw(),查询余额getBanlance(),银行转账方法transferTo(),
重载方法toString()方法,该方法返回银行当前账户中的余额。
4、 主函数中:拥有两个账户:分别为曹操和刘备,(1):曹操存款1000,刘备存款2000。 (2):刘备向曹操转账1元钱,返回两个账户当前的余额。

interface BankAccount{  
    void playIn(int money);  
    void withdraw(int money);  
    int getBalance();  
}  
interface ITransferBankAccount extends BankAccount{  
    void transferTo(CurrentAccount ca,int money);  
}  
class CurrentAccount implements ITransferBankAccount{  

    private String name;  
    private int balance;  

    public CurrentAccount(String name, int balance) {  
        super();  
        this.name = name;  
        this.balance = balance;  
    }  

    @Override  
    public void playIn(int money) {  
        // TODO Auto-generated method stub  
        this.balance = this.balance+money;  
    }  

    @Override  
    public void withdraw(int money) {  
        // TODO Auto-generated method stub  
        this.balance = this.balance-money;  
    }  

    @Override  
    public int getBalance() {  
        // TODO Auto-generated method stub  
        return balance;  
    }  

    @Override  
    public void transferTo(CurrentAccount ca,int money) {  
        // TODO Auto-generated method stub  
        ca.balance = ca.balance+money;  
        this.balance = this.balance-money;  
    }  

}  
public class Test5 {  
    public static void main(String[] args) {  
        CurrentAccount caocao = new CurrentAccount("caocao",0);  
        CurrentAccount liubei = new CurrentAccount("liubei",0);  
        caocao.playIn(1000);  
        liubei.playIn(2000);  
        liubei.transferTo(caocao, 1);;  
        System.out.println("caocao balance :"+caocao.getBalance());  
        System.out.println("liubei balance :"+liubei.getBalance());  
    }  
}  

运行结果:
这里写图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值