2012-11-30 41 views
3

我正嘗試在我的課程中使用一種收取費用的方法。該方法是這樣的:餘額 - = 10,但減去20?

public double chargeFee() 
    { 
    balance -= 10; 
    return balance; 
    } 

然而,減去20我已經試過重新編譯,但我無法找到是什麼原因造成的問題。

完整代碼:

public class ManageAccounts 
{ 
    public static void main(String[] args) 
    { 
    Account acct1, acct2; 


    //create account1 for Sally with $1000 
    acct1 = new Account(1000, "Sally", 1111); 
    acct2 = new Account(500, "Joe", 2222);//create account2 for Joe with $500 

    System.out.println("Depositing $100 into Account 2222..."); 
    acct2.deposit(100.00);//deposit $100 to Joe's account 
    System.out.println("New Balance for Account 2222: $" + acct2.getBalance());//print Joe's new balance (use getBalance()) 
    System.out.println(); 
    System.out.println("Withdrawing $50 from Account 1111..."); 
    acct1.withdraw(50);//withdraw $50 from Sally's account 

    System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's new balance (use getBalance()) 
    System.out.println(); 
    acct1.chargeFee(); 
    acct2.chargeFee();//charge fees to both accounts 
    System.out.println("Charging usage Fees..."); 
    System.out.println("Account balance after fees:"); 
    System.out.println("Account 1111: $" + acct1.chargeFee()); 
    System.out.println("Account 2222: $" + acct2.chargeFee()); 
    System.out.println(); 
    System.out.println("Changing name on Account 2222..."); 
    acct2.changeName("Joseph");//change the name on Joe's account to Joseph 
    System.out.println(); 
    System.out.println("Printing account summaries..."); 
    System.out.println(acct1.toString()); 
    System.out.println(acct2.toString());//print summary for both accounts 

    } 
} 



import java.text.NumberFormat; 

public class Account 
{ 
    private double balance; 
    private String name; 
    private long acctNum; 
    NumberFormat money = NumberFormat.getCurrencyInstance(); 
    //---------------------------------------------- 
    //Constructor -- initializes balance, owner, and account number 
    //---------------------------------------------- 
    public Account(double initBal, String owner, long number) 
    { 
    balance = initBal; 
    name = owner; 
    acctNum = number; 
    } 

    //---------------------------------------------- 
    // Checks to see if balance is sufficient for withdrawal. 
    // If so, decrements balance by amount; if not, prints message. 
    //---------------------------------------------- 
    public void withdraw(double amount) 
    { 
    if (balance >= amount) 
     balance -= amount; 
    else 
     System.out.println("Insufficient funds"); 
    } 

    //---------------------------------------------- 
    // Adds deposit amount to balance. 
    //---------------------------------------------- 
    public void deposit(double amount) 
    { 
    balance += amount; 
    } 

    //---------------------------------------------- 
    // Returns balance. 
    //---------------------------------------------- 
    public double getBalance() 
    { 
    return balance; 
    } 


    //---------------------------------------------- 
    // Returns a string containing the name, account number, and balance. 
    //---------------------------------------------- 
    public String toString() 
    { 
    return ("Name: " + name + "\tAccount Number: " + acctNum + "\tBalance: " + money.format(balance)); 
    } 

    //---------------------------------------------- 
    // Deducts $10 service fee 
    //---------------------------------------------- 
    public double chargeFee() 
    { 
    balance -= 10; 
    return balance; 
    } 

    //---------------------------------------------- 
    // Changes the name on the account 
    //---------------------------------------------- 
    public void changeName(String newName)       
    { 
    name = newName; 
    } 

} 

一些樣本輸出:

Depositing $100 into Account 2222... 
New Balance for Account 2222: $600.0 

Withdrawing $50 from Account 1111... 
New Balance for Account 1111: $950.0 

Charging usage Fees... 

Account balance after fees: 

Account 1111: $930.0 
Account 2222: $580.0 

Changing name on Account 2222... 

Printing account summaries... 
Name: Sally Account Number: 1111 Balance: $930.00 
Name: Joseph Account Number: 2222 Balance: $580.00 
+3

