FoodOrder.java

 avatar
Jpro1124
plain_text
a year ago
4.1 kB
15
Indexable
import java.util.ArrayList;
import java.util.List;

/**
 * FoodOrder class represents a complete food delivery order
 * This is the main product class that will be built by FoodOrderBuilder
 */
public class FoodOrder {
    private String customerName;
    private String restaurantName;
    private List<OrderItem> items;
    private double totalPrice;
    private String deliveryAddress;
    private String specialInstructions;
    
    // Private constructor to force use of builder
    private FoodOrder() {
        this.items = new ArrayList<>();
        this.totalPrice = 0.0;
    }
    
    // Getters
    public String getCustomerName() {
        return customerName;
    }
    
    public String getRestaurantName() {
        return restaurantName;
    }
    
    public List<OrderItem> getItems() {
        return new ArrayList<>(items); // Return copy to maintain encapsulation
    }
    
    public double getTotalPrice() {
        return totalPrice;
    }
    
    public String getDeliveryAddress() {
        return deliveryAddress;
    }
    
    public String getSpecialInstructions() {
        return specialInstructions;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("=== FOOD DELIVERY ORDER ===\n");
        sb.append("Customer: ").append(customerName).append("\n");
        sb.append("Restaurant: ").append(restaurantName).append("\n");
        sb.append("Delivery Address: ").append(deliveryAddress).append("\n");
        if (specialInstructions != null && !specialInstructions.trim().isEmpty()) {
            sb.append("Special Instructions: ").append(specialInstructions).append("\n");
        }
        sb.append("\n--- ORDER ITEMS ---\n");
        
        for (OrderItem item : items) {
            sb.append(item.toString()).append("\n");
        }
        
        sb.append("\n--- TOTAL: $").append(String.format("%.2f", totalPrice)).append(" ---\n");
        return sb.toString();
    }
    
    /**
     * Builder class for FoodOrder
     * Implements the Builder pattern for constructing FoodOrder objects
     */
    public static class FoodOrderBuilder {
        private FoodOrder order;
        
        public FoodOrderBuilder() {
            this.order = new FoodOrder();
        }
        
        public FoodOrderBuilder setCustomerName(String name) {
            order.customerName = name;
            return this;
        }
        
        public FoodOrderBuilder setRestaurantName(String name) {
            order.restaurantName = name;
            return this;
        }
        
        public FoodOrderBuilder addItem(OrderItem item) {
            if (item != null) {
                order.items.add(item);
                order.totalPrice += item.getPrice() * item.getQuantity();
            }
            return this;
        }
        
        public FoodOrderBuilder setDeliveryAddress(String address) {
            order.deliveryAddress = address;
            return this;
        }
        
        public FoodOrderBuilder setSpecialInstructions(String instructions) {
            order.specialInstructions = instructions;
            return this;
        }
        
        public FoodOrder build() {
            // Validation
            if (order.customerName == null || order.customerName.trim().isEmpty()) {
                throw new IllegalStateException("Customer name is required");
            }
            if (order.restaurantName == null || order.restaurantName.trim().isEmpty()) {
                throw new IllegalStateException("Restaurant name is required");
            }
            if (order.deliveryAddress == null || order.deliveryAddress.trim().isEmpty()) {
                throw new IllegalStateException("Delivery address is required");
            }
            if (order.items.isEmpty()) {
                throw new IllegalStateException("Order must contain at least one item");
            }
            
            return order;
        }
    }
}
Editor is loading...
Leave a Comment