2014-10-01 59 views
0
package assignment; 
public class BankAccount { 
private double balance; 
private String accountHolderName; 

BankAccount() 
{ 
    balance = 0; 
    accountHolderName = ""; 
} 
BankAccount(double balance, String accountHolderName) 
{ 
    this.balance = balance; 
    this.accountHolderName = accountHolderName; 
} 
public String withdrawal(double withdrawalAmount) 
{ 
    String result = ""; 
    if(withdrawalAmount > balance) 
    { 
     result = "Account can not be overdrawn"; 
    }else if(withdrawalAmount <= 0) 
    { 
     result = "negative withdrawal amounts not allowed"; 
    } 
    else 
    { 
     balance -= withdrawalAmount; 
     result = "$ " +withdrawalAmount + " withdrawn. Remaining balance is: " +balance; 
    } 
    return result; 
} 

public String deposit(double amountToDeposit) 
{ 
    String result = ""; 
    if(amountToDeposit <= 0) 
    { 
     result = "Deposits must be positive"; 

    }else 
    { 
     balance += amountToDeposit; 
     result = "$ " +amountToDeposit + " deposited. Balance is: " + balance; 
    } 
    return result; 
} 


public String transfer(double amountToTransfer, BankAccount recipient) 
{ 
    String result = ""; 
    recipient.withdrawal(amountToTransfer); 
    recipient.deposit(amountToTransfer); 
    result = recipient.withdrawal(amountToTransfer) + "\n" +  recipient.deposit(amountToTransfer);; 
    return result; 
} 
public double getBalance() { 
    return balance; 
} 
public void setBalance(double balance) { 
    this.balance = balance; 
} 
public String getAccountHolderName() { 
    return accountHolderName; 
} 
public void setAccountHolderName(String accountHolderName) { 
    this.accountHolderName = accountHolderName; 
} 
@Override 
public String toString() { 
    return "BankAccount [balance=" + balance + ", accountHolderName=" 
      + accountHolderName + ", getBalance()=" + getBalance() 
      + ", getAccountHolderName()=" + getAccountHolderName() + "]"; 
} 


} 

BankAccount person1 = new BankAccount(500,「Steve」);找到執行對象

BankAccount person2 = new BankAccount(700,「Bob」);

在轉移方法im卡住我應該如何將錢轉移到person2.transfer(100,person1);

我不知道我會如何從person2對象中減去100。感謝您的幫助

+1

你的問題是什麼?這個任務真的和在'撤銷'方法中減去你自己無法解決你的問題的方法不同嗎? – fabian 2014-10-01 22:16:10

+0

你的問題很不明確,似乎與你的頭銜沒有任何關係。對象不執行,線程可以執行。 – EJP 2014-10-02 00:07:38

回答

0

當您在銀行賬戶對象上操作的對象(BankAccount)的方法(transfer())中時。所以你可以引用在同一個對象中定義的其他變量和方法。

public void transfer(double amount, BankAccount recipient) 
{ 
    if (balance >= amount) 
    { 
     balance -= amount; 
     //this balance variable represents the balance in the bank account 
     //object this money is coming from. 
     recipient.balance += amount; 
     //this balance variable is the balance of the recipient. 
     } 
} 

說的是,Java Trails確實不錯。在這裏試試這個。 Java Trails: Objects會向你解釋對象是如何工作的。

+0

感謝幫助和鏈接真的很感激! – superhamster 2014-10-02 00:28:36