Failed to set the private value "balance" from the setter, despite the fact that the change in value from the internal block of the method is displayed

Adopting a method of withdrawing money from an active account with a balance. The console shows that the revocation value is correctly passed from TestClass to the account class where the output method is defined. It also shows the "balance" value changing within the output method.

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

I'll include what seems appropriate:

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 a 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 a method 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 added a print to the console to see what was happening as it was all printed via 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 it finds the account and walks away from it using withdrawal (double withdrawal, double balance)

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

      

this is where the recipient should show the change!

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

      

Now let's say I have a $ 12 account. I enter a withdrawal amount of 11 and this is what you find:

A print from a test class showing the output to be transferred and the value obtained from getting the balance:

current balance is: 12.0   
withdraw amount: 11.0

      

It prints from the corresponding Sub-class as a method block:

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

      

Fine! Now showing the value received from the receiver, which is called after the output method, and hence the value should be .98:

new balance is: 12.0

      

as you can see that no new balance is being set in the withdrawal method. Any ideas? Possible pass by value? maybe with my constructor? Lost indeed. Think about it so I can write the other three methods that use balance as well.

+3


source to share


2 answers


Extract the balance argument from the method and use the item balance instead. Otherwise, the changes will be lost.



0


source


Your classes have few bugs. First, in your inference methods, you have a balance variable that hides your balance of class variables. So if you want to change the class balance variable use

this.balance = this.balance - withdrawInput;

      

Second, I see no reason to have a balance parameter at all, so unless there are special requirements, 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 /

Last but not least, remove your fine implementation (and any other override method that doesn't change anything) in CheckingAccount, since you are not using inheritance.

0


source







All Articles