您只需要調用該方法*一次*! –

+0

你的問題到底是什麼? – sunleo

+0

+1用於發佈代碼並清除問題描述。 –

回答

7

您在兩個帳戶打電話chargeFee兩次,一次是當你收取實際費用,並再次當您打印結果。

爲了將來的參考,有一個指導原則叫做"Seperate command and query"或CQS。你可能想要閱讀它。把你的情況,它的光:

  • 您正在使用chargeFee作爲查詢(即返回平衡)
  • 並使用chargeFee作爲命令,扣錢。

變化chargeFee返回一個void(或this如果你想方法鏈接),你會不會意外地使用它作爲一個查詢。

http://en.wikipedia.org/wiki/Command-query_separation

+1

我想說你應該在這種情況下返回'this'。你同意嗎? – jozefg

+0

你可以,如果你想要的方法鏈接... –

+0

是的,我不得不將它從無效改爲雙重的作業。不過謝謝。 – user1740066

3

它爲每個帳戶扣除2費用。

acct1.chargeFee(); 
acct2.chargeFee();//charge fees to both accounts 
System.out.println("Charging usage Fees..."); 
System.out.println("Account balance after fees:"); 
System.out.println("Account 1111: $" + acct1.chargeFee()); 
System.out.println("Account 2222: $" + acct2.chargeFee()); 

按理說,chargeFee()不應該返回一個平衡點,但要像voiddeposit()withdraw()。另請注意,財務事宜通常以int而非double號碼處理。

2
System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's new balance (use getBalance()) 
System.out.println(); 
acct1.chargeFee();------------------------//First time 
acct2.chargeFee();//charge fees to both accounts 
System.out.println("Charging usage Fees..."); 
System.out.println("Account balance after fees:"); 
System.out.println("Account 1111: $" + acct1.chargeFee());------------------------//second time 
System.out.println("Account 2222: $" + acct2.chargeFee()); 
2
1) 
acct1.chargeFee(); 
acct2.chargeFee();//charge fees to both accounts 

2) 
System.out.println("Account 1111: $" + acct1.chargeFee()); 
System.out.println("Account 2222: $" + acct2.chargeFee()); 

注意到你怎麼稱呼chargeFee()兩次。這就是給你搞砸的結果。您只需要撥打一次,然後第二次在每個帳戶中打印餘額(第2部分)。

System.out.println("Account 1111: $" + Account 1111's balance); 
System.out.println("Account 2222: $" + Account 2222's balance); 

希望有所幫助!

1

你已經有幾個正確的答案指出你的程序中的問題。這不是一個真正的答案,而是一個關於如何自己發現這個問題的擴展評論。

給出一個理論的理論,方法chargefee將被減去20,接下來的一步是添加斷點或打印輸出,以檢查它確實:

public double chargeFee() { 
    System.out.println("Before subtract: " + balance + " Account: " + acctNum); 
    balance -= 10; 
    System.out.println("After subtract: " + balance + " Account: " + acctNum); 
    return balance; 
} 

從程序的輸出與打印輸出的附加很明顯,每次chargeFee調用僅減去10次,但對於每個賬戶調用兩次。

+0

或者爲了更全面的調試處理,請參閱[調試策略](http://home.earthlink.net/~patricia_shanahan/debug/)。 IDE的調試器可以提供幫助,但它只是該文檔中描述的過程的一部分。 –

+0

我覺得有點尷尬,繼續推動我自己的網頁。請注意,我正在嘗試將鏈接更改爲www.patriciashanahan.com/debug/ –

+0

*「我繼續推送自己的網頁時感到有些尷尬。」*我認爲沒有理由不願意提供有價值的信息。我不是(當涉及到我的網站,或你的)。至於鏈接:我的道歉,我相信某個搜索引擎。嘗試改爲http://www.patriciashanahan.com/debug/(如果它顯示足夠的次數作爲鏈接,最終應取代較舊的鏈接)。另一個可能的幫助是讓舊頁面自動重定向到新域名,因爲用戶不太可能對舊URL進行預訂(或重複)。 –