This is only the second time I use this, and the first time I got really good help so I am hoping I could get some more help!
Background:
My program tests my Account
class by creating an object that holds a balance and other things. The instructions say I need to override the subclasse's "Withdraw" and "Deposit" methods in order to keep track of a transaction.
I did this, but it doesn't use the current balance and instead just sends in a 0. I want to be able to keep the new balance so it actually withdraws or deposits from the current balance.
Sorry if this makes no sense, if any clarification is needed I will try to explain in a different way.
Below is my code snippet:
Note: I kept out the stuff that already works (constructors and other methods) that has nothing to do with this:
Account.java
public class Account
{
private static double balance;
public void Withdraw(double withdrawAmount)
{
this.balance -= withdrawAmount;
}
public void Deposit(double depositAmount)
{
this.balance += depositAmount;
}
}
UserAccount.java
public class UserAccount extends Account
{
private ArrayList<Transactions> transactions = new ArrayList();
@Override
public void Withdraw(double withdrawAmount)
{
super.Withdraw(withdrawAmount);
Transactions thisTransaction = new Transactions('W',
withdrawAmount, this.AccBalance(), "Withdraw");
this.transactions.add(thisTransaction);
}
@Override
public void Deposit(double depositAmount)
{
super.Deposit(depositAmount);
Transactions thisTransaction = new Transactions('D',
depositAmount, this.AccBalance(), "Deposit");
this.transactions.add(thisTransaction);
}
public void fillTransactions(char type, double amount, double balance)
{
switch (type) {
case 'D':
this.Deposit(amount);
break;
case 'W':
this.Withdraw(amount);
break;
default:
System.out.println("ERROR fillTransactions");
break;
}
}
public static void main(String[] args) {
ArrayList<Transactions> copyTransactions;
UserAccount thisAccount = new UserAccount("George", 1122, 1000);
thisAccount.MutAIR(.015);
thisAccount.fillTransactions('D', 30, thisAccount.AccBalance());
thisAccount.fillTransactions('D', 40, thisAccount.AccBalance());
thisAccount.fillTransactions('D', 50, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 5, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 4, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 2, thisAccount.AccBalance());
}
The Transactions
class that I have holds the type (withdraw or deposit), the amount getting withdrawn or deposited, and the balance. What happening is when I call the super class from the overridden deposit or withdraw methods, it sets balance to 0 so it says that the balance is 50, or 40, or -5 when I want the original balance with the deposited or withdrawn currency.
If anyone could help, that would be amazing! I can clarify if anything is confusing! Thank you!
Hey I think you are not setting the balance in the constructor of UserAccount.
Here's an example of setting variable
Assume class A as Account and class B as UserAccount
class A {
private double balance;// Don't use static
public A(double balance) {// You will need this
this.balance = balance;
}
public double getBalance() { return balance; }
}
class B extends A {
public B(double balance) {
super(balance);//Important
}
public void d() { System.out.println(getBalance()); }
}
public class Main {
public static void main(String args[]) {
B b = new B(100.0);
b.d();
}
}
If you use static balance in the super class, then only one instance of it will be used for all the objects, since you want individual balance for each UserAccount I guess.