OnJava8-Examples/generics/BankTeller.java

59 lines
1.6 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: generics/BankTeller.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// A very simple bank teller simulation.
import java.util.*;
import net.mindview.util.*;
class Customer {
private static long counter = 1;
private final long id = counter++;
private Customer() {}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String toString() { return "Customer " + id; }
// A method to produce Generator objects:
public static Generator<Customer> generator() {
2015-05-27 23:30:19 -07:00
return Customer::new; // Constructor reference
2015-04-20 15:36:01 -07:00
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
class Teller {
private static long counter = 1;
private final long id = counter++;
private Teller() {}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String toString() { return "Teller " + id; }
// A single Generator object:
2015-05-27 23:30:19 -07:00
public static Generator<Teller> generator = Teller::new;
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
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);
2015-05-05 11:20:13 -07:00
Queue<Customer> line = new LinkedList<>();
2015-04-20 15:36:01 -07:00
Generators.fill(line, Customer.generator(), 15);
2015-05-05 11:20:13 -07:00
List<Teller> tellers = new ArrayList<>();
2015-04-20 15:36:01 -07:00
Generators.fill(tellers, Teller.generator, 4);
for(Customer c : line)
serve(tellers.get(rand.nextInt(tellers.size())), c);
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
} /* 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
Teller 2 serves Customer 13
Teller 1 serves Customer 14
Teller 1 serves Customer 15
*///:~