Categories: Java

Book Shop Inventory System in Java

The BookShop Inventory Management System is developed by Java and MySQL.java best practice is must if you interested in java. The project is built to manage books and transactions. To make a new transaction, fields such as: book name, qty and payment needs to be selected. If you like to learn point of sales systems step by step, this is the right place to learn from the beginning.

In this tutorial, you will learn  jasper reports step by step and designing the java print receipt.java coding practices is very important  for us to grow the knowledge in java day by day.how to be a good programmer in the future. Let go and learn the java mini projects step by step.i have attached the complete project video also how the flow works step by step below.

In order to create the system we have used NetBeans IDE and Mysql database as a backend.

Features of the Project

  • Point of Sales
  • Stock Management
  • Java Receipt

Learn how to make this System Step by step

Step 1: Create the database named with “bookshop“. then established the database connection to download the mysql connector in order to connect Java & mysql.if you don’t  have an idea about  download the mysql connector you can follow this link.

To connect the database please refer the following code. To create the method name connect (); paste the below code inside the Connect () method.

public void Connect()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver"); // Register the mysql driver
            con = DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root",""); 
               //Connection Path where your database is located.     
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
           ex.printStackTrace();
        }
    }

After that call the method Connect() inside the constructor. When the programming is started to run, all the forms will be loaded along with the connection method. Pasted the Connect() method inside to the constructor.

 public BookShop() {
        initComponents();
        Connect();
    }

Search Record

The system shall be able search the book name, price by their “book id”.

Paste this Code inside the keypress Event of the textfield. we created the textfield name which is txtbcode of this project.

    private void txtbcodeKeyPressed(java.awt.event.KeyEvent evt) 
{                                    
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            try {
                String bcode = txtbcode.getText(); 
                pst = con.prepareStatement("select * from book where id = ?");
                pst.setString(1, bcode);
                rs = pst.executeQuery();
                
               if(rs.next() == false)
               {
                   JOptionPane.showMessageDialog(this, "Book Code not Found");
               }
               else
               {
                   String bname = rs.getString("bname");
                   txtbname.setText(bname.trim());
                   String price = rs.getString("price");
                   txtprice.setText(price.trim()); 
                   txtqty.requestFocus();
               }
  
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

    }

Add the Product details into the JTable

After receiving the product name and price where the user has the option to add the qty by clicking the add button to see all Products details which will be shown in the below table.

Paste this code inside the add button

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
        int price = Integer.parseInt(txtprice.getText());
        int qty = Integer.parseInt(txtqty.getValue().toString());
        int tot = price * qty;
        df = (DefaultTableModel)jTable3.getModel();

        df.addRow(new Object[]
        {
            txtbname.getText(),
            txtprice.getText(),
            txtqty.getValue().toString(),
            tot
        });
        int sum = 0;
        for(int i=0; i<jTable3.getRowCount(); i++)
        {
            sum = sum + Integer.parseInt(jTable3.getValueAt(i, 3).toString());
        }
            txttcost.setText(String.valueOf(sum));
            txtbcode.setText("");
            txtbname.setText("");
            txtprice.setText("");
            txtqty.setValue(0);
            txtbcode.requestFocus();
    }

Calculating the Total and balance

After that  create the method Sales().we have to create two different tables to store data into the database. we also have to create the following tables from database.
Sales : tables consist of following colums – id,subtotal,pay,bal.
Sales_product : tables consist of following –  id,sales_id,bname,price,qty,total.

    public void sales()
    {
        String totalcost = txttcost.getText();
        String pay = txtpay.getText();
        String bal = txtbal.getText();
        int lastid =0;
        try {
             
            String query = "insert into sales(subtotal,pay,bal)values(?,?,?)";
            pst = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
            pst.setString(1, totalcost);
            pst.setString(2, pay); 
            pst.setString(3, bal);
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            if(rs.next())
            {
                lastid = rs.getInt(1);
            }
              int rows = jTable3.getRowCount();
              
              String query1 = "insert into sales_product(sales_id,bname,price,qty,total)values(?,?,?,?,?)";
              pst1 = con.prepareStatement(query1);
              
              String bname ="";
              String price;
              String qty;
              int total = 0;
              
              for(int i=0; i<jTable3.getRowCount(); i++)
              {
                   bname = (String)jTable3.getValueAt(i, 0);
                   price = (String)jTable3.getValueAt(i, 1);
                   qty = (String)jTable3.getValueAt(i, 2);
                   total = (int)jTable3.getValueAt(i, 3);
                    pst1.setInt(1, lastid);
                    pst1.setString(2, bname);
                    pst1.setString(3, price);
                     pst1.setString(4, qty);
                     pst1.setInt(5, total);
                     pst1.executeUpdate();
              }
              
              JOptionPane.showMessageDialog(this, "Sales Completedddddddddd");
              
              HashMap a = new HashMap();
              a.put("invo", lastid);

            try {
                JasperDesign jdesign = JRXmlLoader.load("C:\\Users\\kobinath\\Documents\\NetBeansProjects\\BookShopInventory\\src\\BookShop\\report1.jrxml");
                JasperReport jreport = JasperCompileManager.compileReport(jdesign);
                
                JasperPrint jprint = JasperFillManager.fillReport(jreport, a,con);
                
               JasperViewer.viewReport(jprint);

            } catch (JRException ex) {
               ex.printStackTrace();
            }

        } catch (SQLException ex) {
           ex.printStackTrace();
        }

    }

Call the Sales() method inside the Print Invoice button. after calculating the total print receipt will be released. this print receipt is design by Jsper Reporting.

Paste this code inside the Print Invoice button

       int pay = Integer.parseInt(txtpay.getText());
       int totalcost = Integer.parseInt(txttcost.getText());          
       int bal = pay - totalcost;   
       txtbal.setText(String.valueOf(bal));
       sales();

Print Recipt

i have attached the video link below. which will do this tutorials step by step.java projects with source codes will help you to learn java.

admin

Recent Posts

Build Simple Water System Calculator in Java Using Swing

If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…

6 days ago

GitHub Copilot vs Microsoft Copilot Best AI Tool to Use in 2025

GitHub is a powerful tool used by teams and developers around the globe. This guide is…

1 week ago

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

2 weeks ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

2 weeks ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

2 weeks ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

3 weeks ago