Bookstore system mini project in Java – Console based

In this tutorial, we are going to learn how to create a mini project on an Online Bookstore System that is console-based. A console-based online bookstore system allows users to buy books.
The users have the following available options:

  1. Browse Books: Displays the available books.
  2. Add to Cart: Displays the available books and prompts the user to enter the book number to add to the cart.
  3. View Cart: Displays the contents of the cart.
  4. Checkout: Calculates the total price of the books in the cart, displays a thank you message with the total amount, and clears the cart.
  5. Exit: Exits the program. If the user enters an invalid choice, an error message is displayed.

Java Code: console based Bookstore system

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Book {
    private String title;
    private String author;
    private double price;

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

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Title: " + title + "\nAuthor: " + author + "\nPrice: $" + price + "\n";
    }
}

class ShoppingCart {
    private List<Book> books;

    public ShoppingCart() {
        this.books = new ArrayList<>();
    }

    public void addToCart(Book book) {
        books.add(book);
        System.out.println("Book added to the cart: " + book.getTitle());
    }

    public void viewCart() {
        if (books.isEmpty()) {
            System.out.println("Your cart is empty.");
        } else {
            System.out.println("Your Shopping Cart:");
            for (Book book : books) {
                System.out.println(book);
            }
            System.out.println("Total Price: $" + calculateTotalPrice());
        }
    }

    public double calculateTotalPrice() {
        double total = 0;
        for (Book book : books) {
            total += book.getPrice();
        }
        return total;
    }

    public void clearCart() {
        books.clear();
        System.out.println("Your cart is cleared.");
    }
}

class Bookstore {
    private List<Book> books;
    private ShoppingCart shoppingCart;

    public Bookstore() {
        this.books = new ArrayList<>();
        this.shoppingCart = new ShoppingCart();
        initializeBooks();
    }

    private void initializeBooks() {
        // Add some sample books
        books.add(new Book("The Guide", "R.K Narayan", 10.99));
        books.add(new Book("A Suitable Boy", "Vikram Seth", 12.50));
        books.add(new Book("1984", "George Orwell", 9.99));
        books.add(new Book("The Catcher in the Rye", "J.D. Salinger", 11.25));
    }

    public void displayBooks() {
        System.out.println("\nAvailable Books:");
        for (int i = 0; i < books.size(); i++) {
            System.out.println("\n"+(i + 1) + ". " + books.get(i));
        }
    }

    public void addToCart(int bookIndex) {
        if (bookIndex >= 1 && bookIndex <= books.size()) {
            Book selectedBook = books.get(bookIndex - 1);
            shoppingCart.addToCart(selectedBook);
        } else {
            System.out.println("Invalid book selection.");
        }
    }

    public void viewCart() {
        shoppingCart.viewCart();
    }

    public void checkout() {
        if (shoppingCart.calculateTotalPrice()!=0.0) {
            System.out.println("Thank you for shopping with us!");
            System.out.println("Total Amount: $" + shoppingCart.calculateTotalPrice());
            shoppingCart.clearCart();
        } else {
            System.out.println("Your cart is empty. Add books before checking out.");
        }
    }
}

public class OnlineBookstoreSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Bookstore bookstore = new Bookstore();

        while (true) {
            System.out.println("\nOnline Bookstore System");
            System.out.println("\n1. Browse Books");
            System.out.println("2. Add to Cart");
            System.out.println("3. View Cart");
            System.out.println("4. Checkout");
            System.out.println("5. Exit");
            System.out.println("Select an option: ");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    bookstore.displayBooks();
                    break;
                case 2:
                    bookstore.displayBooks();
                    System.out.println("Enter the book number to add to cart: ");
                    int bookIndex = scanner.nextInt();
                    bookstore.addToCart(bookIndex);
                    break;
                case 3:
                    bookstore.viewCart();
                    break;
                case 4:
                    bookstore.checkout();
                    break;
                case 5:
                    System.out.println("Exiting Online Bookstore. Goodbye!");
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("Invalid choice. Please choose a valid option.");
            }
        }
    }
}

Explanation

In the above code we have shown the implementation of an online bookstore system.
It allows the users to browse books, add them to a shopping cart, view the cart and checkout(Buy).
The code defines three classes: Book, ShoppingCart and Bookstore.
The Book class represents a book and has private variables for the title, author, and price.

  • It has a constructor to initialize these variables, as well as getter methods to retrieve the title, author, and price.
  • It also overrides the toString() method to provide a string representation of the book.

The ShoppingCart class represents a shopping cart and has a private list of  Book objects.

  • It has a constructor to initialize the list, as well as methods to add a book to the cart, view the cart, calculate the total price of the books in the cart, and clear the cart.

The Bookstore class represents the online bookstore system.

  • It has private variables for a list of Book objects and a ShoppingCart object.
  • It has a constructor to initialize these variables and a method to add some sample books to the list.
  • It also has methods to display the available books, add a book to the cart based on the user’s selection, view the cart, and checkout.

The OnlineBookstoreSystem class is the main class that contains the main method. It creates a Scanner object to read user input and a Bookstore object. It then enters a loop where it displays a menu of options for the user to choose from. Based on the user’s choice, it calls the corresponding method in the Bookstore object.

Output

Online Bookstore System

1. Browse Books
2. Add to Cart
3. View Cart
4. Checkout
5. Exit
Select an option: 
1

Available Books:

1. Title: The Guide
Author: R.K Narayan
Price: $10.99


2. Title: A Suitable Boy
Author: Vikram Seth
Price: $12.5


3. Title: 1984
Author: George Orwell
Price: $9.99


4. Title: The Catcher in the Rye
Author: J.D. Salinger
Price: $11.25


Online Bookstore System

1. Browse Books
2. Add to Cart
3. View Cart
4. Checkout
5. Exit
Select an option:
2

Available Books:

1. Title: The Guide
Author: R.K Narayan
Price: $10.99


2. Title: A Suitable Boy
Author: Vikram Seth
Price: $12.5


3. Title: 1984
Author: George Orwell
Price: $9.99


4. Title: The Catcher in the Rye
Author: J.D. Salinger
Price: $11.25

Enter the book number to add to cart:
3
Book added to the cart: 1984

Online Bookstore System

1. Browse Books
2. Add to Cart
3. View Cart
4. Checkout
5. Exit
Select an option:
3
Your Shopping Cart:
Title: 1984
Author: George Orwell
Price: $9.99

Total Price: $9.99

Online Bookstore System

1. Browse Books
2. Add to Cart
3. View Cart
4. Checkout
5. Exit
Select an option:
4
Thank you for shopping with us!
Total Amount: $9.99
Your cart is cleared.

Online Bookstore System

1. Browse Books
2. Add to Cart
3. View Cart
4. Checkout
5. Exit
Select an option:
5
Exiting Online Bookstore. Goodbye!

Thank you for learning from CodeSpeedy. If you have any doubts or questions then feel free to ask them in the comments section below.
You can also read:

Leave a Reply

Your email address will not be published. Required fields are marked *