Java
Project design
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.
Exercise 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);.
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;
}
}
public class Main {
public static void main(String[] args) {
Order o = new Order("1", "Ayşe", 50);
System.out.println(o.customer);
}
}
Test yourself
-
What is the role of the Order class?
- Write orders to a file
- Hold order data
- Filter orders
- Read user input
Show answer
Hold order data — Order holds the data of a single order such as id, customer and amount.
-
Complete the constructor body
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.____ = amount;
}
Show answer
Order(String id, String customer, int amount) {
this.id = id;
this.customer = customer;
this.amount = amount;
}
-
Match the project concepts
- Order
- Single order data
- OrderBook
- Order collection
- id
- Order identifier
- amount
- Order total
Next lesson: OOP model