2017-06-06 11 views
1

您好,我試圖讓我的程序運行,以便您在轉賬時,轉賬費用爲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); 
} 

回答

1

雪地,可以請你正確設置代碼的格式?它給了我一個頭痛。但不管怎麼說:

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; 
} 

此外,返回-21神祕的值不符合的可理解性幫助代碼。

+0

對不起,新的和謝謝! – Mikeyubuee

+0

老師讓我們挑選這些價值觀,但是謝謝! – Mikeyubuee

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){ 
+0

感謝幫助! – Mikeyubuee