-1

I'm trying to apply a method that will check if the account has went over the OVERDRAFT_LIMIT. I have tried several things but haven't succeeded.

This was something I did but did not know how to apply it:

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."); 
    }
}

Account:

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);
    }
}

CheckingAccount:

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);
    }
}

Test:

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);

    }
}
Ram
  • 3,092
  • 10
  • 40
  • 56
  • `balancE <= 0 && balancE >= OVERDRAFT_LIMIT` - is that ever possible? Think on it and you should have the fix. – sparc_spread Jun 16 '15 at 13:49
  • 4
    @sparc_spread It would make sense if he stores OVERDRAFT_LIMIT as negative. – R Quijano Jun 16 '15 at 13:51
  • Now that I think of the meaning of overdraft limit, you are correct. – sparc_spread Jun 16 '15 at 13:55
  • Updated tags and formatting. Good question for a first time user +1 – Ram Jun 19 '15 at 03:27
  • 1
    Title give no clues on what you are asking. Lots of unnecessary codes that have nothing to do for you question. You haven't even mention the purpose of this code: if it is for real life system, everything is done wrong. You should at least mention it is just for your study etc – Adrian Shum Jun 19 '15 at 04:18

1 Answers1

0

Check the new balance before assinging it in the withdraw method

The withdraw method will return true if it was success full

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."); 
}
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99