import javax.swing.
*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class FoodMenuAppWithDifferentRestaurants {
private JFrame frame;
private JPanel menuPanel;
private JTextField nameField;
private JTextField mobileField;
private JTextField addressField;
private JLabel totalLabel;
private JTextPane orderSummaryTextPane;
private Map<String, Double> foodPrices;
private double totalAmount;
public FoodMenuAppWithDifferentRestaurants() {
frame = new JFrame("Food Menu App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLayout(new BorderLayout());
frame.getContentPane().setBackground(new Color(216, 191, 216)); // Light Purple Color
menuPanel = new JPanel(new GridBagLayout());
menuPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
createMenuItems();
JPanel userInfoPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(20);
nameField.setFont(new Font("Arial", Font.PLAIN, 18)); // Increased font size
JLabel mobileLabel = new JLabel("Mobile Number:");
mobileField = new JTextField(20);
mobileField.setFont(new Font("Arial", Font.PLAIN, 18)); // Increased font size
JLabel addressLabel = new JLabel("Delivery Address:");
addressField = new JTextField(20);
addressField.setFont(new Font("Arial", Font.PLAIN, 18)); // Increased font size
userInfoPanel.add(nameLabel);
userInfoPanel.add(nameField);
userInfoPanel.add(mobileLabel);
userInfoPanel.add(mobileField);
userInfoPanel.add(addressLabel);
userInfoPanel.add(addressField);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
createPlaceOrderButton(buttonPanel);
frame.add(userInfoPanel, BorderLayout.NORTH);
frame.add(new JScrollPane(menuPanel), BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
totalAmount = 0.0;
totalLabel = new JLabel("Total Amount: $0.00");
totalLabel.setForeground(Color.BLUE);
totalLabel.setFont(new Font("Arial", Font.BOLD, 20));
orderSummaryTextPane = new JTextPane();
orderSummaryTextPane.setEditable(false);
orderSummaryTextPane.setFont(new Font("Arial", Font.PLAIN, 18));
orderSummaryTextPane.setForeground(Color.BLUE);
frame.add(totalLabel, BorderLayout.WEST);
frame.add(orderSummaryTextPane, BorderLayout.EAST);
foodPrices = new HashMap<>();
initializeFoodPrices();
frame.setVisible(true);
private void createMenuItems() {
String[] restaurants = {"Restaurant A", "Restaurant B", "Restaurant C", "Restaurant D"};
for (String restaurant : restaurants) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
JLabel restaurantLabel = new JLabel(restaurant);
restaurantLabel.setForeground(Color.BLUE);
restaurantLabel.setFont(new Font("Arial", Font.BOLD, 24)); // Increased font size
menuPanel.add(restaurantLabel, gbc);
String[] foodItems = getFoodItemsForRestaurant(restaurant);
for (String food : foodItems) {
JLabel foodLabel = new JLabel(food);
foodLabel.setFont(new Font("Arial", Font.PLAIN, 18)); // Increased font size
createFoodQuantitySpinner(food);
menuPanel.add(foodLabel, gbc);
}
}
private void initializeFoodPrices() {
// Initialize food prices for all the food items here
// Replace these placeholders with actual food prices
foodPrices.put("Burger", 5.0);
foodPrices.put("Pizza", 8.0);
foodPrices.put("Sushi", 10.0);
foodPrices.put("Pasta", 7.0);
foodPrices.put("Salad", 4.0);
foodPrices.put("Steak", 12.0);
foodPrices.put("Chicken Curry", 9.0);
foodPrices.put("Ice Cream", 3.0);
// Add prices for other food items
private String[] getFoodItemsForRestaurant(String restaurant) {
// Return an array of food items specific to each restaurant
// Modify this method to provide different food items for different restaurants
if (restaurant.equals("Restaurant A")) {
return new String[]{"Burger", "Pizza", "Sushi"};
} else if (restaurant.equals("Restaurant B")) {
return new String[]{"Pasta", "Salad", "Steak"};
} else if (restaurant.equals("Restaurant C")) {
return new String[]{"Burger", "Chicken Curry"};
} else if (restaurant.equals("Restaurant D")) {
return new String[]{"Pizza", "Ice Cream"};
} else {
return new String[]{};
}
private void createFoodQuantitySpinner(String foodItem) {
// Create and add a spinner for each food item
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
SpinnerModel model = new SpinnerNumberModel(0, 0, 10, 1);
JSpinner spinner = new JSpinner(model);
spinner.addChangeListener(e -> {
int quantity = (int) spinner.getValue();
double price = foodPrices.get(foodItem);
double itemTotal = quantity * price;
totalAmount += itemTotal;
totalLabel.setText("Total Amount: $" + String.format("%.2f", totalAmount));
});
menuPanel.add(spinner, gbc);
private void createPlaceOrderButton(JPanel buttonPanel) {
JButton placeOrderButton = new JButton("Place Order");
placeOrderButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String mobile = mobileField.getText();
String address = addressField.getText();
// Prepare the order summary
StringBuilder orderSummary = new StringBuilder();
orderSummary.append("Order Summary:\n");
orderSummary.append("Name: " + name + "\n");
orderSummary.append("Mobile Number: " + mobile + "\n");
orderSummary.append("Delivery Address: " + address + "\n");
// Add food items to the order summary
for (String restaurant : foodPrices.keySet()) {
String[] foodItems = getFoodItemsForRestaurant(restaurant);
for (String foodItem : foodItems) {
int quantity = (int) ((JSpinner)
menuPanel.getComponent(menuPanel.getComponentCount() - 1)).getValue();
if (quantity > 0) {
double price = foodPrices.get(foodItem);
orderSummary.append(foodItem + " from " + restaurant + ": " + quantity + " x $" +
price + " = $" + String.format("%.2f", quantity * price) + "\n");
orderSummary.append("Total Amount: $" + String.format("%.2f", totalAmount) + "\n");
orderSummary.append("THANK YOU!");
orderSummaryTextPane.setText(orderSummary.toString());
// Reset total amount
totalAmount = 0.0;
totalLabel.setText("Total Amount: $0.00");
});
buttonPanel.add(placeOrderButton);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new FoodMenuAppWithDifferentRestaurants();
});