0% found this document useful (0 votes)
4 views6 pages

Grocery Store Billing System in Java

The document is a Java program for a grocery store billing system that allows users to select products, specify quantities, and generate a bill. It maintains a list of products and uses a HashMap to manage the shopping cart, combining quantities of the same product. The program also prints a final bill with the total amount due, along with the current date and time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Grocery Store Billing System in Java

The document is a Java program for a grocery store billing system that allows users to select products, specify quantities, and generate a bill. It maintains a list of products and uses a HashMap to manage the shopping cart, combining quantities of the same product. The program also prints a final bill with the total amount due, along with the current date and time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

import [Link].*;

import [Link];

public class GroceryStoreBilling {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Products in store (NO DATABASE)

List<Item> products = new ArrayList<>();

[Link](new Item(1, "Rice 5kg", 250));

[Link](new Item(2, "Wheat Flour 5kg", 200));

[Link](new Item(3, "Sugar 1kg", 40));

[Link](new Item(4, "Milk 1L", 30));

[Link](new Item(5, "Oil 1L", 120));

// cart using HashMap → merges quantity if repeated

Map<Integer, CartEntry> cart = new HashMap<>();

String storeName = "SUPER MART GROCERY STORE";

[Link]("\
n============================================
===");

[Link](" " + storeName);

[Link](" GROCERY STORE BILLING SYSTEM");


[Link]("================================
===============");

boolean continueShopping = true;

while (continueShopping) {

[Link]("\n------------- PRODUCT MENU -------------");

for (Item i : products) {

[Link]([Link] + ". " + [Link] + " - Rs. " + [Link]);

[Link]("0. Finish & Generate Bill");

[Link]("----------------------------------------");

[Link]("Enter Product Number: ");

int choice = [Link]();

if (choice == 0) break;

Item selected = null;

for (Item i : products) {

if ([Link] == choice) {

selected = i;

break;

if (selected == null) {

[Link]("❌ Invalid product number. Try again.");


continue;

[Link]("Enter Quantity: ");

int qty = [Link]();

if (qty <= 0) {

[Link]("❌ Quantity must be greater than 0!");

continue;

// Add to cart → combine quantity if product already exists

if ([Link]([Link])) {

[Link]([Link]).quantity += qty;

} else {

[Link]([Link], new CartEntry(selected, qty));

[Link]("✔ Added: " + [Link] + " x " + qty);

// SPECIAL CONDITION:

// If customer bought 2 or more products, give option to add more

if ([Link]() >= 2) {

[Link]("Do you want to add more products? (yes/no):


");

String ans = [Link]().toLowerCase();

if (![Link]("yes")) {

continueShopping = false;
}

generateBill(cart, storeName);

[Link]();

// BILL PRINTING METHOD

public static void generateBill(Map<Integer, CartEntry> cart, String


storeName) {

// Get current date & time

LocalDateTime now = [Link]();

DateTimeFormatter dateFormatter =
[Link]("dd-MM-yyyy");

DateTimeFormatter timeFormatter =
[Link]("HH:mm:ss");

[Link]("\n==================== FINAL BILL


====================");

[Link]("Store: " + storeName);

[Link]("Date: " + [Link](dateFormatter));

[Link]("Time: " + [Link](timeFormatter));

[Link]("----------------------------------------------------");

[Link]("%-20s %-10s %-10s %-10s\n",

"Product", "Price", "Qty", "Total");

[Link]("----------------------------------------------------");

double grandTotal = 0;
for (CartEntry entry : [Link]()) {

double lineTotal = [Link] * [Link];

grandTotal += lineTotal;

[Link]("%-20s %-10.2f %-10d %-10.2f\n",

[Link], [Link],

[Link], lineTotal);

[Link]("----------------------------------------------------");

[Link]("GRAND TOTAL: Rs. %.2f\n", grandTotal);

[Link]("================================
====================");

[Link](" THANK YOU! VISIT AGAIN");

[Link]("================================
====================\n");

// ITEM CLASS

class Item {

int id;

String name;

double price;

Item(int id, String name, double price) {

[Link] = id;
[Link] = name;

[Link] = price;

// CART ENTRY CLASS

class CartEntry {

Item item;

int quantity;

CartEntry(Item item, int quantity) {

[Link] = item;

[Link] = quantity;

You might also like