DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Java Learning Series - Product Management System

Today, I created Product Management System in Java.

Product.java

public class Product{

    private int id;
    private String name;
    private String category;
    private int price;

    public Product(int i, String n, String c, int p) {
        this.id = i;
        this.name = n;
        this.category = c;
        this.price = p;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPrice() {
        return this.price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return this.getId() + " , " + this.getName() + " , " + this.getCategory() + " , " + this.getPrice();
    }
}
Enter fullscreen mode Exit fullscreen mode

ProductService.java

import java.util.ArrayList;

public class ProductService{

    ArrayList<Product> arr = new ArrayList();

    public void addProduct(Product product) {
        boolean addStatus = arr.add(product);
        if (addStatus) {
            System.out.println("Product is Added");
        } else {
            System.out.println("Product is not Added");
        }
    }

    public void viewProduct() {
        for (Product product : arr) {
            System.out.println(product);
        }
    }

    public void searchProduct(int id) {
        System.out.println();
        boolean found = false;

        for (Product product : arr) {
            if (product.getId() == id) {
                System.out.println(product.getId() + " - " + product.getName() + " - " + product.getCategory() + " - " + product.getPrice());
                found = true;
                break;
            }
        }

        if (found == true) {
            System.out.println("Product is Found");
        } else {
            System.out.println("Product is not Found");
        }
    }

    public void updateProduct(Product newProduct) {
        for (Product existingProduct : arr) {
            if (existingProduct.getId() == newProduct.getId()) {
                existingProduct.setName(newProduct.getName());
                existingProduct.setCategory(newProduct.getCategory());
                existingProduct.setPrice(newProduct.getPrice());
            }
        }
    }

    public void deleteProduct(int id) {
        boolean deleteStatus = false;

        for (Product product : arr) {
            if (product.getId() == id) {
                arr.remove(product);
                deleteStatus = true;
                break;
            }
        }

        if (deleteStatus == true) {
            System.out.println("Product is Deleted");
        } else {
            System.out.println("Product is Not Found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Main.java

public class Main{

    public static void main(String[] args) {

        Product p1 = new Product(101, "Laptop", "Electronics", 50000);
        Product p2 = new Product(102, "Mobile", "Electronics", 30000);
        Product p3 = new Product(102, "Chair", "Furniture", 5000);

        ProductService service = new ProductService();

        service.addProduct(p1);
        service.addProduct(p2);

        service.viewProduct();

        service.searchProduct(102);

        service.updateProduct(p3);

        service.viewProduct();

        service.deleteProduct(101);

        service.viewProduct();

    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Product is Added
Product is Added
101 , Laptop , Electronics , 50000
102 , Mobile , Electronics , 30000

102 - Mobile - Electronics - 30000
Product is Found
101 , Laptop , Electronics , 50000
102 , Chair , Furniture , 5000
Product is Deleted
102 , Chair , Furniture , 5000

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
marcusykim profile image
Marcus Kim

Reusing ID 102 to turn "Mobile" into "Chair" makes the update-by-identity behavior very easy to follow, while deleting ID 101 completes the CRUD loop cleanly. The next useful step would be enforcing unique IDs and returning a result from search, update, and delete instead of printing inside ProductService, which keeps business logic separate from the console UI. For a real catalog, I'd also store price as BigDecimal or integer cents so money handling stays explicit.

Collapse
 
dev_saravanan_journey profile image
Saravanan Lakshmanan

Thank you so much for taking the time to comment and for your detailed feedback! I really appreciate your suggestions. I’m still a beginner and learning Java, so this is a beginner-friendly Product Management System that I built to practice CRUD concepts. I’ll definitely work on the improvements you mentioned, especially unique IDs and separating the business logic from the console UI. I'll also keep in mind about BigDecimal and integer cents. Thanks again for the valuable suggestions! 😇