测试类
public class TEST {
public static void main(String[] args) {
CheckAccount a=new CheckAccount(1122,20000,0.045,5000);
a.withdraw(5000);
System.out.println("账户余额为"+a.getBalance()+"元,"+"可透支余额为"+a.getOverdraft()+"元!");
a.withdraw(10000);
System.out.println("账户余额为"+a.getBalance()+"元,"+"可透支余额为"+a.getOverdraft()+"元!");
a.withdraw(3000);
System.out.println("账户余额为"+a.getBalance()+"元,"+"可透支余额为"+a.getOverdraft()+"元!");
}
}
子类
public class Account {
private int id;
private double balance;
private double annuallterestRate;
public Account(int id, double balance, double annuallterestRate) {
this.id = id;
this.balance = balance;
this.annuallterestRate = annuallterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnuallterestRate() {
return annuallterestRate;
}
public void setAnnuallterestRate(double annuallterestRate) {
this.annuallterestRate = annuallterestRate;
}
public double getMonthlyInterest(){
return annuallterestRate;
}
public void withdraw(double amount){
if(amount > balance)
System.out.println("余额不足!");
else
{
balance-=(amount);
System.out.println("成功取出" + amount + "元!");
}
}
public void deposit(double amount){
balance+=amount;
System.out.println("成功存入"+amount+"元!");
}
}
**
父类
**
public class CheckAccount extends Account {
private double overdraft;
public CheckAccount(int id, double balance, double annuallterestRate,double overdraft) {
super(id,balance,annuallterestRate);
this.overdraft = overdraft;
}
public void withdraw(double amount){
if (amount <= getBalance())
{
super.withdraw(amount);
}
else{
if((amount-getBalance())<=overdraft)
{
overdraft-=(getBalance()-amount);
setBalance(0);
}
else
System.out.println("超过可透支余额!");
}
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
}