2013-05-20 79 views
-4

我試圖計算一個簡單的銀行function.I通過兩個撤回值150和47.62通過一個方法從執行類到另一個。但它需要兩次47.62兩次,並給我錯誤的結果這裏是執行類。給出錯誤的結果,我該怎麼辦?

public class Account { 
    public double balance ; 
    public double deposite; 
    public double withdraw; 
    public Account(double balance) { 
    this.balance = balance; 
    } 
    public double getBalance() { 
    return balance; 
    } 
    public void setBalance(double balance) { 
    this.balance = balance; 
    } 
    public double getDeposite() { 
    balance = balance + deposite; 
    return deposite; 
    } 
    public void setDeposite(double deposite) { 
    this.deposite = deposite; 

    } 
    public double getWithdraw() { 
    return withdraw; 
    } 
    public void setWithdraw(double withdraw) { 
    this.withdraw = withdraw; 
    if(withdraw <= balance){ 
     balance = balance - withdraw; 
    } 
    } 
    public boolean withdraw(double wamt) 
    { 
    boolean result = false; 
    if(withdraw <= wamt) 
    { 
     balance= balance - withdraw; 
     return true; 
    } 
    return result; 
    } 
} 

我的客戶類

public class Customer { 
    private String firstName; 
    private String lastName; 
    Account account; 
    public Customer(String firstName, String lastName) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    //this.account = account; 
    } 
    public String getFirstName() { 
    return firstName; 
    } 
    public void setFirstName(String firstName) { 
    this.firstName = firstName; 
    } 
    public String getLastName() { 
    return lastName; 
    } 
    public void setLastName(String lastName) { 
    this.lastName = lastName; 
    } 
    public Account getAccount() { 
    return account; 
    } 
    public void setAccount(Account account) { 
    this.account = account; } 
} 

主要類:

public class BankProjectDemo { 
    public static void main(String[] args) { 
    double balance = 500; 
    Customer cust = new Customer("asasd0","asdasda"); 
    Account accnt = new Account(balance); 
    System.out.println("Creating customer: " +cust.getFirstName()); 
    accnt.setWithdraw(150); 
    accnt.setDeposite(22.50); 
    System.out.println("Withdraw1 "+accnt.getWithdraw()); 
    System.out.println("Depsoite "+accnt.getDeposite()); 
    Account accnt1 = new Account(balance); 
    accnt1.setWithdraw(47.62); 
    System.out.println("Withdraw2 "+accnt1.getWithdraw()+" " + accnt1.withdraw(balance)); 
    System.out.println("Balance " + accnt.getBalance()); 
    } 
} 
+0

Account.withdraw()方法ds返回一個布爾結果。兩個方法是相同的,但一個是布爾類型和其他是無效類型。我的問題是結果餘額應該是324.88作爲總餘額,但它給出277.26。它需要第二次提取金額兩次,但爲什麼?????????? – user2349171

+5

調試器:「你好,請用我吧:(」。 – Maroun

+1

你試過調試它嗎?學習如何調試比請求別人爲你做得更好 – BobTheBuilder

回答

1
public boolean withdraw(double wamt) 
    { 
    boolean result = false; 
    if(withdraw <= wamt) 
    { 
     balance= balance - withdraw; 
     return true; 
    } 
    return result; 
    } 

您降低兼得withdraw()setWithdraw()所以你設置一次,然後在你的系統的println再次降低這就是爲什麼最後你得到兩倍的減少

+0

非常感謝。它解決了我的問題 – user2349171

1
public double getDeposite() { 
    balance = balance + deposite; 
    return deposite; 
    } 

這個方法查找錯誤,每次你回到你增加另一個變量的時間 - 這意味着你稱這種方法越多,平衡越大。這是故意的嗎?

另外 - 你應該看看一些基本的單元測試和調試,所以更清楚地看到不同的變量發生了什麼。

0

也許問題就在這裏

System.out.println("Balance " + accnt.getBalance()); 

應該

System.out.println("Balance " + accnt1.getBalance());