package com.greenorange.promotion;

import java.util.Stack;


// Command 接口
interface Command {
    void execute();  // 执行命令
    void undo();     // 撤销命令
}

// Receiver 类，厨师执行具体的菜品制作命令
class Chef {
    public void makeBurger() {
        System.out.println("制作汉堡...");
    }

    public void makeFriedRice() {
        System.out.println("制作炒饭...");
    }

    public void makeFriedChicken() {
        System.out.println("制作炸鸡...");
    }
}


// 具体命令类
class MakeBurgerCommand implements Command {
    private Chef chef;

    public MakeBurgerCommand(Chef chef) {
        this.chef = chef;
    }

    @Override
    public void execute() {
        chef.makeBurger();  // 执行命令，制作汉堡
    }

    @Override
    public void undo() {
        System.out.println("撤销汉堡制作命令...");
    }
}

class MakeFriedRiceCommand implements Command {
    private Chef chef;

    public MakeFriedRiceCommand(Chef chef) {
        this.chef = chef;
    }

    @Override
    public void execute() {
        chef.makeFriedRice();  // 执行命令，制作炒饭
    }

    @Override
    public void undo() {
        System.out.println("撤销炒饭制作命令...");
    }
}

class MakeFriedChickenCommand implements Command {
    private Chef chef;

    public MakeFriedChickenCommand(Chef chef) {
        this.chef = chef;
    }

    @Override
    public void execute() {
        chef.makeFriedChicken();  // 执行命令，制作炸鸡
    }

    @Override
    public void undo() {
        System.out.println("撤销炸鸡制作命令...");
    }
}


// 服务员类，接收并执行命令
class Waiter {
    private Stack<Command> commandHistory = new Stack<>();  // 用栈来保存历史命令，方便撤销

    public void takeOrder(Command command) {
        command.execute();  // 执行命令
        commandHistory.push(command);  // 将命令存入历史记录
    }

    public void undoLastOrder() {
        if (!commandHistory.isEmpty()) {
            Command lastCommand = commandHistory.pop();  // 获取并移除栈顶命令
            lastCommand.undo();  // 执行撤销操作
        } else {
            System.out.println("没有可撤销的命令！");
        }
    }

    // 宏命令：一次性下多个菜品
    public void placeMultipleOrders(Command[] commands) {
        for (Command command : commands) {
            takeOrder(command);
        }
    }
}



// 客户端模拟点餐过程
public class Client {
    public static void main(String[] args) {
        // 创建厨师
        Chef chef = new Chef();

        // 创建具体命令
        Command makeBurger = new MakeBurgerCommand(chef);
        Command makeFriedRice = new MakeFriedRiceCommand(chef);
        Command makeFriedChicken = new MakeFriedChickenCommand(chef);

        // 创建服务员
        Waiter waiter = new Waiter();

        // 用户下单
        waiter.takeOrder(makeBurger);
        waiter.takeOrder(makeFriedRice);

        // 用户撤销上一条订单（炒饭）
        waiter.undoLastOrder();

        // 用户一次性下多个菜品（宏命令）
        Command[] orders = {makeBurger, makeFriedChicken};
        waiter.placeMultipleOrders(orders);
    }
}

