OOP model
- Java course
- Intermediate
- Lesson 113/116
- 6 min
Set up the class hierarchy and interfaces.
OrderBook manages the collection
Once you have a single Order instance, you need to collect them in a list. The OrderBook class does this: it holds a List<Order> named orders and adds new orders through an add method. The listAll method prints each order with forEach.
The line List<Order> orders = new ArrayList<>(); gives every new OrderBook instance an empty list. The add method does one thing in a single line: orders.add(o);. listAll walks all orders with orders.forEach(o -> System.out.println(...)).
listAll with forEach
The listAll method prints all orders with orders.forEach(o -> System.out.println(o.id + " " + o.customer + " " + o.amount));. forEach expects a Consumer; the lambda defines a print action for each order.
This is shorter than a classic for-each loop. It walks each order and prints its id, customer and amount fields on one line. The output format is deterministic: lines appear in insertion order.
Practice Write the OrderBook class and listAll method
The Order class from above is ready. Now define class OrderBook with a List<Order> orders field initialized to an empty ArrayList, a void add(Order o) method that calls orders.add(o);, and a void listAll() method that calls orders.forEach(o -> System.out.println(o.id + " " + o.customer + " " + o.amount));. In main, create an OrderBook book = new OrderBook();, add two orders and call book.listAll();.
Starter code
import java.util.List;
import java.util.ArrayList;
class Order {
String id;
String customer;
int amount;
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.amount = amount;
}
}
// Define the OrderBook class: orders list, add and listAll methods
// main: create an OrderBook, add two orders, call listAllSample 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));
}
}
public class Main {
public static void main(String[] args) {
OrderBook book = new OrderBook();
book.add(new Order("1", "Ayşe", 50));
book.add(new Order("2", "Alex", 150));
book.listAll();
}
}Expected output
1 Ayşe 50 2 Alex 150
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the add method
void add(Order o) { orders.____(o); }
void add(Order o) { orders.add(o); }
Explanation: The add method appends the new order to the orders list.
2 What relation is there between OrderBook and Order?
- Inheritance
- Dependency
- Composition Correct answer
- Abstraction
Explanation: OrderBook contains an Order list; this is composition.
3 Match the OrderBook methods
| Term | Meaning |
|---|---|
add(o) |
Appends an order |
listAll() |
Prints all orders |
orders |
List<Order> field |
forEach |
Method used by listAll |