Careeroza
Real World Example
Order Processing in an E-commerce App
A Flipkart-like order system uses several collections together: a Map for the product catalogue, a List for cart lines, a Set for applied coupons (no duplicates), and a Queue of orders waiting to be packed.
Example
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> catalogue = new LinkedHashMap<>();
catalogue.put("P1", 999);
catalogue.put("P2", 2499);
catalogue.put("P3", 349);
List<String> cart = new ArrayList<>(List.of("P1", "P3", "P3", "P2"));
Set<String> coupons = new LinkedHashSet<>();
coupons.add("NEW50");
coupons.add("NEW50"); // ignored: already applied
Map<String, Integer> qty = new TreeMap<>();
for (String id : cart) qty.merge(id, 1, Integer::sum);
int total = 0;
for (var e : qty.entrySet()) {
int line = catalogue.get(e.getKey()) * e.getValue();
total += line;
System.out.println(e.getKey() + " x" + e.getValue() + " = " + line);
}
if (coupons.contains("NEW50")) total -= 50;
System.out.println("Coupons " + coupons + " -> payable " + total);
Queue<String> packing = new ArrayDeque<>(List.of("ORD-101", "ORD-102"));
packing.offer("ORD-103");
System.out.println("Packing next: " + packing.poll() + ", waiting: " + packing);
}
}Output
P1 x1 = 999 P2 x1 = 2499 P3 x2 = 698 Coupons [NEW50] -> payable 4146 Packing next: ORD-101, waiting: [ORD-102, ORD-103]