Online Book Store in Java
Last Updated : 10 Jun, 2025
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
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:
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.
Similar Reads
Local IDE vs Online IDE in Java An integrated development environment (IDE) is software that combines ordinary developer tools into a single graphical user interface for developing applications (GUI). An IDE usually consists of the following components: 1. Source code editor: It is a text editor that can help you write software co
5 min read
JRE in Java Java Runtime Environment (JRE) is an open-access software distribution that has a Java class library, specific tools, and a separate JVM. In Java, JRE is one of the interrelated components in the Java Development Kit (JDK). It is the most common environment available on devices for running Java prog
4 min read
How to Start Learning Java? Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
8 min read
Top 5 Open Source Java Frameworks Almost 30 years and still Ruling the software industry, over the years Java has ranked among the top three most popular programming languages in the world with numerous applications, including back-end development projects, big data and machine learning projects, and obviously, web and Android devel
7 min read
Java Networking When computing devices such as laptops, desktops, servers, smartphones, and tablets and an eternally-expanding arrangement of IoT gadgets such as cameras, door locks, doorbells, refrigerators, audio/visual systems, thermostats, and various sensors are sharing information and data with each other is
15+ min read