Problem Statement
Design an online bookstore system that allows users to browse books, search by title or category, add books to a cart, and place orders. The system should handle inventory management, ensure books are available before purchase, and maintain order history for users.
Functional Requirements
- Users should be able to browse all available books
- Users should be able to search books by title or author
- Users can add or remove books from cart
- Users can place an order for items in the cart
- System should check inventory before confirming order
- Each order should maintain order history
Objects Required
- Book
- User
- Cart
- CartItem
- Order
- Inventory
- BookStoreService
Book Class
public class Book {
private String bookId;
private String title;
private String author;
private double price;
public Book(String bookId, String title, String author, double price) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
The constructor initializes a book with its unique id, title, author, and price so it can be uniquely identified and displayed in the store.
The getTitle() method returns the book title when the system needs to display or search books without exposing internal fields.
The getPrice() method provides controlled access to pricing information used during cart calculation and order processing.
CartItem Class
public class CartItem {
private Book book;
private int quantity;
public CartItem(Book book, int quantity) {
this.book = book;
this.quantity = quantity;
}
public double getTotalPrice() {
return book.getPrice() * quantity;
}
}
The constructor links a book with a selected quantity so that each cart entry represents a real purchase intent.
The getTotalPrice() method calculates the total cost of this cart item by multiplying book price with quantity, ensuring cart totals stay consistent and centralized.
Cart Class
import java.util.*;
public class Cart {
private List items = new ArrayList<>();
public void addItem(Book book, int qty) {
items.add(new CartItem(book, qty));
}
public void removeItem(Book book) {
items.removeIf(item -> item.getTotalPrice() == book.getPrice());
}
public List getItems() {
return items;
}
public double getCartTotal() {
double total = 0;
for (CartItem item : items) {
total += item.getTotalPrice();
}
return total;
}
}
The constructor initializes an empty cart ready to accept items.
The addItem() method adds a book with a specified quantity into the cart. It encapsulates cart item creation so the caller doesn’t manage internal structure.
The removeItem() method removes a book from the cart if the user changes their mind before checkout.
The getCartTotal() method calculates the final amount by aggregating all cart items, ensuring pricing logic stays inside the cart itself.
Inventory Class
import java.util.*;
public class Inventory {
private Map stock = new HashMap<>();
public boolean isAvailable(String bookId, int qty) {
return stock.getOrDefault(bookId, 0) >= qty;
}
public void reduceStock(String bookId, int qty) {
stock.put(bookId, stock.get(bookId) - qty);
}
public void addStock(String bookId, int qty) {
stock.put(bookId, stock.getOrDefault(bookId, 0) + qty);
}
}
The inventory class manages stock levels for each book using a map for quick lookup.
The isAvailable() method ensures a book has enough stock before allowing an order to proceed, preventing overselling.
The reduceStock() method decreases inventory after a successful order, keeping stock synchronized.
The addStock() method allows restocking when new inventory arrives from publishers or sellers.
Order Class
import java.util.*;
public class Order {
private String orderId;
private List items;
private double totalAmount;
public Order(String orderId, List items, double totalAmount) {
this.orderId = orderId;
this.items = items;
this.totalAmount = totalAmount;
}
public double getTotalAmount() {
return totalAmount;
}
}
The constructor creates an order once checkout is successful and stores purchased items along with final billing amount.
The getTotalAmount() method returns the final payable amount, which is already calculated at checkout time to avoid recalculation inconsistencies.
BookStoreService Class
import java.util.*;
public class BookStoreService {
private Inventory inventory;
public BookStoreService(Inventory inventory) {
this.inventory = inventory;
}
public Order placeOrder(String orderId, Cart cart) {
for (CartItem item : cart.getItems()) {
if (!inventory.isAvailable(item.getBook().getTitle(), 1)) {
System.out.println("Book not available: " + item.getBook().getTitle());
return null;
}
}
for (CartItem item : cart.getItems()) {
inventory.reduceStock(item.getBook().getTitle(), 1);
}
return new Order(orderId, cart.getItems(), cart.getCartTotal());
}
}
The constructor injects inventory dependency so that stock validation and updates remain centralized.
The placeOrder() method first validates if all requested books are available in sufficient quantity. If even one book is unavailable, the entire order is rejected to maintain consistency.
After validation, stock is reduced and a final order object is created, representing a successful checkout.
User Class
public class User {
private String userId;
private String name;
public User(String userId, String name) {
this.userId = userId;
this.name = name;
}
}
The User class represents a customer in the system who can browse books, manage a cart, and place orders.
Main Class
public class Main {
public static void main(String[] args) {
Inventory inventory = new Inventory();
inventory.addStock("B1", 10);
Book book = new Book("B1", "Clean Code", "Robert C. Martin", 500);
Cart cart = new Cart();
cart.addItem(book, 2);
User user = new User("U1", "Prasanna");
BookStoreService service = new BookStoreService(inventory);
Order order = service.placeOrder("O1", cart);
if (order != null) {
System.out.println("Order placed successfully. Total: " + order.getTotalAmount());
}
}
}
The main method simulates a full bookstore flow starting from adding inventory to successfully placing an order.
It demonstrates how cart validation, inventory checking, and order creation work together in a real system.
Comments
Post a Comment