-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBankTeller.java
More file actions
68 lines (54 loc) · 1.41 KB
/
BankTeller.java
File metadata and controls
68 lines (54 loc) · 1.41 KB
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
package thinkinginjava.generics;
// A very simple bank teller simulation.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import thinkinginjava.util.Generator;
class Customer {
private static long counter = 1;
private final long id = counter++;
private Customer() {
}
public String toString() {
return "Customer " + id;
}
// A method to produce Generator objects:
public static Generator<Customer> generator() {
return new Generator<Customer>() {
public Customer next() {
return new Customer();
}
};
}
}
class Teller {
private static long counter = 1;
private final long id = counter++;
private Teller() {
}
public String toString() {
return "Teller " + id;
}
// A single Generator object:
public static Generator<Teller> generator = new Generator<Teller>() {
public Teller next() {
return new Teller();
}
};
}
public class BankTeller {
public static void serve(Teller t, Customer c) {
System.out.println(t + " serves " + c);
}
public static void main(String[] args) {
Random rand = new Random(47);
Queue<Customer> line = new LinkedList<Customer>();
Generators.fill(line, Customer.generator(), 15);
List<Teller> tellers = new ArrayList<Teller>();
Generators.fill(tellers, Teller.generator, 4);
for (Customer c : line)
serve(tellers.get(rand.nextInt(tellers.size())), c);
}
}