Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.4 kB
3
Indexable
Never
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Elewsmart extends JFrame {
    protected JButton addButton, remButton, clearButton, payButton;
    protected DefaultListModel<String> item;
    protected JList<String> itemList;
    protected DefaultListModel<String> cartItem;
    protected JList<String> cartList;

    public Elewsmart() {
        super("Elewsmart App");

        // buttons
        addButton = new JButton("Add to Cart");
        remButton = new JButton("Remove");
        clearButton = new JButton("Clear");
        payButton = new JButton("Pay");

        // main
        JPanel jp = new JPanel(new BorderLayout());
        add(jp);

        // itemList
        item = new DefaultListModel<>();
        item.addElement("Item 1 (RM5.00)");
        item.addElement("Item 2 (RM10.00)");
        item.addElement("Item 3 (RM7.50)");
        item.addElement("Item 4 (RM3.25)");
        itemList = new JList<>(item);
        itemList.setBorder(BorderFactory.createLineBorder(Color.black));

        // cartLabel
        JLabel cartLabel = new JLabel("Shopping Cart");

        // cartList
        cartItem = new DefaultListModel<>();
        cartList = new JList<>(cartItem);
        cartList.setBorder(BorderFactory.createLineBorder(Color.black));

        // totalLabel
        JLabel totalLabel = new JLabel("Total: RM0.00");

        // centerPanel
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(cartLabel, BorderLayout.NORTH);
        centerPanel.add(totalLabel, BorderLayout.SOUTH);
        centerPanel.add(cartList, BorderLayout.CENTER);

        // buttonPanel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(0,1));

        buttonPanel.add(addButton);
        buttonPanel.add(remButton);
        buttonPanel.add(clearButton);
        buttonPanel.add(payButton);

        jp.add(buttonPanel, BorderLayout.EAST);
        jp.add(itemList, BorderLayout.WEST);
        jp.add(centerPanel, BorderLayout.CENTER);

        setSize(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        //button
        addButton.addActionListener(new AddButtonListener());
        remButton.addActionListener(new RemoveButtonListener());
        clearButton.addActionListener(new ClearButtonListener());
        payButton.addActionListener(new PayButtonListener());
    }
    //function 
    private class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = itemList.getSelectedIndex();
            if (selectedIndex != -1) {
                String selectedItem = item.getElementAt(selectedIndex);
                cartItem.addElement(selectedItem);
            }
        }
    }

    private class RemoveButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = cartList.getSelectedIndex();
            if (selectedIndex != -1) {
                cartItem.remove(selectedIndex);
            }
        }
    }

    private class ClearButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            cartItem.clear();
        }
    }

    private class PayButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        double totalAmount = calculateTotal(); // Calculate total amount
        String paymentMessage = String.format("Payment processed successfully! \n Total Amount: RM%.2f", totalAmount); 
        JOptionPane.showMessageDialog(Elewsmart.this, paymentMessage, "Payment Confirmation", JOptionPane.INFORMATION_MESSAGE);
        clearCart(); // Clear cart after successful payment
    }

    private double calculateTotal() {
        double total = 0.0;
        for (int i = 0; i < cartItem.size(); i++) {
            String item = cartItem.getElementAt(i);
            double price = Double.parseDouble(item.substring(item.lastIndexOf("RM") + 2, item.length() - 1));
            //extracts substring from item(start= RM, end = last char) -> convert to double(price)
            total += price;
        }
        return total;
    }
  //clr  
    private void clearCart() {
        cartItem.clear();
    }
}

    public static void main(String a[]) {
        new Elewsmart();
    }
}
Leave a Comment