import java.util.*;

public class Order {
    private int id; // 订单编号
    private String customerName; // 订餐人姓名
    private LinkedList<Dish> dishes; // 菜品列表
    private double totalPrice; // 合计价格
    private Date orderTime; // 下单时间
    private Date receiveTime; // 确认收货时间
    private int state;// 0未出单 1已出单未收货 2已收货

    public Order(int id, String customerName, LinkedList<Dish> dishes) {
        this.id = id;
        this.customerName = customerName;
        this.dishes = dishes;
        this.totalPrice = calculateTotalPrice();
        this.orderTime = new Date();
        this.receiveTime = null;
        this.state = 0;
    }

    // 计算合计价格
    private double calculateTotalPrice() {
        double totalPrice = 0;
        for (Dish dish : this.dishes) {
            totalPrice += dish.getPrice() * dish.getQuantity();
        }
        return totalPrice;
    }

    // 确认收货
    public void confirmReceive() {
        this.receiveTime = new Date();
    }

    // 获取订单编号
    public int getId() {
        return this.id;
    }

    // 获取订餐人姓名
    public String getCustomerName() {
        return this.customerName;
    }

    // 获取菜品列表
    public LinkedList<Dish> getDishes() {
        return this.dishes;
    }

    // 获取合计价格
    public double getTotalPrice() {
        return this.totalPrice;
    }

    // 获取下单时间
    public Date getOrderTime() {
        return this.orderTime;
    }

    // 获取确认收货时间
    public Date getReceiveTime() {
        return this.receiveTime;
    }

    // 获取状态
    public int getState() {
        return this.state;
    }

    // 设置状态
    public void setState(int state) {
        this.state=state;
    }

    // string
    public String toString() {
        String string = '{订单编号:' + id +
                ',订餐人姓名:' + customerName +
                ',菜品列表:' + dishes +
                ',合计价格:' + totalPrice +
                ',下单时间:' + orderTime +
                ',确认收货时间:' + receiveTime +
                ',订单状态:' + state
                + '}';
        return string;
    }

}

// 该代码片段使用以下基本原理或技术内容:

1. **面向对象编程**:使用类和对象来实现订单的管理和操作。

2. **集合**:使用LinkedList来存储菜品列表,方便添加和删除菜品。

3. **封装**:将订单的属性私有化并提供公共的getter和setter方法来访问和修改属性,保证了数据的安全性和灵活性。

4. **构造方法**:使用构造方法来初始化订单对象,方便快捷地创建订单对象。

5. **继承**:由于该代码片段没有显示继承其他类,因此不涉及继承的相关知识。

6. **多态**:由于该代码片段没有涉及到多态的应用,因此不涉及多态的相关知识。
Java 外卖点餐管理系统 - 订单类实现

原文地址: https://www.cveoy.top/t/topic/oWaR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录