Input/Output and Streams in Java
Alex Liu
Software Development Engineer
Goal: build clear, reusable, and maintainable methods
Example program:
OrderStatus
system with order tracking methodsOrderStatus
Enum
to represent order statesenum OrderStatus { PLACED("Order placed successfully"), DELIVERED("Order delivered");
private final String message; OrderStatus(String message) { this.message = message; } public String getMessage() { return message; } }
orderDate
to estimate when the order will deliver to customerimport java.time.LocalDate;
public class OrderUtils {
public static LocalDate estimateDelivery(LocalDate orderDate,
int daysToDeliver) {
return orderDate.plusDays(daysToDeliver);
}
}
public class DiscountCalculator {
public static double cumulativeDiscount(int months) {
if (months <= 0) return 0;
return 5 + cumulativeDiscount(months - 1); // $5 discount per month
}
}
class Order {
int orderId;
OrderStatus orderStatus;
LocalDate orderDate;
Order(int orderId, LocalDate orderDate) {
this.orderId = orderId;
this.orderDate = orderDate;
this.orderStatus = orderStatus;
}
toString()
method to define how the Order
object will be printed.public String toString() {
return "Order[id=" + orderId + ", date=" + orderDate + "]";
}
}
Order[id=10015, date=2025-03-19]
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); } }
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