Project design
- Java course
- Intermediate
- Lesson 112/116
- 6 min
Design the intermediate final skeleton.
Project: Order tracking
In this five-lesson project you'll build an order tracking application. Each lesson adds one piece, and by the final lesson you'll complete a working mini command-line application. You'll combine what you learned in previous groups: classes, List, lambda and Stream API.
First you'll design an Order class that holds id, customer and amount fields. In later lessons you'll manage the order collection with an OrderBook class, filter it with streams and save it to a file.
The OrderBook class
Rather than holding orders one by one, you collect them in a collection. The OrderBook class does this: it holds a List<Order> named orders and adds new orders through an add method. In later lessons you'll add listAll, largeOrders, save and load methods to this class.
This design creates a composition between Order and OrderBook: OrderBook contains an Order list. Order stays an independent class.
Practice Define the Order class
Define class Order with String id, String customer and int amount fields and an Order(String id, String customer, int amount) constructor that assigns them. In main, create an order with Order o = new Order("1", "Ayşe", 50); and print the customer name with System.out.println(o.customer);.
Starter code
// Define the Order class: id, customer, amount fields and constructor
// main: create an Order instance and print the customer nameSample 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;
}
}
public class Main {
public static void main(String[] args) {
Order o = new Order("1", "Ayşe", 50);
System.out.println(o.customer);
}
}Expected output
Ayşe
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the role of the Order class?
- Write orders to a file
- Hold order data Correct answer
- Filter orders
- Read user input
Explanation: Order holds the data of a single order such as id, customer and amount.
2 Complete the constructor body
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.____ = amount;
}
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.amount = amount;
}
Explanation: this.amount = amount copies the parameter into the field.
3 Match the project concepts
| Term | Meaning |
|---|---|
Order |
Single order data |
OrderBook |
Order collection |
id |
Order identifier |
amount |
Order total |