3

Making a withdrawal method to withdraw money from an active account with a balance. The console shows the withdraw value passing correctly from the TestClass to the account class where the withdrawal method is defined. It also shows the value of "balance" changing inside the withdraw method.

However, when i call the value of the balance in my next Line of code, it gives me the initial balance value of when i created the account.

I'll include what seems relevant:

Here is the superclass

public class Account {
//declares the variables
private double initialBalance;
private double credit;
private double balance;

//default constructor
public Account() {
    this(0.0, 0.0);
}

//constructs Account object
public Account(double initialBalance, double balance) {
        this.initialBalance = initialBalance;
        this.balance = initialBalance;
}

//gets initial balance
public double getInitialBalance() {
    return initialBalance;
}

//gets balance
public double getBalance() {
    return balance;
}

// Sets initial balance
public void setInitialBalance(double initialBalance) {
    if (initialBalance > 0){
        this.initialBalance = initialBalance;
        this.balance = initialBalance;
    }
    else noCrediting();
}

//crediting the account if the credit function gets a positive input
public double credit(double creditInput, double balance){
    if (creditInput>0)
    {
        balance = balance + creditInput;
    }   
    else
        noCrediting();
    return balance;
}


//tells the user no credit was added
public void noCrediting() {
    System.out.println("No credit was added because the deposit was not a positive double.");
}

//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance){
    if (withdrawInput>0 && withdrawInput<balance)
    {
        balance = balance - withdrawInput;
    }
    else
        noWithdrawing();
}

//tells the user no withdrawal was performed
public void noWithdrawing() {
    System.out.println("No amount was withdrawn because the deposit was not a positive double less than the balance.");
}

}

Here is the subclass

public class CheckingAccount extends Account {

//declares the variables
private double feeChargedPerTransaction = 2.0;

//default constructor
public CheckingAccount() {
    this(0.0, 0.0, 2.0);
}

//constructs Account object
public CheckingAccount(double initialBalance, double balance, double feeChargedPerTransaction) {
    super(initialBalance,  balance);
    feeChargedPerTransaction = 2.0;
}

//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance){
    if (withdrawInput>0 && withdrawInput < balance)
    {
        System.out.println("The withdrawal amount from the checking account class is showing"+ withdrawInput); 
        balance = (balance - withdrawInput)*.98;
        System.out.println("Now The balance from the checking account class is showing: " + balance);

    }
    else
        noWithdrawing();
}

//gets fee
public double getFee() {
    return feeChargedPerTransaction;
}

public void noWithdrawing() {
    System.out.println("No amount was withdrawnnn because the deposit was not a positive double less than the balance.");
}

}

Here is the method being called

//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance) {
    if (withdrawInput>0 && withdrawInput < balance) {
        System.out.println("The withdrawal amount from the checking account class is showing"+ withdrawInput); 
        balance = (balance - withdrawInput)*.98;
        System.out.println("Now The balance from the checking account class is showing: " + balance);
    }
    else
        noWithdrawing();
}

I have added print to the console to folllow what is going on, since it is all printed through javaFX in my testClass

String test = "current balance is: "
    + findCheckingAccountIndex(s, checkingsArray, nameListChecking).getBalance();
System.out.println(test);
test = "withdraw amount: " + Double.parseDouble(transactionField.getText());
System.out.println(test);

here is where it finds the account and withdraws from it using withdraw(double withdrawal, double balance)

findCheckingAccountIndex(s, checkingsArray,           nameListChecking).withdraw(Double.parseDouble(transactionField.getText()),findCheckingAccountIndex(s, checkingsArray, nameListChecking).getBalance());

this is where the getter should show the change!

test = "new balance is: " + findCheckingAccountIndex(s,checkingsArray,nameListChecking).getBalance();
    System.out.println(test);

Now lets say I make the account to have $12. I input a withdrawal amount of 11 and here's what you find:

Printing from Test Class showing the withdraw about to be passed and the value obtained from getting the balance:

current balance is: 12.0   
withdraw amount: 11.0

Printing From pertinent Sub Class as the method block is being executed:

The withdrawal amount from the checking account class is showing 11.0
Now The balance from the checking account class is showing: 0.98

Great! Now showing the value obtained from the getter that is called after the withdraw method, and thus the value should be .98:

new balance is: 12.0

as you can see the new balance isn't being set within the withdrawal method. Any ideas? Possible pass by value issue? have to do with my constructor maybe? Really lost. Have to figure this out so I can write the other three methods that use balance as well.

4
  • Java doesn't support pass by reference. Jul 24, 2015 at 20:15
  • Can you show us your class structure? My first thought is that you have defined a private field in the super class and hidden it with a field of the same name in your sub-class. Jul 24, 2015 at 20:15
  • @Peter Lawrey just put that in. I also have a variable called balance in my testClass but the function is calling the getter specific to the object stored in the arrayList so I dont think that should be a problem? i do know that the function is calling the method in the subclass that I want. maybe I need to call the getter method explicitly from there as well?
    – Patrick
    Jul 24, 2015 at 20:52
  • Methods can be overriden ie replaced by the method in a sub-class but fields are hidden and they both exist for the sub-class. you can be more explicit with ((SuperClass) this).field but it much better to give the field a different name to avoid confusion. Jul 25, 2015 at 5:38

2 Answers 2

0

Remove the balance argument from the method and use the class member balance instead. Otherwise the changes will be lost.

0

Your classes have few errors. First in your withdraw methods you have a balance variable which is hiding your class variable balance. So if you want to modify the class balance variable use

this.balance = this.balance - withdrawInput;

Second I don`t see a reason to have balance parameter at all so if there is no specific requirement just change your method to:

// withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput) {
    if (withdrawInput > 0 && withdrawInput < balance) {
        balance = balance - withdrawInput;
    } else
        noWithdrawing();
}

which should work/

And last but not least remove your withdraw implementation(and any other method reimplementation that is not changing anything) in CheckingAccount since this way you don`t benefit from the inheritance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.