Online Book Store in Java

Last Updated : 8 Sep, 2025

An online bookstore is a platform that allows us to browse books, view details (title, author, price, publish date) and search for specific books. In this tutorial, we will build a simple Online Book Store system in Java.

This project will help you practice core Java concepts like:

  • Classes and Objects (OOP)
  • Collections Framework
  • Java 8 Streams and Optional
  • Grouping and Filtering

Features of the Online Book Store

Our Online Book Store will have the following features:

  • Store a list of books with details (title, author, price, publish date).
  • Filter books based on price.
  • Group books by their publication year.
  • Search for a book by title using Optional.
  • Display all available books.

Step-by-Step Implementation of Online Book Store Project

Step 1. Create the project

In IntelliJ

  1. File -> New -> Project -> Choose Java -> Next.
  2. Set Project SDK to Java 8 (or later).
  3. Create a project with a project name
  4. Click Finish.

Step 2: Create package

Inside src (IntelliJ) create package:

com.bookstore

Project Structure

The image below demonstrates the project structure

Project-Structure

We will organize the code into these parts which are listed below:

  • Book Class: This class represents a book's details.
  • BookStore Class: This class manage the book store inventory, handling filtering, searching and grouping of books.
  • BookStoreApp: This is the main class where all the store operations are performed.

Let's now try to implement the code.

Step 3: Add Book.java

This class is used to represent a book in the store, with attributes such as title, author, price and publish date.

Java
package com.bookstore;

import java.time.LocalDate;

public class Book {
    private String title;
    private String author;
    private double price;
    private LocalDate publishDate;

    public Book(String title, String author, double price, LocalDate publishDate) {
        this.title = title;
        this.author = author;
        this.price = price;
        this.publishDate = publishDate;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

    public LocalDate getPublishDate() {
        return publishDate;
    }

    @Override
    public String toString() {
        return "Book{Title='" + title + "', Author='" + author + "', Price=" + price + ", Publish Date=" + publishDate + "}";
    }
}

Step 4. Add BookStore.java

The BookStore class is where the main logic for filtering, grouping and searching books resides.

Java
package com.bookstore;

import java.time.LocalDate;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class BookStore {

    private List<Book> books;

    public BookStore() {
        books = new ArrayList<>();
        books.add(new Book("Java Programming", "Geek1", 39.99, LocalDate.of(2021, 1, 15)));
        books.add(new Book("Learning Java 8", "Geek2", 49.99, LocalDate.of(2020, 5, 10)));
        books.add(new Book("Advanced Java", "Geek3", 59.99, LocalDate.of(2021, 8, 20)));
        books.add(new Book("Spring Framework", "Geek4", 29.99, LocalDate.of(2019, 11, 30)));
    }

    // Filtering books based on price
    public List<Book> filterBooksByPrice(double maxPrice) {
        return books.stream()
                .filter(book -> book.getPrice() <= maxPrice)
                .collect(Collectors.toList());
    }

    // Grouping books by year
    public Map<Integer, List<Book>> groupBooksByYear() {
        return books.stream()
                .collect(Collectors.groupingBy(book -> book.getPublishDate().getYear()));
    }

    // Searching books by title using Optional
    public Optional<Book> searchBookByTitle(String title) {
        return books.stream()
                .filter(book -> book.getTitle().equalsIgnoreCase(title))
                .findFirst();
    }

    // Display all books
    public void displayBooks() {
        books.forEach(System.out::println);
    }
}

Step 5. Add BookStoreApp.java (main)

This class will be the entry point for running the application. It calls the method of the BookStore class to display and perform operations on the book list.

Java
package com.bookstore;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class BookStoreApp {
    public static void main(String[] args) {
        BookStore store = new BookStore();

        System.out.println("All Books in Store:");
        store.displayBooks();

        System.out.println("\nBooks under $40:");
        List<Book> cheapBooks = store.filterBooksByPrice(40);
        cheapBooks.forEach(System.out::println);

        System.out.println("\nBooks Grouped by Year:");
        Map<Integer, List<Book>> groupedBooks = store.groupBooksByYear();
        groupedBooks.forEach((year, books) -> {
            System.out.println(year + " -> " + books);
        });

        System.out.println("\nSearching for 'Advanced Java':");
        Optional<Book> book = store.searchBookByTitle("Advanced Java");
        book.ifPresent(System.out::println);
    }
}

Step 6. Run the app

Right-click BookStoreApp.java -> Run 'BookStoreApp.main()'. Check Run tool window for console output.

Running-the-project

Output:

file
Comment