对银行卡定义,包括功能有:存款,取款,查询余额,转账功能
(图片上传失败)
学习了extend``implements
public interface IBankAccount {
void payIn(long money);//存款方法
void withdraw(long money);//取款方法
void getBalance();//查询余额方法
}public interface ITransBankAccount extends IBankAccount{
void transferTo(CurrentAccount a);//转账功能
}
public class CurrentAccount implements ITransBankAccount{
private double balance;
@Override
public void payIn(long money) {//存款
balance+=money;
}
@Override
public void withdraw(long money) {//取款
if (balance>=money){
balance-=money;
System.out.println(“取款金额 “+money);
}
else System.out.println(“余额不足”);
}
@Override
public void getBalance() {//查询余额
System.out.println("账户余额为"+balance);
}
@Override
public void transferTo(CurrentAccount a) {//转账
System.out.println("输入转账金额");
Scanner scan=new Scanner(System.in);
double money=scan.nextInt();
if (balance>=money){
balance-=money;
a.balance+=money;
System.out.println("转账给x账户,金额为 "+money);
System.out.println("b账户余额为"+balance);
}
}
@Override
public String toString() {
return "CurrentAccount{" + "balance=" + balance + '}';
}
public static void main(String[] args){
CurrentAccount b=new CurrentAccount();
b.payIn(800);
System.out.println("存款 "+b.balance);
b.withdraw(290);
b.getBalance();
CurrentAccount x=new CurrentAccount();//创建账户名为X 转账两次
b.transferTo(x);
b.transferTo(x);
}
}
转账的时候需要创建新的账户X,由b账户转账到x账户。