Overdraft method for bank account

7.3k views Asked by At

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

    }
}
1

There are 1 answers

0
Mauricio Gracia Gutierrez On

Check the new balance before assinging it in the withdraw method

The withdraw method will return true if it was successful

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

    aux = balance - ammount ;
    possible = (aux >= OVERDRAFT_LIMIT)

    if(doable)
    {
        setBalance(aux);
    }

    return possible ;
}

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