DONEE ITPF
unknown
java
5 months ago
26 kB
3
Indexable
import java.awt.*; import java.util.LinkedHashMap; import java.util.Map; import javax.swing.*; public class casestudy extends JFrame { private final Map<String, Integer> orderMap; private final Map<String, Double> priceMap; private final Map<String, Integer> stockMap; private JLabel totalLabel; private double totalAmount = 0.0; private JPanel orderPanel; private JPanel cardPanel; private CardLayout cardLayout; private static final double ESPRESSO_SHOT_PRICE = 10.0; private static final double CARAMEL_DRIZZLE_PRICE = 10.0; private static final double STRAWBERRY_PURE = 10.0; private static final double BLUEBERRY_PURE = 10.0; private String userName; public casestudy() { setTitle("Calle Brews"); setSize(800, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); priceMap = new LinkedHashMap<>(); orderMap = new LinkedHashMap<>(); stockMap = new LinkedHashMap<>(); cardLayout = new CardLayout(); cardPanel = new JPanel(cardLayout); add(cardPanel, BorderLayout.CENTER); cardPanel.add(createWelcomePanel(), "WelcomePanel"); cardLayout.show(cardPanel, "WelcomePanel"); setVisible(true); } private JPanel createWelcomePanel() { JPanel welcomePanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.CENTER; JLabel nameLabel = new JLabel("Enter your name:"); nameLabel.setFont(new Font("Arial", Font.BOLD, 18)); JTextField nameField = new JTextField(15); JButton submitButton = new JButton("Proceed"); welcomePanel.add(nameLabel, gbc); gbc.gridy++; welcomePanel.add(nameField, gbc); gbc.gridy++; welcomePanel.add(submitButton, gbc); submitButton.addActionListener(e -> { userName = nameField.getText(); if (!userName.isEmpty()) { setTitle("Calle Brews - Welcome, " + userName); cardPanel.add(createCategoryPanel(userName), "CategoryPanel"); cardLayout.show(cardPanel, "CategoryPanel"); } else { JOptionPane.showMessageDialog(this, "Please enter a valid name.", "Error", JOptionPane.ERROR_MESSAGE); } }); return welcomePanel; } private JPanel createCategoryPanel(String userName) { JPanel categoryPanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(20, 20, 20, 20); JLabel categoryLabel = new JLabel("Choose a category: "); categoryLabel.setFont(new Font("Arial", Font.BOLD, 18)); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; categoryPanel.add(categoryLabel, gbc); JButton coffeeBasedButton = new JButton("Coffee-Based"); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; categoryPanel.add(coffeeBasedButton, gbc); JButton nonCoffeeBasedButton = new JButton("Non-Coffee Based"); gbc.gridx = 1; gbc.gridy = 1; categoryPanel.add(nonCoffeeBasedButton, gbc); JButton refreshersButton = new JButton("Refreshers"); gbc.gridx = 0; gbc.gridy = 2; categoryPanel.add(refreshersButton, gbc); JButton foodsButton = new JButton("Foods"); gbc.gridx = 1; gbc.gridy = 2; categoryPanel.add(foodsButton, gbc); Dimension buttonSize = new Dimension(300, 180); coffeeBasedButton.setPreferredSize(buttonSize); nonCoffeeBasedButton.setPreferredSize(buttonSize); refreshersButton.setPreferredSize(buttonSize); foodsButton.setPreferredSize(buttonSize); Color buttonColor = new Color(234, 210, 168); coffeeBasedButton.setBackground(buttonColor); nonCoffeeBasedButton.setBackground(buttonColor); refreshersButton.setBackground(buttonColor); foodsButton.setBackground(buttonColor); coffeeBasedButton.addActionListener(e -> loadMenu("Coffee-Based")); nonCoffeeBasedButton.addActionListener(e -> loadMenu("Non-Coffee Based")); refreshersButton.addActionListener(e -> loadMenu("Refreshers")); foodsButton.addActionListener(e -> loadMenu("Food")); return categoryPanel; } private void loadMenu(String category) { switch (category) { case "Coffee-Based": loadCoffeeBasedMenu(); break; case "Refreshers": loadRefreshersMenu(); break; case "Non-Coffee Based": loadNonCoffeeBasedMenu(); break; case "Food": loadFoodMenu(); break; default: break; } cardLayout.show(cardPanel, "MenuPanel"); } private void loadCoffeeBasedMenu() { JPanel coffeePanel = createCategoryItemsPanel("Coffee-Based", new String[][]{ {"Iced Spanish Latte", "49.00", "30"}, {"Vanilla Latte", "49.00", "30"}, {"Salted Caramel", "49.00", "30"}, {"Caramel Macchiato", "49.00", "30"}, {"Cafe Mocha", "49.00", "20"}, {"Berry Latte", "49.00", "30"}, {"Dirty Matcha", "49.00", "30"}, {"Sea Salt Latte", "49.00", "30"} }); JPanel orderPanel = createOrderPanel(); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, coffeePanel, orderPanel); splitPane.setDividerLocation(900); cardPanel.add(splitPane, "MenuAndOrderPanel"); cardLayout.show(cardPanel, "MenuAndOrderPanel"); } private void loadRefreshersMenu() { JPanel refreshersPanel = createCategoryItemsPanel("Refreshers", new String[][]{ {"Apple Berry", "49.00", "20"}, {"Mogu - Mogu", "49.00", "20"}, {"Lipton", "49.00", "20"}, {"Lychee", "49.00", "20"} }); JScrollPane scrollPane = new JScrollPane(refreshersPanel); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); Dimension fixedSize = new Dimension(900, 0); scrollPane.setPreferredSize(fixedSize); scrollPane.setMinimumSize(fixedSize); scrollPane.setMaximumSize(fixedSize); JPanel orderPanel = createOrderPanel(); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, orderPanel); splitPane.setDividerLocation(fixedSize.width); splitPane.setResizeWeight(0.0); cardPanel.add(splitPane, "MenuAndOrderPanel"); cardLayout.show(cardPanel, "MenuAndOrderPanel"); } private void loadNonCoffeeBasedMenu() { JPanel nonCoffeePanel = createCategoryItemsPanel("Non-Coffee Based", new String[][]{ {"Matcha Berry", "49.00", "20"}, {"Strawberry Milk", "49.00", "20"}, {"Blueberry Milk", "49.00", "20"}, {"Strawberry Cinnamon", "49.00", "20"}, {"Blueberry Cinnamon", "49.00", "20"}, {"Matcha Latte", "49.00", "20"}, {"Meiji Apollo", "49.00", "20"}, {"Salted Oreo Latte", "49.00", "20"} }); JPanel orderPanel = createOrderPanel(); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, nonCoffeePanel, orderPanel); splitPane.setDividerLocation(900); cardPanel.add(splitPane, "MenuAndOrderPanel"); cardLayout.show(cardPanel, "MenuAndOrderPanel"); } private void loadFoodMenu() { JPanel foodPanel = createCategoryItemsPanel("Food", new String[][]{ {"Yema Cake", "69.00", "30"}, {"Chicken Poppers", "89.00", "30"}, {"Java Rice With Shanghai", "89.00", "30"}, {"Fries", "120.00", "10"} }); JPanel orderPanel = createOrderPanel(); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, foodPanel, orderPanel); splitPane.setDividerLocation(900); cardPanel.add(splitPane, "MenuAndOrderPanel"); cardLayout.show(cardPanel, "MenuAndOrderPanel"); } private JPanel createCategoryItemsPanel(String category, String[][] menuItems) { JPanel categoryPanel = new JPanel(); categoryPanel.setBorder(BorderFactory.createTitledBorder(category)); categoryPanel.setLayout(new GridLayout(0, 1, 10, 10)); for (String[] item : menuItems) { String itemName = item[0]; double price = Double.parseDouble(item[1]); priceMap.put(itemName, price); stockMap.put(itemName, Integer.parseInt(item[2])); JButton menuButton = new JButton(itemName + " - ₱" + price); menuButton.setFont(new Font("Arial", Font.BOLD, 18)); menuButton.setPreferredSize(new Dimension(250, 50)); menuButton.setBackground(new Color(196, 164, 132)); menuButton.addActionListener(e -> askQuantityAndAddItem(itemName, price)); categoryPanel.add(menuButton); } return categoryPanel; } private JPanel createOrderPanel() { JPanel rightPanel = new JPanel(new BorderLayout()); orderPanel = new JPanel(); orderPanel.setLayout(new BoxLayout(orderPanel, BoxLayout.Y_AXIS)); JScrollPane scrollPane = new JScrollPane(orderPanel); scrollPane.setPreferredSize(new Dimension(900, 500)); JLabel titleLabel = new JLabel("Orders", JLabel.CENTER); titleLabel.setFont(new Font("Arial", Font.BOLD, 25)); JPanel panel = new JPanel(new BorderLayout()); panel.add(titleLabel, BorderLayout.NORTH); panel.add(scrollPane, BorderLayout.CENTER); rightPanel.add(panel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(1, 2, 10, 10)); JButton decreaseButton = new JButton("Decrease Quantity"); decreaseButton.setPreferredSize(new Dimension(300, 60)); decreaseButton.setFont(new Font("Arial", Font.BOLD, 20)); decreaseButton.addActionListener(e -> decreaseSelectedItem()); JButton backToCategoryButton = new JButton("Menu"); backToCategoryButton.setPreferredSize(new Dimension(300, 60)); backToCategoryButton.setFont(new Font("Arial", Font.BOLD, 20)); backToCategoryButton.addActionListener(e -> cardLayout.show(cardPanel, "CategoryPanel")); buttonPanel.add(decreaseButton); buttonPanel.add(backToCategoryButton); rightPanel.add(buttonPanel, BorderLayout.NORTH); JPanel totalPanel = new JPanel(new BorderLayout()); totalLabel = new JLabel("Total: ₱0.00", SwingConstants.RIGHT); totalLabel.setFont(new Font("Arial", Font.BOLD, 20)); totalPanel.add(totalLabel, BorderLayout.NORTH); JButton checkoutButton = new JButton("Checkout"); checkoutButton.setPreferredSize(new Dimension(400, 100)); checkoutButton.setFont(new Font("Arial", Font.BOLD, 16)); checkoutButton.addActionListener(e -> checkout()); totalPanel.add(checkoutButton, BorderLayout.SOUTH); rightPanel.add(totalPanel, BorderLayout.SOUTH); return rightPanel; } private void askQuantityAndAddItem(String itemName, double price) { int availableStock = stockMap.get(itemName); String quantityStr = JOptionPane.showInputDialog(this, "Enter quantity for " + itemName + " (Available: " + availableStock + "):"); if (quantityStr != null) { try { int quantity = Integer.parseInt(quantityStr); if (quantity > 0 && quantity <= availableStock) { String selectedAddOn = ""; double addOnPrice = 0.0; boolean isCoffee = isCoffeeBased(itemName); boolean isNonCoffee = isNonCoffeeBased(itemName); boolean isRefresher = isRefresherBased(itemName); if (isCoffee || isNonCoffee) { String addOnMessage = "Select add-ons for " + itemName + ": \n" + "1. Espresso Shot - ₱" + ESPRESSO_SHOT_PRICE + "\n" + "2. Caramel Drizzle - ₱" + CARAMEL_DRIZZLE_PRICE + "\n" + "3. None\n" + "Please enter your selection (1-3):"; String addOnSelection = JOptionPane.showInputDialog(this, addOnMessage); if (addOnSelection != null) { switch (addOnSelection.trim()) { case "1": selectedAddOn = "Extra shot"; addOnPrice = ESPRESSO_SHOT_PRICE; break; case "2": selectedAddOn = "Caramel drizzle"; addOnPrice = CARAMEL_DRIZZLE_PRICE; break; case "3": selectedAddOn = "No add-ons"; addOnPrice = 0.0; break; default: JOptionPane.showMessageDialog(this, "Invalid selection. No add-ons will be added.", "Error", JOptionPane.ERROR_MESSAGE); } } } else if (isRefresher) { String addOnMessage = "Select add-ons for " + itemName + ": \n" + "1. Strawberry Pure - ₱" + STRAWBERRY_PURE + "\n" + "2. Blueberry Pure - ₱" + BLUEBERRY_PURE + "\n" + "3. None\n" + "Please enter your selection (1-3):"; String addOnSelection = JOptionPane.showInputDialog(this, addOnMessage); if (addOnSelection != null) { switch (addOnSelection.trim()) { case "1": selectedAddOn = "Strawberry Pure"; addOnPrice = STRAWBERRY_PURE; break; case "2": selectedAddOn = "Blueberry Pure"; addOnPrice = BLUEBERRY_PURE; break; case "3": selectedAddOn = "No add-ons"; addOnPrice = 0.0; break; default: JOptionPane.showMessageDialog(this, "Invalid selection. No add-ons will be added.", "Error", JOptionPane.ERROR_MESSAGE); selectedAddOn = "No add-ons"; addOnPrice = 0.0; } } else { selectedAddOn = "No add-ons"; addOnPrice = 0.0; } } String orderDetails = itemName + (selectedAddOn.isEmpty() ? "" : " with " + selectedAddOn); orderMap.put(orderDetails, orderMap.getOrDefault(orderDetails, 0) + quantity); totalAmount += (price + addOnPrice) * quantity; stockMap.put(itemName, availableStock - quantity); updateOrderPanel(); updateTotal(); } else { JOptionPane.showMessageDialog(this, "Invalid quantity. Out of stock.", "Error", JOptionPane.ERROR_MESSAGE); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Please enter a valid number for quantity.", "Invalid Input", JOptionPane.ERROR_MESSAGE); } } } private boolean isCoffeeBased(String itemName) { return itemName.equals("Iced Spanish Latte") || itemName.equals("Vanilla Latte") || itemName.equals("Salted Caramel") || itemName.equals("Caramel Macchiato") || itemName.equals("Cafe Mocha") || itemName.equals("Berry Latte") || itemName.equals("Dirty Matcha") || itemName.equals("Sea Salt Latte"); } private boolean isNonCoffeeBased(String itemName) { return itemName.equals("Matcha Berry") || itemName.equals("Strawberry Milk") || itemName.equals("Blueberry Milk") || itemName.equals("Strawberry Cinnamon") || itemName.equals("Blueberry Cinnamon") || itemName.equals("Matcha Latte") || itemName.equals("Meiji Apollo") || itemName.equals("Salted Oreo Latte"); } private boolean isRefresherBased(String itemName) { return itemName.equals("Apple Berry") || itemName.equals("Mogu - Mogu") || itemName.equals("Lipton") || itemName.equals("Lychee"); } private void decreaseSelectedItem() { if (orderMap.isEmpty()) { JOptionPane.showMessageDialog(this, "No items to decrease.", "Error", JOptionPane.ERROR_MESSAGE); return; } String[] items = orderMap.keySet().toArray(new String[0]); String selectedItem = (String) JOptionPane.showInputDialog(this, "Select an item to decrease", "Decrease Item", JOptionPane.QUESTION_MESSAGE, null, items, items[0]); if (selectedItem != null) { String quantityStr = JOptionPane.showInputDialog(this, "Enter the number of " + selectedItem + " to decrease:"); if (quantityStr != null) { try { int quantityToDecrease = Integer.parseInt(quantityStr); if (quantityToDecrease > 0) { decreaseItem(selectedItem, quantityToDecrease); } else { JOptionPane.showMessageDialog(this, "Quantity must be greater than 0.", "Invalid Quantity", JOptionPane.ERROR_MESSAGE); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Please enter a valid number for quantity.", "Invalid Input", JOptionPane.ERROR_MESSAGE); } } } } // FOR DECREASING ITEMS private void decreaseItem(String orderDetails, int quantityToDecrease) { int currentQuantity = orderMap.getOrDefault(orderDetails, 0); if (currentQuantity > 0) { int newQuantity = currentQuantity - quantityToDecrease; if (newQuantity < 0) { JOptionPane.showMessageDialog(this, "Cannot decrease quantity below zero.", "Error", JOptionPane.ERROR_MESSAGE); return; } if (newQuantity == 0) { orderMap.remove(orderDetails); } else { orderMap.put(orderDetails, newQuantity); } double basePrice = priceMap.get(orderDetails.split(" with ")[0]); double addOnPrice = 0.0; if (orderDetails.contains(" with ")) { String addOn = orderDetails.split(" with ")[1]; switch (addOn.trim()) { case "Extra shot": addOnPrice = ESPRESSO_SHOT_PRICE; break; case "Caramel drizzle": addOnPrice = CARAMEL_DRIZZLE_PRICE; break; case "No add-ons": addOnPrice = 0.0; break; } } totalAmount -= (basePrice + addOnPrice) * quantityToDecrease; updateOrderPanel(); } else { JOptionPane.showMessageDialog(this, "No items to decrease for " + orderDetails, "Error", JOptionPane.ERROR_MESSAGE); } } //FOR ORDER PANNEL private void updateOrderPanel() { orderPanel.removeAll(); totalAmount = 0.0; for (Map.Entry<String, Integer> entry : orderMap.entrySet()) { String orderDetails = entry.getKey(); int quantity = entry.getValue(); String itemName = orderDetails.split(" with ")[0]; double basePrice = priceMap.get(itemName); double addOnPrice = 0.0; if (orderDetails.contains(" with ")) { String addOn = orderDetails.split(" with ")[1]; switch (addOn.trim()) { case "Extra shot": addOnPrice = ESPRESSO_SHOT_PRICE; break; case "Caramel drizzle": addOnPrice = CARAMEL_DRIZZLE_PRICE; break; case "No add-ons": addOnPrice = 0.0; break; } } double totalPrice = (basePrice + addOnPrice) * quantity; totalAmount += totalPrice; JPanel itemPanel = new JPanel(); itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.Y_AXIS)); JLabel itemLabel = new JLabel(orderDetails + " x " + quantity + " - ₱" + String.format("%.2f", totalPrice)); itemLabel.setFont(new Font("Arial", Font.BOLD, 20)); itemPanel.add(itemLabel); itemPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); orderPanel.add(itemPanel); } orderPanel.revalidate(); orderPanel.repaint(); updateTotal(); } //FOR THE TOTAL private void updateTotal() { totalLabel.setText("Total: ₱" + String.format("%.2f", totalAmount)); totalLabel.setFont(new Font("Arial", Font.BOLD, 20)); } //FOR CHECKOUT BUTTON private void checkout() { if (orderMap.isEmpty()) { JOptionPane.showMessageDialog(this, "No items in the order list!", "Checkout Error", JOptionPane.ERROR_MESSAGE); return; } StringBuilder receipt = new StringBuilder(); receipt.append("Hi ").append(userName).append(",\n\n"); receipt.append("Receipt:\n\n"); double checkoutTotalAmount = 0.0; for (Map.Entry<String, Integer> entry : orderMap.entrySet()) { String itemName = entry.getKey(); Integer quantity = entry.getValue(); Double itemPrice = priceMap.get(itemName.split(" with ")[0]); if (itemPrice != null) { double addOnPrice = 0.0; if (itemName.contains(" with ")) { String addOn = itemName.split(" with ")[1]; switch (addOn.trim()) { case "Extra shot": addOnPrice = ESPRESSO_SHOT_PRICE; break; case "Caramel drizzle": addOnPrice = CARAMEL_DRIZZLE_PRICE; break; case "No add-ons": addOnPrice = 0.0; break; } } double totalPrice = (itemPrice + addOnPrice) * quantity; checkoutTotalAmount += totalPrice; receipt.append(itemName).append(": ₱").append(String.format("%.2f", totalPrice)) .append(" (x").append(quantity).append(")").append("\n"); } else { JOptionPane.showMessageDialog(this, "Price not found for item: " + itemName, "Checkout Error", JOptionPane.ERROR_MESSAGE); } } receipt.append("\nTotal Amount: ₱").append(String.format("%.2f", checkoutTotalAmount)); String paymentInput = JOptionPane.showInputDialog(this, "Enter the amount you are paying: ₱"); if (paymentInput != null) { try { double paymentAmount = Double.parseDouble(paymentInput); if (paymentAmount < checkoutTotalAmount) { JOptionPane.showMessageDialog(this, "Insufficient amount! Please provide at least ₱" + String.format("%.2f", checkoutTotalAmount), "Payment Error", JOptionPane.ERROR_MESSAGE); return; } double changeAmount = paymentAmount - checkoutTotalAmount; receipt.append("\nChange: ₱").append(String.format("%.2f", changeAmount)); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Invalid input! Please enter a valid amount.", "Input Error", JOptionPane.ERROR_MESSAGE); return; } } receipt.append("\n\nThank you for your purchase!"); JOptionPane.showMessageDialog(this, receipt.toString(), "Receipt", JOptionPane.INFORMATION_MESSAGE); orderMap.clear(); totalAmount = 0.0; updateOrderPanel(); updateTotal(); } public static void main(String[] args) { SwingUtilities.invokeLater(casestudy::new); } }
Editor is loading...
Leave a Comment