Java Program for Shopping Bill
We will go through how to write Java program-for shopping bills in this article. We require the product name, ID, quantity, price per item, total product price, and cumulative total in order to build the shopping bill. In addition to the product details, we can include other details like the date of purchase, any applicable discounts, the SGST and CGST rates, the name of the point of sale, the address, the phone number, and so forth.
Two classes, one called Products and the other called Bill, are created in the Java program-that follow.
The product's name, quantity, price per unit, and total price are among the information contained in the Products class. These properties are now private, as we have indicated. In order to prevent direct access to these features. We have developed a constructor for the Products class and will pass all of the product properties to it in order to build an object of the Products class. Additionally, the class contains a display() function that shows the bill on the console.
The item name, quantity, and price per unit are read by the Bill class, which then computes the total amount due. Also displays a character 'Y' or 'N' following the entry of a product's detail. After entering the data of each product, hit 'Y' if you wish to enter more than one; otherwise, press 'N.' It displays the entire cost.
The display() method is needed to have some printing formats since, at the end of the program, we will display all product properties before presenting the outcome.
Implementation:
FileName: ShoppingBill.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.io.*;
class Products
{
// creation of the product properties
private String pid;
private String prodname;
private int quantity;
private double price;
private double overallPrice;
// constructor
Products(String pid, String prodname, int quantity, double price, double overallPrice)
{
this.pid=pid;
this.prodname = prodname;
this.quantity = quantity;
this.price = price;
this.overallPrice = overallPrice;
}
// getter methods
public String getId()
{
return pid;
}
public String getProdname()
{
return prodname;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
public double overallPrice()
{
return overallPrice;
}
//format displaying the bill
public static void displayFormat()
{
System.out.format("-----------------------------------------------------------------------------------");
System.out.print("\nProduct ID \t\tName\t\tQuantity\t\tRate \t\t\t\tTotal Price\n");
System.out.format("-----------------------------------------------------------------------------------\n");
}
// display function
public void display()
{
System.out.format(" %-9s %-9s %5d %9.2f %14.2f\n" ,pid, prodname, quantity, price, overallPrice);
}
}
public class ShoppingBill
{
public static void main(String args[])
{
String id = null;
String prodName = null;
int qty = 0;
double price = 0.0;
double totalcost = 0.0;
double totalprice = 0.0;
double cgst, sgst, subtotal=0.0, discount=0.0;
char choice = '\0';
System.out.println("\t\t\t\t--------------------Invoice-----------------");
System.out.println("\t\t\t\t\t "+" "+"Metro Mart Grocery Shop");
System.out.println("\t\t\t\t\t3/98 Nilkamal Grocery Super Market");
System.out.println("\t\t\t\t\t" +" " +"Opposite Balanagar Metro Station");
System.out.println("GSTIN: 03AWBPP8756K592"+"\t\t\t\t\t\t\tContact: (+91) 9875146901");
// format of the particulars like, date and time
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
// prints the time and date currently right now.
System.out.println("Date: "+formatter.format(date)+" "+days[calendar.get(Calendar.DAY_OF_WEEK) - 1]+"\t\t\t\t\t\t (+91) 7799541470");
Scanner scan = new Scanner(System.in);
System.out.print("Enter Name of the Customer : ");
String customername=scan.nextLine();
// make an object of the Scanner class
//building an ArrayList to hold the products.
List<Products> product = new ArrayList<Products>();
do
{
// read input values
System.out.println("Enter the details of the product : ");
System.out.print("Product ID: ");
id = scan.nextLine();
System.out.print("Product Name: ");
prodName = scan.nextLine();
System.out.print("Quantity: ");
qty = scan.nextInt();
System.out.print("Price (per unit): ");
price = scan.nextDouble();
// determine the overall cost of a specific item.
totalcost = price * qty;
// determines the total cost
totalprice = totalprice + totalcost;
// constructs an object of the Product class and adds it to the List
product.add( new Products(id, prodName, qty, price, totalcost));
// request to carry on shopping?
System.out.print("Want to add more items? (Y or N): ");
// reads a Y or N character
choice = scan.next().charAt(0);
// read the remaining characters and discard (don't store)
scan.nextLine();
}while (choice == 'Y');
// display every product together with its attributes
Products.displayFormat();
for (Products p : product)
{
p.display();
}
// calculation of overall price
System.out.println("\n\t\t\t\t\t\t\t\t\t\tTotal Amount (Rs.) " + totalprice);
// calculating the discount amount
discount = totalprice*2/100;
System.out.println("\n\t\t\t\t\t\t\t\t\t\t Discount (Rs.) " +discount);
// calculation of total amount after discount
subtotal = totalprice-discount;
System.out.println("\n\t\t\t\t\t\t\t\t\t\t Subtotal "+subtotal);
// calculation of tax like CGST, SGST
sgst = totalprice*12/100;
System.out.println("\n\t\t\t\t\t\t\t\t\t\t SGST (%) "+sgst);
cgst = totalprice*12/100;
System.out.println("\n\t\t\t\t\t\t\t\t\t\t CGST (%) "+cgst);
//determining the buyer's payment amount
System.out.println("\n\t\t\t\t\t\t\t\t\t\t Invoice Total " +(subtotal+cgst+sgst));
System.out.println("\t\t\t\t----------------Thank You for Shopping!!-----------------");
System.out.println("\t\t\t\t Visit Again");
// close the procedure
scan.close();
}
}
Output: