Learn Java · lesson 61 of 61 · about 12 minutes
Project: Order Tracker
Track café orders through their stages with an enum, a record and a generic helper.
What you'll learn
- Model stages with an enum that knows its next stage
- Store orders as records in a map
- Write a generic counting helper
The snack counter
The last job of the night is the snack counter, where orders go through four stages: placed, being made, ready and collected. Commands are typed one per line until close: new popcorn places an order, Order 1 placed: popcorn; next 2 moves order 2 on a stage, Order 2: MAKING, or says No order 7; and report counts the orders at each stage, Orders: {PLACED=1, MAKING=0, READY=1, COLLECTED=0}. Anything else gets Unknown command: refund 3. When the counter closes, the desk counts tonight's seats the same way: Seats tonight: {STANDARD=1, PREMIUM=2, RECLINER=1}. The OrderStage enum, with its next() method, is written for you.
The plan
Every order moves through the same stages: placed, being made, ready, collected. The tracker will:
1. Use enum Stage { PLACED, MAKING, READY, COLLECTED } with a next() method
2. Keep each order's stage in a Map<Integer, Stage> by order number
3. Handle commands like new, advance 2 and report
4. Count orders per stage with a generic method that works for any enum
An enum that knows what comes next
public class Main {
enum Stage {
PLACED, MAKING, READY, COLLECTED;
Stage next() {
Stage[] all = values();
return ordinal() + 1 < all.length ? all[ordinal() + 1] : this;
}
}
public static void main(String[] args) {
Stage s = Stage.PLACED;
for (int i = 0; i < 4; i++) {
System.out.print(s + " ");
s = s.next();
}
System.out.println();
}
}Output
PLACED MAKING READY COLLECTEDCounting any enum
EnumMap is a Map made for enum keys; it keeps them in the enum's order. A generic method <E extends Enum<E>> can count the values in any collection of enums, whichever enum it is.
A generic count
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public class Main {
enum Stage { PLACED, MAKING, READY, COLLECTED }
static <E extends Enum<E>> Map<E, Integer> countAll(Class<E> type, List<E> items) {
Map<E, Integer> counts = new EnumMap<>(type);
for (E e : type.getEnumConstants()) counts.put(e, 0);
for (E e : items) counts.merge(e, 1, Integer::sum);
return counts;
}
public static void main(String[] args) {
List<Stage> orders = List.of(Stage.READY, Stage.PLACED, Stage.READY);
System.out.println(countAll(Stage.class, orders));
}
}Output
{PLACED=1, MAKING=0, READY=2, COLLECTED=0}One count for orders and seats
The orders live in a Map<Integer, OrderStage> snackOrders by number, a TreeMap, so the next number is snackOrders.size() + 1. An enum value never changes, so moving an order on means putting snackOrders.get(orderNumber).next() back into the map. countAll(OrderStage.class, ...) counts the orders' stages, and because it works for any enum, countAll(SeatKind.class, seatsBooked) counts the seats with the same method, every value there even if it's 0.
Tip
counts.merge(key, 1, Integer::sum) adds 1 to the count, or puts 1 if the key wasn't there yet.
Now try it yourself
This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.