An account has the properties account number, balance, annual,interest rate, and date created, and methods to deposit and withdraw.Create two sub classes for checkings and savings account.Checkings account has an overdraft limit, but a savings account
cannot be overdrawn.

One problem that i'm having is that when I withdraw x amount of money from the balance, the balance doesn't store it's new value.When i try to deposit x amount of money, the deposit isn't added to the existing balance.

The other issue is that after i withdraw 500 from my balance 1000, the balance is -500.
I look over the setwithDraw() and the calculations seem right. I'm confused of why the balance is negative.

Here is the results that I'm getting.

Balance 1000.0
Withdraw 500.0
Checking balance after withdraw: -500.0

Deposited 300.0
Savings balance after deposit: 300.0

Account class

import java.util.Date;
import javax.swing.JOptionPane;

public class Account
{
    int id = 0;
    double balance = 0;
    double annualInterestRate = 0;
    java.util.Date dateCreated = new java.util.Date();

    Account()
    {
        //balance = 1000;
        //balance = b;
        //annualInterestRate = aI;
        //id = i;
       //setbalance(b);
       //setannualInterestRate(aI);
    }


    public void setid(int i)
    {
        id = i;
    }

    public void setbalance(double b)
    {
        balance = b;
    }

    public void setannualInterestRate(double a)
    {
        annualInterestRate = a;
    }

    public int getid()
    {
        return id;
    }

    public double getbalance()
    {
        return balance;
    }

    public double getannualInterestRate()
    {
        return annualInterestRate;
    }

    public String dateCreated()
    {
        String output = "The date created " + dateCreated.toString();

        return output;
    }

    //need to modify
    public double getmonthlyInterestRate()
    {
        return annualInterestRate / 12;
    }

    public void printInfo()
    {

        String output = "Customer id: " + getid() +
                "\nThe monthly interest rate: " + getmonthlyInterestRate() +
                "\nOrginal balance: " + balance +
                "\nThe balance: " + getbalance() +
                "\n" + dateCreated();

        System.out.println(output);
        //JOptionPane.showMessageDialog(null, output);
    }
}

Checkings class

public class Checkings extends Account
{
    double withDraw = 0;

    Checkings()//double a
    {
        //super(a);
        //withDraw = w;
        //setwithDraw(w);
    }

    public void setwithDraw(double w)
    {
        /*if(w > balance)
        {
            System.out.println("Can't withdraw a value greater " +
                    " than your balance!");
            System.exit(0);
        }
        else*/
            withDraw = w;
            balance -= w;
    }

    public double getwithDraw()
    {
        return withDraw;
    }

     public void print()
     {
         String output = "Withdraw " + withDraw +
                 "\nChecking balance after withdraw: " + balance;

        System.out.println(output);
     }
}

Savings Class

public class Savings extends Account
{
    double deposit = 0;

    Savings()//double s
    {
        //super(s);
        //deposit = s;
        //setdeposit(s);
    }

    public void setdeposit(double d)
    {
        /*if(d > balance)
        {
            System.out.println("Can't deposit a value greater than " +
                    " your balance");
            System.exit(0);
        }
        else*/
            deposit = d;
            balance += d;
    }

    public double getdeposit()
    {
        return deposit;
    }

    public void print()
    {
        String output = "Deposited " + deposit +
                "\nSavings balance after deposit: " + balance;

        System.out.println(output);
    }
}

Main class

public class Main
{
    public static void main(String[] args)
    {
        Account a = new Account();
        a.setbalance(1000);

        Checkings c = new Checkings();
        Savings s = new Savings();

        System.out.println("Balance " + a.getbalance());

        c.setwithDraw(500);
        c.print();

        System.out.println();

        s.setdeposit(300);
        s.print();
    }
}

I'm confused of why the balance is negative.

Normally that is caused by an over withdrawal.
What does your code do if an attempt is made to with draw more than the balance?

Try debugging your code by adding print outs showing the values of the variables as they change so you can see why the code allows a balance to go negative.

You create an account a and deposit $1000, and print the balance of $1000.
You then create another account c with the default balance of $0, withdraw $500, and print a balance of -$500.
What's the problem? It's working perfectly.

Normally that is caused by an over withdrawal.
What does your code do if an attempt is made to with draw more than the balance?

Try debugging your code by adding print outs showing the values of the variables as they change so you can see why the code allows a balance to go negative.

Thanks for the suggestion! I'll give that a try.

GooeyG
Your code is working. The result it gives is the correct result for the test case you created. Look at your test case.

You create an account a and deposit $1000, and print the balance of $1000.
You then create another account c with the default balance of $0, withdraw $500, and print a balance of -$500.
What's the problem? It's working perfectly.

I thought using the Account a.setbalance(1000) would set the balance to $1000? I assumed that both classes(Checking and Savings) would have access to balance.
I tried creating a new method(getnewBalance()) in Checkings and Savings class and still come up with the same results. So, how would both classes have access to the balance?

each instance has its own variables, inherited or not

I assumed that both classes(Checking and Savings) would have access to balance.

They could with some banks. How does your bank work?

each instance has its own variables, inherited or not

Thanks for clearing that up. For some reason I thought inheritance allowed the ability to have access to data in it's superclass.

They could with some banks. How does your bank work?

If I make a withdraw or deposit, I have to wait at least 24 hours for that transaction to take into effect. During that time span, If i go on a spending spree, the original balance is effected and not new balance.

Thanks for clearing that up. For some reason I thought inheritance allowed the ability to have access to data in it's superclass.

Just be careful to distinguish between the class and an instance of the class. Inheritance does allow you to access (non-private) variables inherited from the superclass, but that's not the same as accessing variables from another instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.