制定提款方法,从有余额的活跃账户中提款。控制台显示提款值从 TestClass 正确传递到定义提款方法的帐户类。它还显示了提款方法内“余额”变化的值。
但是,当我在下一行代码中调用余额值时,它会给出我创建帐户时的初始余额值。
我将包括看起来相关的内容:
这是父类(super class)
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.");
}
}
这是子类
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.");
}
}
这是被调用的方法
//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();
}
我已将 print 添加到控制台以跟踪发生的情况,因为它都是通过我的 testClass 中的 javaFX 打印的
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);
在这里找到帐户并使用提款(双倍提款,双倍余额)从帐户中提款
findCheckingAccountIndex(s, checkingsArray, nameListChecking).withdraw(Double.parseDouble(transactionField.getText()),findCheckingAccountIndex(s, checkingsArray, nameListChecking).getBalance());
这是 getter 应该显示更改的地方!
test = "new balance is: " + findCheckingAccountIndex(s,checkingsArray,nameListChecking).getBalance();
System.out.println(test);
现在假设我的账户余额为 12 美元。我输入提款金额 11,您会看到以下内容:
从测试类打印显示即将通过的提款以及获得余额所获得的值:
current balance is: 12.0
withdraw amount: 11.0
在执行方法 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
太棒了!现在显示在withdraw方法之后调用的getter获得的值,因此该值应该是0.98:
new balance is: 12.0
正如您所看到的,新的余额并未在提款方式中设置。有任何想法吗?可能存在传值问题?也许与我的构造函数有关?真的输了。必须弄清楚这一点,以便我可以编写其他三个使用平衡的方法。
最佳答案
从方法中删除余额参数并使用类成员余额。否则更改将会丢失。
关于java - 尽管显示值从方法 block 内部更改,但从 setter 设置私有(private)值 "balance"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31618189/