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.
((SuperClass) this).field
but it much better to give the field a different name to avoid confusion.