forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankTeller.java
More file actions
61 lines (57 loc) · 1.77 KB
/
BankTeller.java
File metadata and controls
61 lines (57 loc) · 1.77 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
// generics/BankTeller.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// A very simple bank teller simulation
import java.util.*;
import onjava.*;
class Customer {
private static long counter = 1;
private final long id = counter++;
@Override
public String toString() { return "Customer " + id; }
}
class Teller {
private static long counter = 1;
private final long id = counter++;
@Override
public String toString() { return "Teller " + id; }
}
class Bank {
private List<BankTeller> tellers = new ArrayList<>();
public void put(BankTeller bt) { tellers.add(bt); }
}
public class BankTeller {
public static void serve(Teller t, Customer c) {
System.out.println(t + " serves " + c);
}
public static void main(String[] args) {
// Demonstrate create():
RandomList<Teller> tellers =
Suppliers.create(RandomList::new, Teller::new, 4);
// Demonstrate fill():
List<Customer> customers = Suppliers.fill(
new ArrayList<>(), Customer::new, 12);
customers.forEach(c -> serve(tellers.select(), c));
// Demonstrate assisted latent typing:
Bank bank = Suppliers.fill(
new Bank(), Bank::put, BankTeller::new, 3);
// Can also use second version of fill():
List<Customer> customers2 = Suppliers.fill(
new ArrayList<>(), List::add, Customer::new, 12);
}
}
/* Output:
Teller 3 serves Customer 1
Teller 2 serves Customer 2
Teller 3 serves Customer 3
Teller 1 serves Customer 4
Teller 1 serves Customer 5
Teller 3 serves Customer 6
Teller 1 serves Customer 7
Teller 2 serves Customer 8
Teller 3 serves Customer 9
Teller 3 serves Customer 10
Teller 2 serves Customer 11
Teller 4 serves Customer 12
*/