你好我正试图让我的程序运行,这样当你转账时,2.0美元的转账费仅适用于余额低于10,000美元的账户。
我遇到的问题是,无论余额如何,我的代码都向我收取转帐费用。如果我有5,000,000美元的余额而我转账9,999美元则向我收取2.0的转账费。
如果我尝试转移> 10,000美元没有转让费。请发现我的错误。非常感谢。
有5个课程,所以我将在下面粘贴所有相关信息。
// CLASS 1
public class AccountConstants {
private final double CHECKING_BALANCE_THRESHOLD = 10000;
private final double TRANSFER_FEE = 2.0;
public double getFee(double amount){
if (amount < CHECKING_BALANCE_THRESHOLD){
return TRANSFER_FEE;
}
return 0.0;
}
public class CheckingAccount extends Account{
public CheckingAccount(String number, String name, GregorianCalendar
openDate, double balance){
super(number,name,openDate,balance);
}
//CLASS 2
@override
public int transferTo(Account account, double amount) {
AccountConstants ac = new AccountConstants();
double fee = ac.getFee(amount);
if (this.getBalance() < amount)
return -2;
else if (this.getBalance() < (amount+fee))
return -1;
else if (this.getBalance()>=(amount+fee)&& fee==2.0){
this.setBalance(-(amount+fee));
account.setBalance(amount);
return 1;
}
else
this.setBalance(-amount);
account.setBalance(amount);
return 0;
}
//CLASS 3
if (result == 0)
{
au.setBalance(jComboBox1.getSelectedIndex(),
accounts.getBalance());
au.setBalance(index, accounts_to.getBalance());
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
txtBalance.setText(defaultFormat.format(accounts.getBalance()));
au.updateFile(au.getAccountNumber(index), au.getOpenDate(index),
au.getCustomerName(index), au.getBalance(index));
index = jComboBox1.getSelectedIndex();
au.updateFile(au.getAccountNumber(index), au.getOpenDate(index), au.getCustomerName(index), au.getBalance(index));
JOptionPane.showMessageDialog(this, defaultFormat.format(amount)+" was transfered to "+accountNumber,"Transfer successful", JOptionPane.INFORMATION_MESSAGE);
}
else if (result == 1)
{
au.setBalance(jComboBox1.getSelectedIndex(), accounts.getBalance());
au.setBalance(index, accounts_to.getBalance());
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
txtBalance.setText(defaultFormat.format(accounts.getBalance()));
au.updateFile(au.getAccountNumber(index), au.getOpenDate(index),au.getCustomerName(index), au.getBalance(index));
index = jComboBox1.getSelectedIndex();
au.updateFile(au.getAccountNumber(index), au.getOpenDate(index), au.getCustomerName(index), au.getBalance(index));
JOptionPane.showMessageDialog(this, defaultFormat.format(amount)+" was transfered to "+accountNumber+"\n$2.0 transfer fee was applied", "Transfer successful", JOptionPane.INFORMATION_MESSAGE);
}
答案 0 :(得分:1)
您要将金额发送到getFee
功能而不是余额。
变化:
double fee = ac.getFee(amount);
要:
double fee = ac.getFee(this.getBalance());
这也很糟糕:
else if (this.getBalance()>=(amount+fee)&& fee==2.0){
您应将其更改为:
else if (this.getBalance()>=(amount+fee)&& fee==TRANSFER_FEE){
甚至更好:
else if (this.getBalance()>=(amount+fee)&& fee > 0.0){
答案 1 :(得分:1)
Ugg,你能否正确格式化代码?它给了我一个酸痛的头。但无论如何:
double fee = ac.getFee(amount);
应该是:
double fee = ac.getFee(this.getBalance());
由于费用是根据余额计算的,而不是转移的金额。
此外,这不会导致问题,但是:
else
this.setBalance(-amount);
account.setBalance(amount);
return 0;
应该是:
else
{
this.setBalance(-amount);
account.setBalance(amount);
return 0;
}
此外,将-2
的神秘值返回到1
并不能帮助理解代码。