Custom methods - Bringing it all together

Input/Output and Streams in Java

Alex Liu

Software Development Engineer

Integrating Concepts

  • Combine and use:
    • Enums
    • Date and Time
    • Recursion
  • Goal: build clear, reusable, and maintainable methods

  • Example program:

    • An e-commerce site
      • with practical OrderStatus system with order tracking methods
      • with loyalty discount algorithm.
Input/Output and Streams in Java

Defining Enum with methods

  • Define OrderStatus Enum to represent order states
enum OrderStatus {
    PLACED("Order placed successfully"),
    DELIVERED("Order delivered");

private final String message; OrderStatus(String message) { this.message = message; } public String getMessage() { return message; } }
Input/Output and Streams in Java

Custom method for Date operations

  • Use orderDate to estimate when the order will deliver to customer
    • User .plusDays() method adds days to a given date
import java.time.LocalDate;

public class OrderUtils {
    public static LocalDate estimateDelivery(LocalDate orderDate, 
                                            int daysToDeliver) {
        return orderDate.plusDays(daysToDeliver);
    }
}
Input/Output and Streams in Java

Recursive method for discount

  • Reward policy: Incrementing discounts month-by-month to reward return customers
    • Use recursive method to calculate the cumulative discount:
public class DiscountCalculator {
    public static double cumulativeDiscount(int months) {
        if (months <= 0) return 0;
        return 5 + cumulativeDiscount(months - 1); // $5 discount per month
    }
}
Input/Output and Streams in Java

Custom object

  • Create an Order class which contain all order related information
    • Contain important information about order like id, date and current status.
class Order {
    int orderId;
    OrderStatus orderStatus;
    LocalDate orderDate;

    Order(int orderId, LocalDate orderDate) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.orderStatus = orderStatus;
    }
Input/Output and Streams in Java

Customer object with toString() method

  • We want to print the order in a way that the employee is easy to read or process
    • Define toString() method to define how the Order object will be printed.
public String toString() {
        return "Order[id=" + orderId + ", date=" + orderDate + "]";
    }
}
  • Sample output by calling .toString():
    Order[id=10015, date=2025-03-19]
    
Input/Output and Streams in Java

Main method

public class Main {
    public static void main(String[] args) {
        Order order = new Order(101, LocalDate.now());
        System.out.println(order.toString());

LocalDate delivery = OrderUtils.estimateDelivery(order.orderDate, 7); System.out.println("Estimated Delivery: " + delivery); System.out.println(OrderStatus.PLACED);
double discount = DiscountCalculator.cumulativeDiscount(3); System.out.println("Total Discount: $" + discount); } }
Input/Output and Streams in Java

Main method - output

  • This main method is executed when a new order is placed with following information:
    • The user has subscribed for 3 months already
    • The estimate delivery time is 7 days
    • Order status will initially be PLACED
Order[id=101, date=2025-03-09]
Estimated Delivery: 2025-03-16
Order placed successfully
Total Discount: $15.0
Input/Output and Streams in Java

Let's practice!

Input/Output and Streams in Java

Preparing Video For Download...