เมธอดแบบกำหนดเอง - รวมทุกอย่างเข้าด้วยกัน

Input/Output และ Streams ใน Java

Alex Liu

Software Development Engineer

การผสานแนวคิด

  • นำมาผสมผสานและใช้งาน:
    • Enums
    • วันที่และเวลา
    • Recursion
  • เป้าหมาย: สร้างเมธอดที่ชัดเจน นำกลับมาใช้ได้ และบำรุงรักษาง่าย

  • โปรแกรมตัวอย่าง:

    • ระบบอีคอมเมิร์ซ
      • มีระบบ OrderStatus พร้อมเมธอดติดตามคำสั่งซื้อ
      • มีอัลกอริทึมส่วนลดสำหรับลูกค้าประจำ
Input/Output และ Streams ใน Java

การกำหนด Enum พร้อมเมธอด

  • กำหนด OrderStatus Enum เพื่อแทนสถานะของคำสั่งซื้อ
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 และ Streams ใน Java

เมธอดแบบกำหนดเองสำหรับการจัดการวันที่

  • ใช้ orderDate เพื่อประมาณวันที่ลูกค้าจะได้รับสินค้า
    • เมธอด .plusDays() ใช้เพิ่มจำนวนวันให้กับวันที่ที่กำหนด
import java.time.LocalDate;

public class OrderUtils {
    public static LocalDate estimateDelivery(LocalDate orderDate, 
                                            int daysToDeliver) {
        return orderDate.plusDays(daysToDeliver);
    }
}
Input/Output และ Streams ใน Java

เมธอดแบบ recursive สำหรับคำนวณส่วนลด

  • นโยบายรางวัล: เพิ่มส่วนลดทีละเดือนเพื่อตอบแทนลูกค้าที่กลับมาซื้อซ้ำ
    • ใช้เมธอดแบบ recursive คำนวณส่วนลดสะสม:
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 และ Streams ใน Java

ออบเจ็กต์แบบกำหนดเอง

  • สร้างคลาส Order เพื่อเก็บข้อมูลทั้งหมดที่เกี่ยวกับคำสั่งซื้อ
    • เก็บข้อมูลสำคัญ เช่น id, วันที่ และ สถานะปัจจุบัน
class Order {
    int orderId;
    OrderStatus orderStatus;
    LocalDate orderDate;

    Order(int orderId, LocalDate orderDate) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.orderStatus = orderStatus;
    }
Input/Output และ Streams ใน Java

ออบเจ็กต์ลูกค้าพร้อมเมธอด toString()

  • ต้องการแสดงคำสั่งซื้อในรูปแบบที่พนักงานอ่านและประมวลผลได้ง่าย
    • กำหนดเมธอด toString() เพื่อระบุวิธีแสดงผลออบเจ็กต์ Order
public String toString() {
        return "Order[id=" + orderId + ", date=" + orderDate + "]";
    }
}
  • ตัวอย่างผลลัพธ์จากการเรียก .toString():
    Order[id=10015, date=2025-03-19]
    
Input/Output และ Streams ใน Java

เมธอด main

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 และ Streams ใน Java

เมธอด main - ผลลัพธ์

  • เมธอด main นี้จะทำงานเมื่อมีการสั่งซื้อใหม่ โดยมีข้อมูลดังนี้:
    • ผู้ใช้สมัครสมาชิกมาแล้ว 3 เดือน
    • เวลาจัดส่งโดยประมาณคือ 7 วัน
    • สถานะเริ่มต้นของคำสั่งซื้อจะเป็น PLACED
Order[id=101, date=2025-03-09]
Estimated Delivery: 2025-03-16
Order placed successfully
Total Discount: $15.0
Input/Output และ Streams ใน Java

มาฝึกกันเถอะ!

Input/Output และ Streams ใน Java

Preparing Video For Download...