Bank Account Program, Change Balance

11.3k views Asked by At

I'm trying to get the balance to change in the account after a withdraw but it just stays at 10.00. I don't know how to properly apply a method in SavingsAccount that will change it. I tried but had no success.

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

    public void deposit(double ammount) {
        balance = balance + ammount;
    }
}

public class SavingsAccount extends Account {
    public SavingsAccount(int iD, double balancE) {
        super(iD, balancE);
    }

    @Override
    public void withdraw(double amount) {
        if (amount > getBalance()) {
            System.out.println("Current Balance: " + getBalance() 
                    + "\nThe             withdrawal  cannot be made due to insufficient  funds.");
        }

    }
}



public class Test extends Account {

    public static void main(String[] args) {
        SavingsAccount a1 = new SavingsAccount(1122, 10.00);
        a1.withdraw(5.00);
        a1.deposit(00.00);
        a1.setAnnualInterestRate(4.5);
        Date dat = new Date();

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

    }
}
3

There are 3 answers

0
Akash Magoon On

try:

public void deposit(double ammount) {
        balance = balance + ammount;
        setBalance(balance);
    }

You should call setBalance at the end of withdraw and balance, passing in the new balance amount.

0
Mike Nitchie On

In withdraw(), you are only checking the condition in which the withdrawal amount is greater than the balance, then printing a message. You must also handle the case where it is a legal amount to withdraw.

@Override
public void withdraw(double amount) {
    if (amount > getBalance()) {
        System.out.println("Current Balance: " + getBalance() 
                + "\nThe             withdrawal  cannot be made due to insufficient  funds.");
    } else {
        super.withdraw(amount);
    }

}
0
Francisco Romero On

You have to use your setBalance method in your public void withdraw method. Like this:

public void withdraw(double amount) {
        if (amount > getBalance()) {
            System.out.println("Current Balance: " + getBalance() + "\nThewithdrawal  cannot be made due to insufficient  funds.");
        }
         else
         {
            setBalance(getBalance() - amount);
         }

Now it will sets to your balance variable your new value because if you withdraw, you will have the balance-amount money in your account.

I expect it will be helpful for you!