Java Program for Shopping Bill

Write a Java program for Shopping Bill | Simple Billing Program in Java | Write a Java program that will ask the user to enter the price and quantity of an item.

Here we will create one Java class “Product” having properties product_name, quantity, the price per item, the total price for that particular item. Those properties or fields will be private so that no one can access them directly. To create the object, only one constructor with all properties will be there. At the end of the application, before display the result we will display all product will properties, and for that reason display() method is used to having some printing formats.

Required input values:- product_name, quantity, the price per item
Sample Output:-

Name      Quantity    Price   Total Price
Fan             2    1500.00    3000.00
Computer        1   50000.00   50000.00
Mobile          5    4000.00   20000.00
Book            3     100.00     300.00

Total Price = 73300.0

Java Program for Shopping Bill Code

Now, let us first develop Java Program for Shopping Bill, and then we will discuss the code. In this program, we will use List and ArrayList of Java Collection classes.

package com.know.shop;

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

class Product {
  // properties
  private String pname;
  private int qty;
  private double price;
  private double totalPrice;

  // constructor
  Product(String pname, int qty, 
              double price, double totalPrice) {
    this.pname = pname;
    this.qty = qty;
    this.price = price;
    this.totalPrice = totalPrice;
  }

  // getter methods
  public String getPname() {
    return pname;
  }
  public int getQty() {
    return qty;
  }
  public double getPrice() {
    return price;
  }
  public double getTotalPrice() {
    return totalPrice;
  }

  // displayFormat
  public static void displayFormat() {
    System.out.print(
        "\nName      Quantity    Price   Total Price\n");
  }

  // display
  public void display() {
    System.out.format("%-9s %8d %10.2f %10.2f\n", 
         pname, qty, price, totalPrice);
  }
}

public class ShoppingBill {
  public static void main(String[] args) {
    
    // variables
    String productName = null;
    int quantity = 0;
    double price = 0.0;
    double totalPrice = 0.0;
    double overAllPrice = 0.0;
    char choice = '\0';

    // create Scanner class object
    Scanner scan = new Scanner(System.in);

    List<Product> product = new ArrayList<Product>();

    do {
      // read input values
      System.out.println("Enter product details,");
      System.out.print("Name: ");
      productName = scan.nextLine();
      System.out.print("Quantity: ");
      quantity = scan.nextInt();
      System.out.print("Price (per item): ");
      price = scan.nextDouble();

      // calculate total price for that product
      totalPrice = price * quantity;

      // calculate overall price
      overAllPrice += totalPrice;

      // create Product class object and add it to the list
      product.add( new Product(
          productName, quantity, price, totalPrice) );

      // ask for continue?
      System.out.print("Want to add more item? (y or n): ");
      choice = scan.next().charAt(0);

      // read remaining characters, don't store (no use)
      scan.nextLine();
    } while (choice == 'y' || choice == 'Y');

    // display all product with its properties
    Product.displayFormat();
    for (Product p : product) {
      p.display();
    }

    // overall price
    System.out.println("\nTotal Price = " + overAllPrice);

    // close Scanner
    scan.close();
  }

}

Output:-

Enter product details,
Name: Fan
Quantity: 2
Price (per item): 1500
Want to add more items? (y or n): y
Enter product details,
Name: Computer
Quantity: 1
Price (per item): 50000
Want to add more items? (y or n): y
Enter product details,
Name: Mobile
Quantity: 5
Price (per item): 4000
Want to add more items? (y or n): y
Enter product details,
Name: Book
Quantity: 3
Price (per item): 100
Want to add more items? (y or n): n

Name Quantity Price Total Price
Fan 2 1500.00 3000.00
Computer 1 50000.00 50000.00
Mobile 5 4000.00 20000.00
Book 3 100.00 300.00


Total Price = 73300.0

In the Product class, we have used getter methods (getXxx() ) to get the values. Since to set the properties values we are using constructors and not modifying the product details after input therefore we are not using setter methods (setXxx() ). The display the product details rather than overriding the toString() method we are using the format() method. The format() method works similar to printf() in C, and we can also use printf() in Java. System.out.printf(“…”) and  System.out.format(“…”) both work very similarly.

In the ShoppingBill class, we have taken all the required variables and created the Scanner class object to read the input. To make the product size dynamic, we are using a collection rather than a normal array. We are using the do-while loop to repeat the process, which will continue until you want. In the do-while loop, first, we are reading the input values (name, quantity, and price per item). Then we are calculating the total price and adding it to the overall price. 

Now, using those 4 properties create an object of the Product class and add it to the collection. After that asked the to user for continue. At last, all product details are displayed and the overall price is displayed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

2 thoughts on “Java Program for Shopping Bill”

Leave a Comment

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