?? account.java
字號:
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class Account
{
private String accountNo;
private double balance;
//標識賬戶中是否已有存款的旗標
private boolean flag = false;
public Account(){}
public Account(String accountNo , double balance)
{
this.accountNo = accountNo;
this.balance = balance;
}
public void setAccountNo(String accountNo)
{
this.accountNo = accountNo;
}
public String getAccountNo()
{
return this.accountNo;
}
public double getBalance()
{
return this.balance;
}
public synchronized void draw(double drawAmount)
{
try
{
//如果flag為假,表明賬戶中還沒有人存錢進去,則取錢方法阻塞
if (!flag)
{
wait();
}
else
{
//執行取錢
System.out.println(Thread.currentThread().getName() +
" 取錢:" + drawAmount);
balance -= drawAmount;
System.out.println("賬戶余額為:" + balance);
//將標識賬戶是否已有存款的旗標設為false。
flag = false;
//喚醒其他線程
notifyAll();
}
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
public synchronized void deposit(double depositAmount)
{
try
{
//如果flag為真,表明賬戶中已有人存錢進去,則存錢方法阻塞
if (flag)
{
wait();
}
else
{
//執行存款
System.out.println(Thread.currentThread().getName() +
" 存款:" + depositAmount);
balance += depositAmount;
System.out.println("賬戶余額為:" + balance);
//將表示賬戶是否已有存款的旗標設為true
flag = true;
//喚醒其他線程
notifyAll();
}
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
public int hashCode()
{
return accountNo.hashCode();
}
public boolean equals(Object obj)
{
if (obj != null && obj.getClass() == Account.class)
{
Account target = (Account)obj;
return target.getAccountNo().equals(accountNo);
}
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -