/**
*模拟场景,一家三口用同一账户银行卡,每当余额不足是,会通知丈夫存款,余额充足时妻子和儿子会循环花费。
*练习wait()和notify()方法
*/
public class TestBank {
public static void main(String[] args) {
Account acc = new Account("1234" , "1234" , 2000.0);
Thread h = new Thread(new Husband(acc) , "丈夫");
Thread w = new Thread(new Wife(acc) , "妻子");
Thread s = new Thread(new Son(acc) , "儿子");
h.start();
w.start();
s.start();
}
}
class Account{
String cardNo;
String password;
double balance;
boolean boo;
public Account(String cardNo, String password, double balance) {
super();
this.cardNo = cardNo;
this.password = password;
this.balance = balance;
}
//存款
public synchronized void deposit(String cardNo, String password , double money) throws InterruptedException{
if(cardNo.equals(this.cardNo) && password.equals(this.password)){
System.out.println(Thread.currentThread().getName() + "登录成功");
if(boo == true){
System.out.println("账户余额充足");
this.wait();
}
//余额不足
System.out.println(Thread.currentThread().getName() + "充值了" + money + "元");
this.balance += money;
System.out.println("账户余额剩余" + this.balance);
boo = true;
this.notify();
}
else{
System.out.println("账户或密码错误");
}
}
//取款
public synchronized void withdrawals(String cardNo, String password , double money) throws InterruptedException{
if(cardNo.equals(this.cardNo) && password.equals(this.password)){
System.out.println(Thread.currentThread().getName() + "登录成功");
if(boo == false){
System.out.println("账户余额不足需要充值");
this.wait();
}
if(this.balance > money){
System.out.println(Thread.currentThread().getName() + "花费了" + money + "元");
this.balance -= money;
}
else{
boo = false;
this.notify();
}
System.out.println("账户余额为:" + this.balance);
}else{
System.out.println("账户或密码错误");
}
}
}
//丈夫,每当余额不住,收到消息冲值1000 , 每次多加1000;
class Husband implements Runnable{
Account acc;
public Husband(Account acc) {
super();
this.acc = acc;
}
public void run() {
for (double i = 1000.0; i < 3000; i++) {
if(i % 1000 == 0){
try {
acc.deposit("1234" , "1234" , i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//妻子实现Runnable接口,通过账户,密码登录,花100元,每次多花100;
class Wife implements Runnable{
Account acc;
public Wife(Account acc) {
super();
this.acc = acc;
}
@Override
public void run() {
for (double i = 200.0; i <= 1000.0; i++) {
if(i % 100 == 0){
try {
acc.withdrawals("1234", "1234", i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}//儿子实现Runnable接口,通过账户,密码登录,花10元,每次多花10元;
class Son implements Runnable{
Account acc;
public Son(Account acc) {
super();
this.acc = acc;
}
@Override
public void run() {
for (double i = 10; i <= 100.0; i++) {
if(i % 10 == 0){
try {
acc.withdrawals("1234", "1234", i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
打印结果:
丈夫登录成功
丈夫充值了1000.0元
账户余额剩余3000.0
儿子登录成功
儿子花费了10.0元
账户余额为:2990.0
儿子登录成功
儿子花费了20.0元
账户余额为:2970.0
儿子登录成功
儿子花费了30.0元
账户余额为:2940.0
儿子登录成功
儿子花费了40.0元
账户余额为:2900.0
儿子登录成功
儿子花费了50.0元
账户余额为:2850.0
儿子登录成功
儿子花费了60.0元
账户余额为:2790.0
儿子登录成功
儿子花费了70.0元
账户余额为:2720.0
儿子登录成功
儿子花费了80.0元
账户余额为:2640.0
儿子登录成功
儿子花费了90.0元
账户余额为:2550.0
儿子登录成功
儿子花费了100.0元
账户余额为:2450.0
妻子登录成功
妻子花费了200.0元
账户余额为:2250.0
妻子登录成功
妻子花费了300.0元
账户余额为:1950.0
妻子登录成功
妻子花费了400.0元
账户余额为:1550.0
妻子登录成功
妻子花费了500.0元
账户余额为:1050.0
妻子登录成功
妻子花费了600.0元
账户余额为:450.0
妻子登录成功
账户余额为:450.0
妻子登录成功
账户余额不足需要充值
丈夫登录成功
丈夫充值了2000.0元
账户余额剩余2450.0
妻子花费了800.0元
账户余额为:1650.0
妻子登录成功
妻子花费了900.0元
账户余额为:750.0
妻子登录成功
账户余额为:750.0