Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Supermarket Billing System

import java.util.*;

class Item {
String name;
double price;
int quantity;

public Item(String name, double price, int quantity) {


this.name = name;
this.price = price;
this.quantity = quantity;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

public int getQuantity() {


return quantity;
}

public double getTotalPrice() {


return price * quantity;
}
}

class Supermarket {
private List<Item> items;

public Supermarket() {
items = new ArrayList<>();
}

public void addItem(String name, double price, int quantity) {


Item item = new Item(name, price, quantity);
items.add(item);
}

public void displayItems() {


if (items.isEmpty()) {
System.out.println("No items in the supermarket.");
return;
}
System.out.println("List of Items:");
for (Item item : items) {
System.out.println("Item: " + item.getName() + ", Price: $" + item.getPrice() +
", Quantity: " + item.getQuantity() + ", Total: $" + item.getTotalPrice());
}
}

public double calculateTotalBill() {


double totalBill = 0;
for (Item item : items) {
totalBill += item.getTotalPrice();
}
return totalBill;
}
}

public class SupermarketBillingSystem {


public static void main(String[] args) {
Supermarket supermarket = new Supermarket();
Scanner scanner = new Scanner(System.in);
int choice;

do {
System.out.println("\nSupermarket Billing System Menu:");
System.out.println("1. Add Item");
System.out.println("2. Display Items");
System.out.println("3. Calculate Total Bill");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = Integer.parseInt(scanner.nextLine());

switch (choice) {
case 1:
System.out.print("Enter item name: ");
String itemName = scanner.nextLine();
System.out.print("Enter item price: ");
double itemPrice = Double.parseDouble(scanner.nextLine());
System.out.print("Enter item quantity: ");
int itemQuantity = Integer.parseInt(scanner.nextLine());

supermarket.addItem(itemName, itemPrice, itemQuantity);


break;
case 2:
supermarket.displayItems();
break;
case 3:
double totalBill = supermarket.calculateTotalBill();
System.out.println("Total Bill: $" + totalBill);
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please enter a valid option.");
}
} while (choice != 4);
scanner.close();
}
}

You might also like