Putting it together
- Java course
- Intermediate
- Lesson 116/116
- 6 min
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.
Practice 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());.
Starter code
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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();
}
}
// main: add throws IOException, create an OrderBook, add three orders, save, print load and largeOrders sizeSample 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());
}
}Expected output
3 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the first step in the assembly?
- Read from file
- Add orders Correct answer
- Filter with a stream
- Count large orders
Explanation: First you add orders; file and filtering need the added data.
2 Complete the read-from-file call
int lines = book.____("orders.txt");
int lines = book.load("orders.txt");
Explanation: The load method returns the number of lines in the file.
3 Match the project summary
| Term | Meaning |
|---|---|
Order |
Single order data |
OrderBook |
Collection management |
largeOrders |
Stream filter |
save/load |
File persistence |