Collection Framework - Guided Exercise - InventoryManager – Mastering Java ArrayList #110
Replies: 3 comments
-
**
** |
Beta Was this translation helpful? Give feedback.
-
|
Bhavana public class Product { } import java.util.stream.Collectors; public class InventoryManager { } } |
Beta Was this translation helpful? Give feedback.
-
package com.practice.problems.java;
import java.util.*;
public class LambdaMartInventory {
public static void main(String[] args) {
InventoryManager manager = new InventoryManager();
manager.addProduct(new Product(1, "Phone", "Electronics", 29999, true));
manager.addProduct(new Product(2, "Yoga Mat", "Fitness", 899, true));
manager.displayAllProducts();
manager.removeById(2);
manager.updatePrice(1, 27999);
manager.sortByPriceAscending();
manager.displayAllProducts();
manager.filterByPrice(500, 3000);
}
}
class Product {
private int id;
private String name;
private String category;
private double price;
private boolean inStock;
public Product(int id, String name, String category, double price, boolean inStock) {
this.id = id;
this.name = name;
this.category = category;
this.price = price;
this.inStock = inStock;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getCategory() {
return category;
}
public void setPrice(double newPrice) {
this.price = newPrice;
}
public void setInStock(boolean status) {
this.inStock = status;
}
public String toString() {
return "Product[id=" + id + ", name=" + name + ", category=" + category +
", price=" + price + ", inStock=" + inStock + "]";
}
}
class InventoryManager {
private List<Product> inventory = new ArrayList<>();
public void addProduct(Product product) {
for (Product p : inventory) {
if (p.getName().equalsIgnoreCase(product.getName())) {
System.out.println("Duplicate product name not allowed: " + product.getName());
return;
}
}
inventory.add(product);
System.out.println("Product added: " + product);
}
public void displayAllProducts() {
if (inventory.isEmpty()) {
System.out.println("Inventory is empty.");
return;
}
for (Product p : inventory) {
System.out.println(p);
}
}
public void removeById(int id) {
Iterator<Product> it = inventory.iterator();
while (it.hasNext()) {
Product p = it.next();
if (p.getId() == id) {
it.remove();
System.out.println("Removed product with ID: " + id);
return;
}
}
System.out.println("Product ID not found.");
}
public void updatePrice(int id, double newPrice) {
for (Product p : inventory) {
if (p.getId() == id) {
p.setPrice(newPrice);
System.out.println("Updated price for " + p.getName());
return;
}
}
System.out.println("Product ID not found.");
}
public void searchByCategory(String category) {
boolean found = false;
for (Product p : inventory) {
if (p.getCategory().equalsIgnoreCase(category)) {
System.out.println("Found: " + p);
found = true;
}
}
if (!found) {
System.out.println("No products found in category: " + category);
}
}
public void filterByPrice(double min, double max) {
boolean found = false;
for (Product p : inventory) {
if (p.getPrice() >= min && p.getPrice() <= max) {
System.out.println(p);
found = true;
}
}
if (!found) {
System.out.println("No products found in price range " + min + " - " + max);
}
}
public void searchByName(String nameFragment) {
boolean found = false;
for (Product p : inventory) {
if (p.getName().toLowerCase().contains(nameFragment.toLowerCase())) {
System.out.println("Match: " + p);
found = true;
}
}
if (!found) {
System.out.println("No product matches name fragment: " + nameFragment);
}
}
public void sortByPriceAscending() {
Collections.sort(inventory, new Comparator<Product>() {
public int compare(Product p1, Product p2) {
return Double.compare(p1.getPrice(), p2.getPrice());
}
});
}
public void sortByName() {
Collections.sort(inventory, new Comparator<Product>() {
public int compare(Product p1, Product p2) {
return p1.getName().toLowerCase().compareTo(p2.getName().toLowerCase());
}
});
}
public int getTotalInventoryCount() {
return inventory.size();
}
public void clearInventory() {
inventory.clear();
System.out.println("Inventory cleared.");
}
public boolean isEmpty() {
return inventory.isEmpty();
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🧪 Exercise:
InventoryManager – Mastering Java ArrayList📖 Context: Welcome to LambdaMart!
You’re now a junior backend engineer at LambdaMart, India’s fastest-growing AI-first online retail company.
Your first assignment? Build the Inventory Management Module using Java’s
ArrayListcollection.🧰 Objective
Master the following ArrayList operations:
Adding, accessing, updating, removing elements
Searching and filtering
Sorting using
ComparatorUsing
.contains(),.indexOf(),.clear(),.isEmpty(),.size()Using
Iterator,forEach, andStreamsEnsuring clean code practices and naming conventions
📦 Files to Create
Product.java– Entity classInventoryManager.java– Business logic usingArrayListMain.java– Entry point and test🧩 Part 1: Define the
ProductclassProduct.java✅ Task A: Fill in the blanks
✅ Task B: Add the following methods:
getId()getPrice()getCategory()setPrice(double newPrice)setInStock(boolean status)Add Javadoc-style comments for each method.
🧩 Part 2: Setup Inventory Manager
InventoryManager.java✅ Task C: Fix the bug
🛠 Bug:
NullPointerException✍️ Write the fix:
🧩 Part 3: Modify Products
✅ Task D: Complete these methods
🧩 Part 4: Search & Filter
✅ Task E: Search by category
✅ Task F: Filter by price range
✅ Task G: Search by name fragment
🧩 Part 5: Sorting Logic
✅ Task H: Sort products by price
✅ Task I: Sort by name alphabetically
🧩 Part 6: Utility Operations
✅ Task J: Utility Methods (complete signatures)
🧪 Part 7: Main Class for Testing
Main.java🧩 Predict the output:
✍️ Predict:
How many products remain?
What's the price of product with ID 1?
Which sort order will show first?
🏁 Deliverables
Your submission must include:
✅ Fully working
Product.java✅ Completed
InventoryManager.javawith 10+ methods✅ Working
Main.javafile demonstrating:Product addition
Updates
Removal
Sorting
Searching
Filtering
✅ Console outputs for each operation
✅ A README-style summary of “What I learned about ArrayList”
📘 Clean Code Tips Recap
🚀 Bonus Challenges
Add a method to prevent duplicate product names
Add stock quantity and auto-decrement on sale
Add
Map<Integer, Product>for fast ID lookupAdd export to CSV functionality using
FileWriterAdd
Set<Product>for unique inventory across warehousesBeta Was this translation helpful? Give feedback.
All reactions