-1

我正在尝试应用一种方法来检查帐户是否已超过OVERDRAFT_LIMIT. 我尝试了几件事,但没有成功。

这是我做过但不知道如何应用它的事情:

public void testForOverdraft(double balancE)
{
    if(balancE <= 0 && balancE >= OVERDRAFT_LIMIT)
    {
        withdraw(10);
        System.out.println("The account is overdrawn.");
    }
    else if(balancE <= 0 && balancE <= OVERDRAFT_LIMIT)
    {
        withdraw(20);
        System.out.println("The account is locked until a deposit is made to bring the account up to a positive value."); 
    }
}

帐户:

import java.util.Date;

public class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    private double monthlyInterestRate;

    public Account()
    {
        id = 0;
        balance = 0;
        annualInterestRate = 0;
    }
    public Account(int iD, double balancE)
    {
        id = iD;
        balance = balancE;
    }
    public void setID(int iD)
    {
        id = iD;
    }
    public int getID()
    {
        return(id);
    }
    public void setBalance(double balancE)
    {
        balance = balancE;
    }
    public double getBalance()
    {
        return(balance);
    }
    public void setAnnualInterestRate(double AIR)
    {
        annualInterestRate = AIR;
    }
    public double getAnnualInterestRate()
    {
        return(annualInterestRate);
    }
    public void setDateCreated(Date dateCreated)
    {
        this.dateCreated = dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return((annualInterestRate / 100) / 12);
    }
    public double getMonthlyInterest()
    {
        return(balance * monthlyInterestRate);
    }
    public void withdraw(double ammount)
    {
        balance = balance - ammount;
        setBalance(balance);
    }
    public void deposit(double ammount) {
        balance = balance + ammount;
        setBalance(balance);
    }
}

支票账户:

public class CheckingAccount extends Account
{
    final static double OVERDRAFT_LIMIT = -50.00;
    private double annualInterest;

    public CheckingAccount()
    {
        super(); 
    }
    public CheckingAccount(int iD, double balancE)
    {
        super(iD, balancE);
    }
    public double getAnnualInterest()
    {
        return((getBalance() * getAnnualInterestRate()) / 100);
    }
}

测试:

public class Test extends CheckingAccount
{
    public static void main(String [] args)
    {
        CheckingAccount a1 = new CheckingAccount(1122, 15.00);
        a1.withdraw(5.00);
        a1.deposit(00.00);
        a1.setAnnualInterestRate(4.5);
        Date dat = new Date();

        System.out.println("Balance: " + 
                "\nMonthly Interest: " + a1.getMonthlyInterest() + 
                "\nDate Created: " + dat);

    }
}
4

1 回答 1

0

在提取方法中分配之前检查新余额

如果成功,withdraw 方法将返回 true

public boolean withdraw(double ammount)
{
    boolean success ;
    double aux ;

    aux = balance - ammount ;

    if(aux < OVERDRAFT_LIMIT)
    {
        success = false ;
    }
    else 
    {
        setBalance(aux);
        success = true ;
    }

    return success ;
}

Then to use it just go like this 

if(!withdraw(60))
{
    System.out.println("The account is locked until a deposit is made to bring the account up to a positive value."); 
}
于 2015-06-16T13:50:13.160 回答