1. Custom methods - Bringing it all together
In this final video, we'll see how to combine previous concepts we learned in this chapter using custom methods.
2. Integrating Concepts
Throughout this chapter, we've explored `Enums`, `Date`/`Time` operations, and `recursion`. Now, we'll bring these together in real-world methods to see how we can use them effectively in our Java applications.
We are working on an e-commerce site and will implement systems for ordering/tracking and providing loyalty discounts.
3. Defining Enum with methods
We define an `OrderStatus` `enum` to clearly represent two important stages of an order: `PLACED` and `DELIVERED`. Each `enum` constant holds a descriptive message.
The private field message stores this information, initialized through the constructor. The method `getMessage()` conveniently retrieves these descriptive messages.
Using `enums` this way enhances readability and ensures consistency in our application's status management.
4. Custom method for Date operations
An important calculation we need in an order system is calculating the estimated delivery time for the customer.
Here we define the class `OrderUtils` with the method `estimateDelivery()`.
The first parameter, `orderDate`, is a `LocalDate` marking when the order was placed.
The second parameter, `daysToDeliver`, specifies typical delivery duration in days.
Inside the method, `.plusDays()` calculates the estimated delivery date.
5. Recursive method for discount
Like many e-commerce sites, we reward returning customers with progressively increasing discounts—adding an extra `$5` discount each month—to encourage more purchases.
Here, the custom method `cumulativeDiscount()` recursively calculates the total discount. Each subscribed month adds `$5`.
The base case returns `0` if months are zero or negative, stopping infinite recursion.
The recursive step adds `$5` and calls itself with one less month.
This progressively simplifies the cumulative calculation.
6. Custom object
When a customer places an order, we need to store the order information into the system. We can achieve that by creating a custom Java object called `Order`. The `Order` object has three fields:
`id`: a unique order number to identify the order.
`status`: uses the `OrderStatus` `enum` we defined earlier, which can have values like `PLACED` or `DELIVERED`.
`orderDate`: a `LocalDate` object representing when the order was placed.
7. Customer object with toString() method
For both review and accounting purposes, we want to print each order in a way that is easy to read and process.
To achieve that, we can define a custom `.toString()` method in the `Order` class.
This method prints the order with its ID and order date together.
8. Main method
In this example, we create a `Main` class demonstrating the custom methods introduced earlier.
First, we initialize an `Order` object with an id and today's date using `LocalDate.now()`.
Next, we print order details using our custom `.toString()` method.
Finally, we calculate a three-month cumulative discount with our recursive method `cumulativeDiscount()` with parameter `3`.
9. Main method - output
The main method simulates creating an order in a practical scenario. It assumes the order is initially set to `PLACED`, with today's date as the order date and delivery expected in seven days.
The output shows the details of the `Order` object, including its `id` and date, calculated cumulative discount over three months, and current status from the `OrderStatus` enum. These results confirm our methods work as intended and interact smoothly together.
10. Let's practice!
Now it's your turn to practice!