Introduction
In this tutorial, we will be creating a Book Inventory System using Java. This system allows bookstore owners to manage their inventory by keeping track of books available for sale, as well as performing basic operations such as adding, updating, deleting, and viewing books in the inventory. The goal is to create a simple, functional, and user-friendly inventory system to help streamline bookstore management.
Objective
The objective of this Book Inventory System is to help users manage the stock of books in a bookstore efficiently. This program provides basic functionalities such as adding a new book, updating book details, deleting books from the inventory, and displaying the list of available books. It will teach you how to handle collections, user input, and basic file handling for persistent data storage.
Java Code for Book Inventory System
import java.util.ArrayList; import java.util.Scanner; class Book { String title; String author; String isbn; double price; public Book(String title, String author, String isbn, double price) { this.title = title; this.author = author; this.isbn = isbn; this.price = price; } public String toString() { return "Title: " + title + ", Author: " + author + ", ISBN: " + isbn + ", Price: $" + price; } } public class BookInventorySystem { private ArrayList inventory; private Scanner scanner; public BookInventorySystem() { inventory = new ArrayList<>(); scanner = new Scanner(System.in); } public void displayMenu() { System.out.println("Book Inventory System Menu:"); System.out.println("1. Add a new book"); System.out.println("2. Update book details"); System.out.println("3. Delete a book"); System.out.println("4. View inventory"); System.out.println("5. Exit"); } public void addBook() { System.out.print("Enter book title: "); String title = scanner.nextLine(); System.out.print("Enter book author: "); String author = scanner.nextLine(); System.out.print("Enter book ISBN: "); String isbn = scanner.nextLine(); System.out.print("Enter book price: "); double price = scanner.nextDouble(); scanner.nextLine(); // Consume the newline Book book = new Book(title, author, isbn, price); inventory.add(book); System.out.println("Book added successfully!"); } public void updateBook() { System.out.print("Enter ISBN of the book to update: "); String isbn = scanner.nextLine(); for (Book book : inventory) { if (book.isbn.equals(isbn)) { System.out.print("Enter new title (or leave blank to keep current): "); String title = scanner.nextLine(); if (!title.isEmpty()) book.title = title; System.out.print("Enter new author (or leave blank to keep current): "); String author = scanner.nextLine(); if (!author.isEmpty()) book.author = author; System.out.print("Enter new price (or leave blank to keep current): "); String priceInput = scanner.nextLine(); if (!priceInput.isEmpty()) book.price = Double.parseDouble(priceInput); System.out.println("Book updated successfully!"); return; } } System.out.println("Book not found."); } public void deleteBook() { System.out.print("Enter ISBN of the book to delete: "); String isbn = scanner.nextLine(); for (Book book : inventory) { if (book.isbn.equals(isbn)) { inventory.remove(book); System.out.println("Book deleted successfully!"); return; } } System.out.println("Book not found."); } public void viewInventory() { if (inventory.isEmpty()) { System.out.println("No books in inventory."); } else { System.out.println("Inventory List:"); for (Book book : inventory) { System.out.println(book); } } } public void start() { while (true) { displayMenu(); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume the newline switch (choice) { case 1: addBook(); break; case 2: updateBook(); break; case 3: deleteBook(); break; case 4: viewInventory(); break; case 5: System.out.println("Exiting..."); return; default: System.out.println("Invalid choice. Please try again."); } } } public static void main(String[] args) { BookInventorySystem system = new BookInventorySystem(); system.start(); } }
Program Explanation
This Java program manages a simple book inventory for a bookstore. Here’s a breakdown of the components:
- Book Class: Represents a book with attributes like title, author, ISBN, and price. It also includes a
toString()
method for displaying book details. - BookInventorySystem Class: This class manages the inventory of books. It provides functionality for adding, updating, deleting, and viewing books in the inventory. It uses an
ArrayList
to store the books. - Menu System: The program displays a simple menu with options. Based on user input, it calls the appropriate method to handle the inventory operations.
- Scanner for Input: The program uses the
Scanner
class to get user input from the console.
How to Run the Program
- Ensure that you have Java installed on your system.
- Create a new Java file named
BookInventorySystem.java
. - Copy the code above into your file.
- Open a terminal or command prompt, navigate to the folder where your Java file is saved.
- Compile the program by running
javac BookInventorySystem.java
. - Run the program by typing
java BookInventorySystem
. - Follow the on-screen instructions to manage the book inventory.