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。感謝您的幫助
你的問題是什麼?這個任務真的和在'撤銷'方法中減去你自己無法解決你的問題的方法不同嗎? – fabian 2014-10-01 22:16:10
你的問題很不明確,似乎與你的頭銜沒有任何關係。對象不執行,線程可以執行。 – EJP 2014-10-02 00:07:38