forked from RonAdi77/Bank-Queue-Mangement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Customer.java
71 lines (63 loc) · 1.69 KB
/
Customer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Queue;
/**
* Customer - represents a client of the bank.
*
*/
public class Customer extends Thread {
private int custNumber; //the id of the customer
private Bank bank;
private int serviceTime;
// private boolean flag;
// private int state = 0;
private int waitTime = 0;
private int timerStart = 0;
public Customer(Bank bank,int serviceTime,int custNumber){
this.custNumber = custNumber;
this.bank = bank;
this.serviceTime = serviceTime;
// flag = false;
}
/*
* Getters and setters
*/
public int getCustNumber() {
return custNumber;
}
// public int getServiceTime(){
// return serviceTime;
// }
/**
* run - main thread action
*/
public void run() {
synchronized (this){
timerStart = bank.getClock().getCurrentTime();
try {
notify();
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("customer num: " + custNumber + " time wait: " + waitTime);
bank.decreasCusCount();
}
/**
* serve - simulate the service the customer is getting. This method is called
* only by the teller servicing this customer. In addition to holding up the
* teller for the duration of the service, it also notifies this customer's
* thread that it has been serviced and therefore may terminate.
*/
public void serve() {
waitTime = timerStart - bank.getClock().getCurrentTime();
try {
sleep((long)serviceTime*Bank.TIME_SIMULATION_FACTOR);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return String.valueOf(custNumber);
}
} /* class Customer */