Open In App

Online Book Store in Java

Last Updated : 10 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An online bookstore is a platform that allows us to browse through a collection of books. We can also check details like title, author, price, and make purchases. In this article, we are going to build a simple online bookstore using Java. With the help of this project, we will gain hands-on experience with core Java concepts like streams, collections, and OOP (object-oriented programming).

In this article, we are going to build a basic online book store system with the ability to store, filter, and search for books. We will create different classes to keep the code more clean and organized, similar to the approach used in real-world software development.

Working of the Online Book Store System

Here is how our online book store system is going to work:

  • A list of books will be stored with details like title, author, price, and published date.
  • Users will be able to filter books based on their price.
  • The books can be grouped by their publication year.
  • Users can search for a specific book by its title.

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.

Java Online Book Store Project Implementation

1. 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 + "}";
    }
}


2. BookStore Class

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);
    }
}


3. BookStoreApp.java

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.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);
    }
}

Output:

file


How to Run the Project in IntelliJ IDEA?

To run the project on IntelliJ IDEA we just need to follow these steps which are listed below:

  • Open IntelliJ IDEA and create a new Java project.
  • Start creating the classes and the structure must be look like this:
    • Book.java
    • BookStore.java
    • BookStoreApp.java
  • Save all the files.
  • Run the BookStoreApp class.
Running-the-project

Article Tags :
Practice Tags :

Similar Reads