Shopping Bill in Java
Java Shopping Bill
Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity), prod_price (Price per item) and lastly, total_prod_price (Total price of the product). Those properties or attributes of the class have been declared private, so no one outside of the class can access them directly. To create an object, only one constructor is used, which will initialize all the data members of the Products class. At the end of the application, we are using the display() function, having some printing formats to print all the properties or attributes of the Products class before displaying the result.
Now, let's develop a java program for shopping bills, and after that, we will discuss the program code. We will use Arraylist and List of the Java Collection classes in this program.
package com.java.shop;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
classProducts{
// attributes of the Products
privateString prod_name;
privateint qty;
privatedouble prod_price;
privatedouble total_prod_price;
// constructor
Products(String prod_name,int qty,
double prod_price,double total_prod_price){
this.prod_name = prod_name;
this.qty = qty;
this.prod_price = prod_price;
this.total_prod_price = total_prod_price;
}
// getter methods
publicString get_prod_name(){
return prod_name;
}
publicint get_Qty(){
return qty;
}
publicdouble get_prod_price(){
return prod_price;
}
publicdouble get_total_prod_price(){
return total_prod_price;
}
// Display Format function
publicstaticvoid display_Format(){
System.out.print(
"\nName qty prod_price Total prod_price\n");
}
// display
publicvoid display(){
System.out.format("%-8s %7d %9.2f %9.2f\n",
prod_name, qty, prod_price, total_prod_price);
}
}
publicclassShopping_Bill{
publicstaticvoid main(String[] args){
// variables
String Products_Name =null;
int qty =0;
double prod_price =0.0;
double total_prod_price =0.0;
double overAll_prod_price =0.0;
char user_choice ='\0';
// Creating a scanner class object with the help of the new Keyword.
Scanner scan =newScanner(System.in);
List<Products> Products =newArrayList<Products>();
do{
// reading the input values from the user
System.out.println("Enter Products details,");
System.out.print("Name: ");
Products_Name = scan.nextLine();
System.out.print("qty: ");
qty = scan.nextInt();
System.out.print("prod_price (per item): ");
prod_price = scan.nextDouble();
// Calculating the total price of the product.
total_prod_price = prod_price * qty;
// calculating the overall price of the product.
overAll_prod_price += total_prod_price;
// creating a Products class object and adding it to the list
Products.add(newProducts(
Products_Name, qty, prod_price, total_prod_price));
// ask whether to continue or not??
System.out.print("Wanna to add more item? (yes or no): ");
user_choice = scan.nextLine();
}while(user_choice ==“yes”|| user_choice ==“YES”);
// display all Products with their properties
Products.display_Format();
for(Products p: Products){
p.display();
}
// overall prod_price
System.out.println("\nTotal prod_price = "+ overAll_prod_price);
// close Scanner
scan.close();
}
}
Discussion:-
In the Products class, we have used getter methods namely get_prod_name(), get_prod_price(), get_Qty() and get_total_prod_price() to get their respective values since they are declared private inside the class. To set the properties or attribute values, we are using the constructor, and we are not modifying the properties of the class after taking the input from the user. Therefore, we are not using any setter function. To display the product details rather than overriding the toString() methods, we are using the format() method. The format(() work exactly the same like printf() in C, and we can also use printf in Java. System.out.println("....") and System.out.format(".....") both works very similarly.
In the Products class, we have taken all the required attributes value of the class and then created a scanner class object to read the input values from the user. To make the product size dynamic, we are using a collection rather than a regular 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 all the input values from the user (name, quantity, and price per item). Then we calculate the total price and add it to the overall cost.
Now, using those four properties or attributes, create an object of the Products class and add it to the collection. After that, I asked them, the user, to continue. In the end, all the product details are displayed, and the overall price is displayed.