e-commerce management system in java

Introduction
The e-commerce management system is a GUI-based desktop application designed using Java swing in Netbean IDE. It simulates a basic online shopping experience, allowing users to: Browse a list of products Add items to a shopping cart See the total car Czech out and simulate the order confirmation This system is designed for educational purposes and shows how product listing, cart management and total calculations such as core e-commerce functionality can be implemented using events and swing GUI components of e-commerce functionality Java.
steps to create ecommerce management system
1. Create new Java project Open netbeans IDE File> Go to the new project Choose:
Java> Java application Project Name: Ecosystem Click on
2. Add the main class Right-click on source packages> New> Java class Name it:
eCommercessystem Paste the full code provided (colorful gui with shopping cart) This includes product class, cart class and GUI inside JFRAME
3. Design gui You do not need to use a drag-end-drop GUI builder. Code is already included:
Label (jlabel) Lists for products and carts (jlist) “Add into cart” and button for “checkout” (jbutton) Set manually with setbounds for easy control But if you like to use Netbeans GUI designer (Matise):
Right-click Source Package> New> JFRAME Form Design GUI using pellet tools (drag button, lists, labels) Copy the logic from the code to the button action
4. Add product list Inside the constitution:
Productmodel.addelement (new product (1, “laptop”, 74999)); It creates a predetermined product list.
5. Add shopping cart functionality When clicked on “Add Cart”:
The selected product is added to the cartmodel The total value is updated using Cart.gettotal ()
6. Add checkout functionality When “checkout” is clicked:
Display a joptionpane showing confirmation of order Clean the cart and update the total label
7. Run project Right-click on ECommercessystem:
java → Set as the main category Right-click again → run file
code explanation
1. Class Declaration & Setup
public class ECommerceSystem extends JFrame {
ECommerceSystem
extendsJFrame
, making it a window-based GUI application.Product class
Class product { IN ID; String name; Dual price;
Represence a product in the store. Each product has an ID, name and value.
Tostring () method returns the product to a readable format.
Cart class
Class cart { Arraylist <Product> item = new arraylist <> ();
Keeps a list of selected products. Addproduct (product p) – adds a product to the cart. Gettotal () – Calves the total value.
Clearcart () – Empty the car after a checkout.
4. Gui component declaration Defaultlistmodel <product> productmodel = new defaultlistmodel <> (); Defaultlistmodel <product> cartmodel = new defaultlistmodel <> (); Productmodel: Storage all the products displayed in the product list. Cartmodel: Store the products selected in the store. Other GUI components include: Jlist <Product> - To display products and carts. Jlabel, JButton, JSCRLLPANE - for layout and interaction. 🔹 5. GUI layout and design Java Copy edit Setlayout (null); Getcontentpane (). Setbackground (new color (240, 248, 255)); Uses full position (setbound) for complete control. The visual appeal has backgrounds and font color sets. There are label styles like "Welcome", "Products" and "Cart". 🔹 6. Sample product added Java Copy edit Productmodel.addelement (new product (1, "laptop", 74999)); These products are manually added to the product list. 🔹 7. Add to the cart button Java Copy edit btnadddtocart.addakusionlistener (e -> {{{ Product selected = listproducts.getselectedvalue (); , , When clicked: The selected product is added to the cart. Cart models and total labels are updated. 🔹 8. Checkout button Java Copy edit btncheckout.Addactionlistener (e -> {{{ If (Cart.getitems (). isempty ()) { , }
source code
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class ECommerceSystem extends JFrame {
// Product class
class Product {
int id;
String name;
double price;
Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String toString() {
return name + " - ₹" + price;
}
}
// Cart class
class Cart {
ArrayList items = new ArrayList();
void addProduct(Product p) {
items.add(p);
}
ArrayList getItems() {
return items;
}
double getTotal() {
double total = 0;
for (Product p : items) {
total += p.price;
}
return total;
}
void clearCart() {
items.clear();
}
}
// GUI components
DefaultListModel productModel = new DefaultListModel();
DefaultListModel cartModel = new DefaultListModel();
Cart cart = new Cart();
public ECommerceSystem() {
setTitle("🛍️ E-Commerce Management System");
setSize(800, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
getContentPane().setBackground(new Color(240, 248, 255));
JLabel lblTitle = new JLabel("Welcome to My Online Store 🛒");
lblTitle.setFont(new Font("Arial", Font.BOLD, 22));
lblTitle.setForeground(new Color(0, 102, 204));
lblTitle.setBounds(230, 10, 400, 30);
add(lblTitle);
JLabel lblProducts = new JLabel("🛍️ Products");
lblProducts.setFont(new Font("Verdana", Font.BOLD, 16));
lblProducts.setBounds(50, 50, 200, 25);
add(lblProducts);
JList listProducts = new JList(productModel);
listProducts.setFont(new Font("Segoe UI", Font.PLAIN, 14));
listProducts.setBackground(new Color(255, 255, 204));
JScrollPane scroll1 = new JScrollPane(listProducts);
scroll1.setBounds(50, 80, 250, 250);
scroll1.setBorder(new LineBorder(Color.GRAY));
add(scroll1);
JButton btnAddToCart = new JButton("➕ Add to Cart");
btnAddToCart.setBackground(new Color(51, 153, 255));
btnAddToCart.setForeground(Color.WHITE);
btnAddToCart.setFont(new Font("Arial", Font.BOLD, 14));
btnAddToCart.setBounds(310, 160, 150, 35);
add(btnAddToCart);
JLabel lblCart = new JLabel("🛒 Shopping Cart");
lblCart.setFont(new Font("Verdana", Font.BOLD, 16));
lblCart.setBounds(500, 50, 200, 25);
add(lblCart);
JList listCart = new JList(cartModel);
listCart.setFont(new Font("Segoe UI", Font.PLAIN, 14));
listCart.setBackground(new Color(204, 255, 204));
JScrollPane scroll2 = new JScrollPane(listCart);
scroll2.setBounds(500, 80, 250, 250);
scroll2.setBorder(new LineBorder(Color.GRAY));
add(scroll2);
JLabel lblTotal = new JLabel("Total: ₹0.00");
lblTotal.setFont(new Font("Tahoma", Font.BOLD, 16));
lblTotal.setForeground(Color.BLACK);
lblTotal.setBounds(500, 340, 200, 30);
add(lblTotal);
JButton btnCheckout = new JButton("💳 Checkout");
btnCheckout.setBackground(new Color(0, 204, 102));
btnCheckout.setForeground(Color.WHITE);
btnCheckout.setFont(new Font("Arial", Font.BOLD, 14));
btnCheckout.setBounds(640, 340, 110, 35);
add(btnCheckout);
// Sample products
productModel.addElement(new Product(1, "Laptop", 74999));
productModel.addElement(new Product(2, "Smartphone", 25999));
productModel.addElement(new Product(3, "Headphones", 1999));
productModel.addElement(new Product(4, "Smartwatch", 5499));
productModel.addElement(new Product(5, "Tablet", 15999));
// Button Actions
btnAddToCart.addActionListener(e -> {
Product selected = listProducts.getSelectedValue();
if (selected != null) {
cart.addProduct(selected);
cartModel.addElement(selected);
lblTotal.setText("Total: ₹" + cart.getTotal());
}
});
btnCheckout.addActionListener(e -> {
if (cart.getItems().isEmpty()) {
JOptionPane.showMessageDialog(this, "Your cart is empty!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this,
"🎉 Thank you for your purchase!\nTotal Amount: ₹" + cart.getTotal(),
"Order Placed", JOptionPane.INFORMATION_MESSAGE);
cart.clearCart();
cartModel.clear();
lblTotal.setText("Total: ₹0.00");
}
});
setVisible(true);
}
public static void main(String[] args) {
new ECommerceSystem();
}
}
output

