Java
Putting it together
Combine all layers into a working app.
Assemble the order tracking app
In the final lesson we combine all pieces into a working order tracking application. The Order and OrderBook (add, listAll, largeOrders, save, load) classes are ready. In main we add three orders, save them to a file, read them back and filter the large ones.
This flow mirrors a real application's data flow at a small scale: first the data model, then collection management, then stream analysis, then persistence, and finally assembly. Each layer was covered separately in previous lessons.
The assembly flow
The assembly flow has five stages: (1) an empty book with OrderBook book = new OrderBook();; (2) three add calls for orders; (3) writing to a file with book.save("orders.txt");; (4) reading the line count with book.load("orders.txt");; (5) counting large orders with book.largeOrders().size().
This order respects dependencies: file operations require prior adds, the large order count assumes data was saved. The flow is a small slice of a real order system.
Exercise Assemble the whole app
Bring every piece together. Write the Order and OrderBook (add, listAll, largeOrders, save, load) classes; add throws IOException to the main signature. In main, create an OrderBook book = new OrderBook();, add three orders: "1", "Ayşe", 50, "2", "Alex", 150, "3", "Mehmet", 200. Save with book.save("orders.txt");. Print the line count with System.out.println(book.load("orders.txt")); and the large order count with System.out.println(book.largeOrders().size());.
Show example solution
class Order {
String id;
String customer;
int amount;
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.amount = amount;
}
}
class OrderBook {
List<Order> orders = new ArrayList<>();
void add(Order o) { orders.add(o); }
void listAll() {
orders.forEach(o -> System.out.println(o.id + " " + o.customer + " " + o.amount));
}
List<Order> largeOrders() {
return orders.stream().filter(o -> o.amount >= 100).collect(Collectors.toList());
}
void save(String path) throws IOException {
List<String> lines = orders.stream()
.map(o -> o.id + "|" + o.customer + "|" + o.amount)
.collect(Collectors.toList());
Files.write(Path.of(path), lines);
}
int load(String path) throws IOException {
return Files.readAllLines(Path.of(path)).size();
}
}
public class Main {
public static void main(String[] args) throws IOException {
OrderBook book = new OrderBook();
book.add(new Order("1", "Ayşe", 50));
book.add(new Order("2", "Alex", 150));
book.add(new Order("3", "Mehmet", 200));
book.save("orders.txt");
System.out.println(book.load("orders.txt"));
System.out.println(book.largeOrders().size());
}
}
Test yourself
-
What is the first step in the assembly?
- Read from file
- Add orders
- Filter with a stream
- Count large orders
Show answer
Add orders — First you add orders; file and filtering need the added data.
-
Complete the read-from-file call
int lines = book.____("orders.txt");
Show answer
int lines = book.load("orders.txt");
-
Match the project summary
- Order
- Single order data
- OrderBook
- Collection management
- largeOrders
- Stream filter
- save/load
- File persistence
Next lesson: Intermediate section exam