Untitled

 avatar
unknown
plain_text
5 months ago
6.8 kB
3
Indexable
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class MainApp {
    private JFrame frame;
    private JPanel mainPanel;
    private CardLayout cardLayout;
    private List<String> cart = new ArrayList<>();
    private FoodResource selectedResource;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MainApp());
    }

    public MainApp() {
        frame = new JFrame("Food Ordering System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);

        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        frame.add(mainPanel);

        mainPanel.add(createWelcomeScreen(), "Welcome");
        mainPanel.add(createCartScreen(), "Cart");
        mainPanel.add(createConfirmationScreen(), "Confirmation");

        frame.setVisible(true);
    }

    private JPanel createWelcomeScreen() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel welcomeLabel = new JLabel("Welcome to the Food Ordering System");
        welcomeLabel.setFont(new Font("Arial", Font.BOLD, 16));

        String[] restaurantNames = {"Food Bank 1", "Food Bank 2 (24/7)", "Food Bank 3"};
        JComboBox<String> restaurantSelector = new JComboBox<>(restaurantNames);

        JButton orderButton = new JButton("Order");
        orderButton.addActionListener(e -> {
            String selectedRestaurant = (String) restaurantSelector.getSelectedItem();
            if (selectedRestaurant != null) {
                selectedResource = getFoodResourceByName(selectedRestaurant);
                cardLayout.show(mainPanel, "Cart");
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(welcomeLabel, gbc);

        gbc.gridy = 1;
        panel.add(restaurantSelector, gbc);

        gbc.gridy = 2;
        panel.add(orderButton, gbc);

        return panel;
    }

    private FoodResource getFoodResourceByName(String name) {
        switch (name) {
            case "Food Bank 1":
                return new FoodResource("Food Bank 1","Marketing St","Food Bank","9:00 AM - 5:00 PM",true, new String[]{"Apple", "Banana", "Orange"});
            case "Food Bank 2 (24/7)": // Open 24/7
                return new FoodResource("Food Bank 2", "Marketing St","Food Bank","9:00 AM - 5:00 PM",true, new String[]{"Apple", "Banana", "Orange"});
            case "Food Bank 3":
                return new FoodResource("Food Bank 3", "Marketing St","Food Bank","9:00 AM - 5:00 PM",true, new String[]{"Apple", "Banana", "Orange"});
            default:
                return null;
        }
    }

    private JPanel createCartScreen() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel cartLabel = new JLabel("Your Cart");
        cartLabel.setFont(new Font("Arial", Font.BOLD, 16));

        JTextArea cartArea = new JTextArea(10, 30);
        cartArea.setEditable(false);
        updateCartText(cartArea);
        System.out.println("test: " );

        JComboBox<String> foodSelector = new JComboBox<>();
        // Check if availableFoods is null or empty before adding items to JComboBox
        if (selectedResource != null && selectedResource.getAvailableFoods() != null) {
            String[] foods = selectedResource.getAvailableFoods();
            if (foods.length > 0) {
                for (String food : foods) {
                    foodSelector.addItem(food);
                }
            } else {
                foodSelector.addItem("No food items available");
            }
        } else {
            foodSelector.addItem("No food items available");
        }


        JButton addButton = new JButton("Add Item");
        addButton.addActionListener(e -> {
            String foodItem = (String) foodSelector.getSelectedItem();
            if (foodItem != null) {
                cart.add(foodItem);
                updateCartText(cartArea);
            }
        });

        JButton removeButton = new JButton("Remove Item");
        removeButton.addActionListener(e -> {
            String item = JOptionPane.showInputDialog(frame, "Enter item to remove:");
            if (cart.remove(item)) {
                updateCartText(cartArea);
            } else {
                JOptionPane.showMessageDialog(frame, "Item not found in cart.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        });

        JButton confirmButton = new JButton("Confirm Order");
        confirmButton.addActionListener(e -> cardLayout.show(mainPanel, "Confirmation"));

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(cartLabel, gbc);

        gbc.gridy = 1;
        panel.add(new JScrollPane(cartArea), gbc);

        gbc.gridy = 2;
        panel.add(foodSelector, gbc);

        gbc.gridy = 3;
        panel.add(addButton, gbc);

        gbc.gridy = 4;
        panel.add(removeButton, gbc);

        gbc.gridy = 5;
        panel.add(confirmButton, gbc);

        return panel;
    }

    private void updateCartText(JTextArea cartArea) {
        StringBuilder cartText = new StringBuilder();
        for (String item : cart) {
            cartText.append(item).append("\n");
        }
        cartArea.setText(cartText.toString());
    }

    private JPanel createConfirmationScreen() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel confirmLabel = new JLabel("Confirm Order");
        confirmLabel.setFont(new Font("Arial", Font.BOLD, 16));

        JLabel statusLabel = new JLabel("Checking if the selected food bank is open...");
        statusLabel.setFont(new Font("Arial", Font.PLAIN, 14));

        JButton confirmButton = new JButton("Confirm");
        confirmButton.addActionListener(e -> {
            if (selectedResource != null) {
                String status = selectedResource.isOpen() ? "open" : "closed";
                JOptionPane.showMessageDialog(frame, "The selected food bank is " + status + ".", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(confirmLabel, gbc);

        gbc.gridy = 1;
        panel.add(statusLabel, gbc);

        gbc.gridy = 2;
        panel.add(confirmButton, gbc);

        return panel;
    }
}
Editor is loading...
Leave a Comment