bookstore management system in java

intoduction
The Bookstore Management System is a Java-based application designed to manage book inventory and streamline sales operations. This allows the owner of the store to add new books, update stock volume, remove entries and discover books by name or author. It also includes a billing system to process customer procurement, generate invoices and calculate total costs. With the user -friendly swing GUI, this system enhances the efficiency of management of a bookstore.
It is an desktop application developed to simplify and automate the day-to-day operations of a bookstore. It is designed to help bookstore owners and employees manage book inventory, process sales efficiently, and maintain accurate billing records. Built with Java Swing for the GUI, the system offers a clean, responsive interface to handle all core bookstore functions in a structured and user-friendly way.
Core Features and Functionalities:
1. Book Inventory Management:
Add New Books: Admins can add book details including title, author, genre, price, and quantity in stock.
Update Stock: Easily increase or decrease the number of available copies after purchase or restock.
Remove Books: Delete outdated or unavailable book records from the inventory.
Search Functionality: Quickly find books by title, author name, or category using smart filtering.
2. Sales and Billing System:
Customer Purchase Processing: Select books being purchased, calculate total cost based on quantity and unit price.
Invoice Generation: Automatically generate itemized bills with prices, total amount, and optional discount handling.
Transaction Recording: Maintain a record of all customer purchases for accounting and analytics.
3. Graphical User Interface (GUI):
Developed using Java Swing, the application features:
Tables to display available books.
Forms to input or edit book information.
Buttons and dropdowns for easy navigation.
Real-time data reflection after every transaction.
4. Data Storage and Security:
File Handling or Database (Optional): Book and sales data can be stored in structured files (text or CSV) or connected to a backend database (e.g., MySQL or SQLite).
Validation: Prevents invalid entries such as negative stock or missing details, ensuring data accuracy.
steps to implement bookstore management system
-
Requirement Analysis: Define features — add/update/delete books, search books, manage sales.
-
Design: Create class diagrams (Book, Inventory, Sale).
-
Setup Environment: Install JDK, use NetBeans or Eclipse.
-
Design GUI: Use Swing to build forms for book details, inventory list, sales entry.
-
Implement Backend: Classes for Book, Inventory; methods for add, update, delete.
-
Data Storage: Use file serialization or SQLite DB to store book and sales data.
-
Connect GUI & Backend: Link buttons to backend methods.
-
Test: Add sample books, perform sales, generate reports.
-
Enhance: Add search by author/title, generate invoices.
-
Document & Deploy: Comment code, create runnable JAR.
code explanation
1.Imports
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
These import essential Swing and AWT classes for GUI creation, layout, event handling, and table management.
2.Class Declaration
public class CreativeBookStore extends JFrame {
This defines the main GUI class CreativeBookStore
which inherits from JFrame
to create a top-level window.
3.Component Declarations
private JTable bookTable;
private DefaultTableModel tableModel;
private JTextField tfTitle, tfAuthor, tfPrice, tfQuantity;
private JButton btnAdd, btnUpdate, btnDelete;
Declares GUI components: a table to display book data, text fields for input, and buttons for operations.
4.Constructor
public CreativeBookStore() {
Constructor initializes the GUI window and all components.
5.Window Properties
setTitle("Creative Book Store Management");
setSize(750, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
Sets the window title, size, close operation, center positioning, and layout manager.
6.Left Panel with Decorations
JPanel leftPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
...
}
};
Creates a custom panel with overridden paintComponent()
to draw decorative shapes like circles and lines.
leftPanel.setPreferredSize(new Dimension(180, 0));
leftPanel.setBackground(new Color(245, 245, 250));
add(leftPanel, BorderLayout.WEST);
Sets size and background of the left panel and adds it to the west of the frame.
7.createInputField Method
Creates a labeled text field and adds it to the panel.
private JTextField createInputField(String label, JPanel parent) {
...
}
8.createButton Method
Creates a styled button with hover effects.
private JButton createButton(String text, Color color) {
...
}
9.addBook Method
Validates input and adds a new row to the table.
private void addBook() {
...
}
10.updateBook Method
Updates the selected table row with new values.
private void updateBook() {
...
}
11.deleteBook Method
Deletes the selected row after confirmation.
private void deleteBook() {
...
}
12.loadBookToFields Method
Loads the selected table row into the text fields.
private void loadBookToFields() {
...
}
13.validateInput Method
Checks that all fields are filled and price/quantity are valid numbers.
private boolean validateInput() {
...
}
14.clearInputs Method
Clears all text fields and deselects the table row.
private void clearInputs() {
...
}
15.Main Method
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CreativeBookStore().setVisible(true);
});
}
Launches the application in the Swing event-dispatch thread.
source code
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
public class CreativeBookStore extends JFrame {
private JTable bookTable;
private DefaultTableModel tableModel;
private JTextField tfTitle, tfAuthor, tfPrice, tfQuantity;
private JButton btnAdd, btnUpdate, btnDelete;
public CreativeBookStore() {
setTitle("Creative Book Store Management");
setSize(750, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Left panel with shapes decoration
JPanel leftPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw some subtle circles and lines
g2.setColor(new Color(160, 180, 200, 100));
g2.fillOval(30, 30, 60, 60);
g2.fillOval(80, 150, 90, 90);
g2.setColor(new Color(120, 140, 170, 150));
g2.setStroke(new BasicStroke(3f));
g2.drawLine(20, 250, 120, 350);
g2.drawLine(50, 300, 150, 370);
}
};
leftPanel.setPreferredSize(new Dimension(180, 0));
leftPanel.setBackground(new Color(245, 245, 250)); // very light pastel blue
add(leftPanel, BorderLayout.WEST);
// Table area center
String[] cols = {"Title", "Author", "Price", "Quantity"};
tableModel = new DefaultTableModel(cols, 0);
bookTable = new JTable(tableModel);
bookTable.setRowHeight(28);
bookTable.setFont(new Font("SansSerif", Font.PLAIN, 14));
bookTable.setSelectionBackground(new Color(220, 240, 255)); // light blue selection
JScrollPane scrollPane = new JScrollPane(bookTable);
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(scrollPane, BorderLayout.CENTER);
// Right panel for inputs and buttons
JPanel rightPanel = new JPanel();
rightPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBackground(new Color(250, 250, 255));
// Input fields with labels
tfTitle = createInputField("Title:", rightPanel);
tfAuthor = createInputField("Author:", rightPanel);
tfPrice = createInputField("Price:", rightPanel);
tfQuantity = createInputField("Quantity:", rightPanel);
// Buttons panel
JPanel buttonsPanel = new JPanel(new GridLayout(3, 1, 10, 10));
buttonsPanel.setBackground(new Color(250, 250, 255));
btnAdd = createButton("Add Book", new Color(100, 149, 237)); // cornflower blue
btnUpdate = createButton("Update Book", new Color(72, 209, 204)); // medium turquoise
btnDelete = createButton("Delete Book", new Color(255, 99, 71)); // tomato red
buttonsPanel.add(btnAdd);
buttonsPanel.add(btnUpdate);
buttonsPanel.add(btnDelete);
rightPanel.add(Box.createVerticalStrut(20));
rightPanel.add(buttonsPanel);
add(rightPanel, BorderLayout.EAST);
// Button listeners
btnAdd.addActionListener(e -> addBook());
btnUpdate.addActionListener(e -> updateBook());
btnDelete.addActionListener(e -> deleteBook());
// Load selected row into fields
bookTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting() && bookTable.getSelectedRow() != -1) {
loadBookToFields();
}
});
}
private JTextField createInputField(String label, JPanel parent) {
JPanel panel = new JPanel(new BorderLayout(5, 5));
panel.setOpaque(false);
JLabel lbl = new JLabel(label);
lbl.setFont(new Font("SansSerif", Font.BOLD, 13));
JTextField tf = new JTextField(15);
tf.setFont(new Font("SansSerif", Font.PLAIN, 14));
panel.add(lbl, BorderLayout.NORTH);
panel.add(tf, BorderLayout.CENTER);
panel.setMaximumSize(new Dimension(250, 60));
parent.add(panel);
parent.add(Box.createVerticalStrut(10));
return tf;
}
private JButton createButton(String text, Color color) {
JButton btn = new JButton(text);
btn.setBackground(color);
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setFont(new Font("SansSerif", Font.BOLD, 14));
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15));
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
btn.setBackground(color.darker());
}
public void mouseExited(MouseEvent e) {
btn.setBackground(color);
}
});
return btn;
}
private void addBook() {
if (!validateInput()) return;
tableModel.addRow(new Object[]{
tfTitle.getText(),
tfAuthor.getText(),
Double.parseDouble(tfPrice.getText()),
Integer.parseInt(tfQuantity.getText())
});
clearInputs();
JOptionPane.showMessageDialog(this, "Book added successfully!");
}
private void updateBook() {
int row = bookTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "Select a book to update.");
return;
}
if (!validateInput()) return;
tableModel.setValueAt(tfTitle.getText(), row, 0);
tableModel.setValueAt(tfAuthor.getText(), row, 1);
tableModel.setValueAt(Double.parseDouble(tfPrice.getText()), row, 2);
tableModel.setValueAt(Integer.parseInt(tfQuantity.getText()), row, 3);
JOptionPane.showMessageDialog(this, "Book updated successfully!");
}
private void deleteBook() {
int row = bookTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "Select a book to delete.");
return;
}
int confirm = JOptionPane.showConfirmDialog(this, "Delete this book?", "Confirm", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
tableModel.removeRow(row);
clearInputs();
}
}
private void loadBookToFields() {
int row = bookTable.getSelectedRow();
tfTitle.setText(tableModel.getValueAt(row, 0).toString());
tfAuthor.setText(tableModel.getValueAt(row, 1).toString());
tfPrice.setText(tableModel.getValueAt(row, 2).toString());
tfQuantity.setText(tableModel.getValueAt(row, 3).toString());
}
private boolean validateInput() {
if (tfTitle.getText().trim().isEmpty() || tfAuthor.getText().trim().isEmpty() ||
tfPrice.getText().trim().isEmpty() || tfQuantity.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill all fields.");
return false;
}
try {
Double.parseDouble(tfPrice.getText());
Integer.parseInt(tfQuantity.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Price must be a number and Quantity an integer.");
return false;
}
return true;
}
private void clearInputs() {
tfTitle.setText("");
tfAuthor.setText("");
tfPrice.setText("");
tfQuantity.setText("");
bookTable.clearSelection();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CreativeBookStore().setVisible(true);
});
}
}
output

