//*******************************************************************************************************
//  上面的代码是已知的,我主要是实现下面的代码,主要是练习使用继承  
//
class BankAccount
{

protected double accountNumber;
protected String name;
protected double balance;
private static int accountCount = 1; // Start account numbers at 1. public BankAccount(String ownersName, double openingBalance)
{
accountNumber = accountCount++;
name=ownersName;
balance=openingBalance;
} public String getName()
{
return name;
}
public double getBalance()
{
return balance;
}

public void withdraw(double openingBalance)
{
balance-=openingBalance;
System.out.println("balance = "+balance);
}
// Deposit an amount to this account
public void deposit(double openingBalance)
{
balance+=openingBalance;
System.out.println("balance = "+balance);
}
// This method returns a String representing the state of this account.
public String toString(BankAccount record)
{
// create a string to return here.
return "User: "+record.getName()+"\nBalance: "+record.getBalance();
}
}class OverdraftAccount extends BankAccount
{
private double aLimit;

public OverdraftAccount(String ownersName, double openingBalance)
{
super(ownersName,openingBalance);
}

// Constructor with 3 parameters
public OverdraftAccount(String ownersName, double openingBalance, double aLimit)
{
super(ownersName,openingBalance);
this.aLimit=aLimit;
}

public double getALimit()
{
return aLimit;
} // Override methods here.
public void withdraw(double openingBalance)
{
double balance=super.getBalance();
System.out.println("balance = "+balance);
if(balance<aLimit)
System.out.println("You have made a over withdraw!Stop it!");
}}

解决方案 »

  1.   

    没有哇,楼主的代码虽然有些凌乱,但思路还算清晰,
    是大家没有耐心看吧!//测试数据:
    /*1。Name=Stone,Opening Balance=1000
    然后Add Account,点选账号,Amount=500,
    然后Withdraw,则屏幕显示balance=500
    Amount=501,然后Withdraw,则屏幕显示
    帐户为非透支帐户,帐上金额不足。
      2。Name=Bill,Opening Balance=1000,Overdraft limit=200
    然后Add Overdraft Account,点选账号,Amount=1000,
    然后Withdraw,则屏幕显示balance=0
    Amount=200,Withdraw,则屏幕显示-200
    Amount=200,Deposit,则屏幕显示0
    Amount=201,Withdraw,则提示
    帐户为可透支帐户,但已经超出可透支额200元。
    *//*
    其它代码不用改,就改了父类BankAccount和其子类OverdraftAccount类
    里的withdraw(double)方法,另外为了实现透支账号取款透支的问题,添加了一个
    保护权限的方法substract(double)方法,这样既能实现功能,又起到了
    保护的作用,外部调用的接口又不用改变。以下是两个修改过的类代码:*/
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    class BankAccount
    {

    protected double accountNumber;
    protected String name;
    protected double balance;
    private static int accountCount = 1; // Start account numbers at 1. public BankAccount(String ownersName, double openingBalance)
    {
    accountNumber = accountCount++;
    name=ownersName;
    balance=openingBalance;
    } public String getName()
    {
    return name;
    }
    public double getBalance()
    {
    return balance;
    }


    protected void subtract(double openingBalance)
    {
    balance-=openingBalance;
    }

    public void withdraw(double openingBalance)
    {
    //*******************************
    if(openingBalance<=balance)
    {
    subtract(openingBalance);
    System.out.println("balance = "+balance);
    }
    else
    {
    System.out.println("您的账户上没有足够的资金来取,您是非透支账户,不能透支!");
    }
    //*******************************
    }
    // Deposit an amount to this account
    public void deposit(double openingBalance)
    {
    balance+=openingBalance;
    System.out.println("balance = "+balance);
    }
    // This method returns a String representing the state of this account.
    public String toString(BankAccount record)
    {
    // create a string to return here.
    return "User: "+record.getName()+"\nBalance: "+record.getBalance();
    }
    }
    class OverdraftAccount extends BankAccount
    {
    private double aLimit;

    public OverdraftAccount(String ownersName, double openingBalance)
    {
    super(ownersName,openingBalance);
    }

    // Constructor with 3 parameters
    public OverdraftAccount(String ownersName, double openingBalance, double aLimit)
    {
    super(ownersName,openingBalance);
    this.aLimit=aLimit;
    }

    public double getALimit()
    {
    return aLimit;
    } // Override methods here.
    public void withdraw(double openingBalance)
    {
    double balance=super.getBalance();
    //****************************************************
    if(openingBalance>(balance+aLimit))
    {
    System.out.println("您拥有可透支账户,但是你要取的金额已经超出了允许的\""+aLimit+"\"元透支金额!");
    }else
    {
    subtract(openingBalance);
    System.out.println("balance = "+super.getBalance());
    }
    //****************************************************
    }
    }