Загрузка данных


import vending.VendingMachine;
import command.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        VendingMachine vm = new VendingMachine();
        Scanner sc = new Scanner(System.in);
        Command lastCommand = null;  // запоминаем последнюю команду

        System.out.print("Внесите сумму: ");
        int initial = sc.nextInt();
        InsertMoneyCommand initCmd = new InsertMoneyCommand(vm, initial);
        initCmd.execute();
        lastCommand = initCmd;

        while (true) {
            System.out.print("\n1-выбрать 2-добавить деньги 3-отмена последней операции 4-выход: ");
            int act = sc.nextInt();
            if (act == 1) {
                System.out.print("ID товара: ");
                SelectProductCommand cmd = new SelectProductCommand(vm, sc.nextInt());
                cmd.execute();
                lastCommand = cmd;
            } else if (act == 2) {
                System.out.print("Сумма: ");
                InsertMoneyCommand cmd = new InsertMoneyCommand(vm, sc.nextInt());
                cmd.execute();
                lastCommand = cmd;
            } else if (act == 3) {
                if (lastCommand != null) {
                    lastCommand.undo();
                    // После отмены показываем актуальный список товаров
                    Inventory.getInstance().showProducts();
                } else {
                    System.out.println("Нет операции для отмены.");
                }
            } else {
                System.out.println("Выход.");
                break;
            }
        }
        sc.close();
    }
}