Java Project – Supermarket Billing System

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

Managing transactions efficiently and accurately is crucial in today’s fast-paced world, especially in busy environments like supermarkets. That’s where the Supermarket Billing System comes into play. It is an innovative solution built in Java that aims to revolutionize the way billing operations are handled.

In the following project, we will delve into this extraordinary system, its characteristics, and the way it streamlines the supermarket billing procedure. So, get ready to discover a simpler way to handle supermarket transactions. Together, we will uncover the future of efficient and accurate billing, elevating the shopping experience for everyone involved.

About Java Supermarket Billing System Project

We will implement the following functionalities in the Java Supermarket Billing System Project:

1) We will define a Product class to represent a product with its name, price, and quantity.

2) We will define a ShoppingCart class to represent a shopping cart, which will contain a list of products and a discount. The ShoppingCart class will have methods to add/remove items from the cart, calculate the total cost after applying the discount, print the invoice, and save the invoice to a file.

3) The SupermarketBillingSystem class will serve as the main class for the program. We will display a menu with options to add items to the cart, remove items, view the cart, apply a discount, generate an invoice, download the invoice, or exit the program. We will take user input accordingly to perform the selected actions using the methods of the ShoppingCart class.

Hence, the Supermarket Billing System Project will allow us to add and remove items from a shopping cart, apply a discount, view the cart with the total cost, generate and print an invoice, and save the invoice to a file.

Prerequisites For Supermarket Billing System Using Java

To write and run the Java Supermarket Billing System Project code, the following prerequisites are required:

1) Basic understanding of the Java programming language, including knowledge of loops, conditional statements, and object-oriented programming concepts.
2) Familiarity with array data structures and Java.util package, which provides utility classes like Random and Arrays.
3) A working Java development environment, which includes installing the Java Development Kit (JDK) on your computer. You can download the JDK from the official Oracle website or use a package manager specific to your operating system.
4) An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans, can greatly simplify Java development. Choose an IDE you are comfortable with and set it up.

Download Java Supermarket Billing System Project

Please download the source code of the Java Supermarket Billing System Project: Java Supermarket Billing System Project Code.

Code Breakdown

The code can be divided into three main sections: the Product class, the ShoppingCart class, and the SupermarketBillingSystem class.

1) Product Class:

  • This class represents a product.
  • It has private member variables name, price, and quantity.
  • It has a constructor Product(String name, double price, int quantity) to initialize the product with the given name, price, and quantity.
  • It provides getters for the product name, price, and quantity.
// Class representing a product
class Product {
private String name;
private double price;
private int quantity;

// Constructor for the Product class
public Product(String name, double price, int quantity) {
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

// Getter for the product name
public String getName() {
    return name;
}

// Getter for the product price
public double getPrice() {
    return price;
}

// Getter for the product quantity
public int getQuantity() {
    return quantity;
}
}

2) ShoppingCart Class:

  • This class represents a shopping cart.
  • It has private member variables which are items and discounts.
  • It has a constructor ShoppingCart() that initializes an empty list of items and sets the discount to 0.0.
  • It provides methods to add an item to the cart, remove an item from the cart based on the index, calculate the total cost of items after applying the discount, and set the discount amount. It also helps to print the invoice with item details, get the cart size, get an item from the cart based on the index, and save the invoice to a file.
  • The calculateTotal() method iterates over the items in the cart and calculates the total cost by multiplying the price of each item with its quantity and subtracting the discount.
  • The printInvoice() method prints the invoice with details of all items, the total cost, and the applied discount.
  • The saveInvoiceToFile() method writes the invoice details to a file named “invoice.txt”.
// Class representing a shopping cart
class ShoppingCart {
private List<Product> items;
private double discount;

// Constructor for the ShoppingCart class
public ShoppingCart() {
    items = new ArrayList<>();
    discount = 0.0;
}

// Add a product to the cart
public void addItem(Product product) {
    items.add(product);
}

// Remove a product from the cart based on the index
public void removeItem(int index) {
    items.remove(index);
}

// Calculate the total cost of all items in the cart after applying the discount
public double calculateTotal() {
    double total = 0;
    for (Product item : items) {
        total += item.getPrice() * item.getQuantity();
    }
    total -= discount;
    return total;
}

// Set the discount amount
public void setDiscount(double discount) {
    this.discount = discount;
}

// Print the invoice with details of all items, the total cost, and the applied discount
public void printInvoice() {
    System.out.println("----------- INVOICE -----------");
    for (int i = 0; i < items.size(); i++) {
        Product item = items.get(i);
        System.out.println((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity());
    }
    System.out.println("-------------------------------");
    System.out.println("Total: $" + calculateTotal());
    System.out.println("Discount: $" + discount);
    System.out.println("-------------------------------");
}

public int getCartSize() {
    return items.size();
}

public Product getItem(int i) {
    return items.get(i);
}

// Save the invoice to a file
public void saveInvoiceToFile() {
    try (FileWriter writer = new FileWriter("invoice.txt")) {
        writer.write("----------- INVOICE -----------\n");
        for (int i = 0; i < items.size(); i++) {
            Product item = items.get(i);
            writer.write((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity() + "\n");
        }
        writer.write("-------------------------------\n");
        writer.write("Total: $" + calculateTotal() + "\n");
        writer.write("Discount: $" + discount + "\n");
        writer.write("-------------------------------\n");
        System.out.println("Invoice saved to file.");
    } catch (IOException e) {
        System.out.println("Failed to save invoice to file: " + e.getMessage());
    }
}
}

3) SupermarketBillingSystem Class:

  • This is the main class representing the Supermarket Billing System.
  • It has a main() method that serves as the entry point of the program.
  • The showMenu() method displays the main menu and handles user input by using a Scanner.
  • It provides methods for adding an item to the cart, removing an item from the cart, viewing the cart, applying a discount, generating an invoice, and downloading the invoice.
  • The addItemToCart() method prompts the user to enter the product details and creates a new Product object, then adds it to the cart instance of the ShoppingCart class.
  • The removeItemFromCart() method prompts the user to enter the item number to remove and removes the corresponding item from the cart.
  • The viewCart() method displays the current items in the cart along with the total cost.
  • The applyDiscount() method prompts the user to enter a discount amount and applies the discount to the cart.
  • The generateInvoice() method generates an invoice for the current cart, prints it, and clears the cart.
  • The downloadInvoice() method saves the invoice to a file and clears the cart after downloading.
// Main class representing the Supermarket Billing System
public class SupermarketBillingSystem {
private static Scanner scanner = new Scanner(System.in);
private static ShoppingCart cart = new ShoppingCart();

// Entry point of the program
public static void main(String[] args) {
    showMenu();
}

// Display the main menu and handle user input
private static void showMenu() {
    int choice;
    do {
        System.out.println("------ Supermarket Billing System by ProjectGurukul ------");
        System.out.println("1. Add item to cart");
        System.out.println("2. Remove item from cart");
        System.out.println("3. View cart");
        System.out.println("4. Apply discount");
        System.out.println("5. Generate invoice");
        System.out.println("6. Download invoice");
        System.out.println("7. Exit");
        System.out.println("----------------------------------------");
        System.out.print("Enter your choice: ");
        choice = scanner.nextInt();

        switch (choice) {
            case 1:
                addItemToCart();
                break;
            case 2:
                removeItemFromCart();
                break;
            case 3:
                viewCart();
                break;
            case 4:
                applyDiscount();
                break;
            case 5:
                generateInvoice();
                break;
            case 6:
                downloadInvoice();
                break;
            case 7:
                System.out.println("Thank you for using the Supermarket Billing System by ProjectGurukul!");
                break;
            default:
                System.out.println("Invalid choice. Please try again.");
                break;
        }
    } while (choice != 7);
}

// Add an item to the cart based on user input
private static void addItemToCart() {
    System.out.print("Enter product name: ");
    String name = scanner.next();
    System.out.print("Enter price: ");
    double price = scanner.nextDouble();
    System.out.print("Enter quantity: ");
    int quantity = scanner.nextInt();

    Product product = new Product(name, price, quantity);
    cart.addItem(product);

    System.out.println("Item added to cart!");
}

// Remove an item from the cart based on user input
private static void removeItemFromCart() {
    System.out.print("Enter item number to remove: ");
    int index = scanner.nextInt();

    if (index >= 1 && index <= cart.getCartSize()) {
        cart.removeItem(index - 1);
        System.out.println("Item removed from cart!");
    } else {
        System.out.println("Invalid item number.");
    }
}

// View the current items in the cart along with the total cost
private static void viewCart() {
    if (cart.getCartSize() > 0) {
        System.out.println("------ Cart Items ------");
        for (int i = 0; i < cart.getCartSize(); i++) {
            Product item = cart.getItem(i);
            System.out.println((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity());
        }
        System.out.println("------------------------");
        System.out.println("Total: $" + cart.calculateTotal());
        System.out.println("------------------------");
    } else {
        System.out.println("Cart is empty.");
    }
}

// Apply a discount to the total cost based on user input
private static void applyDiscount() {
    System.out.print("Enter discount amount: ");
    double discount = scanner.nextDouble();
    cart.setDiscount(discount);
    System.out.println("Discount applied!");
}

// Generate an invoice for the current cart and clear the cart
private static void generateInvoice() {
    if (cart.getCartSize() > 0) {
        cart.printInvoice();
        cart = new ShoppingCart();  // Clear the cart after generating the invoice
    } else {
        System.out.println("Cart is empty. Cannot generate invoice.");
    }
}

// Download the invoice as a file
private static void downloadInvoice() {
    if (cart.getCartSize() > 0) {
        cart.saveInvoiceToFile();
        cart = new ShoppingCart();  // Clear the cart after downloading the invoice
    } else {
        System.out.println("Cart is empty. Cannot download invoice.");
    }
}
}

Overall, this code represents a simple Supermarket Billing System that allows users to add items to a shopping cart, apply a discount, view the cart, generate and print an invoice, and download the invoice as a file.

Code to illustrate the Java Supermarket Billing System Project

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

// Class representing a product
class Product {
private String name;
private double price;
private int quantity;

// Constructor for the Product class
public Product(String name, double price, int quantity) {
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

// Getter for the product name
public String getName() {
    return name;
}

// Getter for the product price
public double getPrice() {
    return price;
}

// Getter for the product quantity
public int getQuantity() {
    return quantity;
}
}

// Class representing a shopping cart
class ShoppingCart {
private List<Product> items;
private double discount;

// Constructor for the ShoppingCart class
public ShoppingCart() {
    items = new ArrayList<>();
    discount = 0.0;
}

// Add a product to the cart
public void addItem(Product product) {
    items.add(product);
}

// Remove a product from the cart based on the index
public void removeItem(int index) {
    items.remove(index);
}

// Calculate the total cost of all items in the cart after applying the discount
public double calculateTotal() {
    double total = 0;
    for (Product item : items) {
        total += item.getPrice() * item.getQuantity();
    }
    total -= discount;
    return total;
}

// Set the discount amount
public void setDiscount(double discount) {
    this.discount = discount;
}

// Print the invoice with details of all items, the total cost, and the applied discount
public void printInvoice() {
    System.out.println("----------- INVOICE -----------");
    for (int i = 0; i < items.size(); i++) {
        Product item = items.get(i);
        System.out.println((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity());
    }
    System.out.println("-------------------------------");
    System.out.println("Total: $" + calculateTotal());
    System.out.println("Discount: $" + discount);
    System.out.println("-------------------------------");
}

public int getCartSize() {
    return items.size();
}

public Product getItem(int i) {
    return items.get(i);
}

// Save the invoice to a file
public void saveInvoiceToFile() {
    try (FileWriter writer = new FileWriter("invoice.txt")) {
        writer.write("----------- INVOICE -----------\n");
        for (int i = 0; i < items.size(); i++) {
            Product item = items.get(i);
            writer.write((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity() + "\n");
        }
        writer.write("-------------------------------\n");
        writer.write("Total: $" + calculateTotal() + "\n");
        writer.write("Discount: $" + discount + "\n");
        writer.write("-------------------------------\n");
        System.out.println("Invoice saved to file.");
    } catch (IOException e) {
        System.out.println("Failed to save invoice to file: " + e.getMessage());
    }
}
}

// Main class representing the Supermarket Billing System
public class SupermarketBillingSystem {
private static Scanner scanner = new Scanner(System.in);
private static ShoppingCart cart = new ShoppingCart();

// Entry point of the program
public static void main(String[] args) {
    showMenu();
}

// Display the main menu and handle user input
private static void showMenu() {
    int choice;
    do {
        System.out.println("------ Supermarket Billing System by ProjectGurukul ------");
        System.out.println("1. Add item to cart");
        System.out.println("2. Remove item from cart");
        System.out.println("3. View cart");
        System.out.println("4. Apply discount");
        System.out.println("5. Generate invoice");
        System.out.println("6. Download invoice");
        System.out.println("7. Exit");
        System.out.println("----------------------------------------");
        System.out.print("Enter your choice: ");
        choice = scanner.nextInt();

        switch (choice) {
            case 1:
                addItemToCart();
                break;
            case 2:
                removeItemFromCart();
                break;
            case 3:
                viewCart();
                break;
            case 4:
                applyDiscount();
                break;
            case 5:
                generateInvoice();
                break;
            case 6:
                downloadInvoice();
                break;
            case 7:
                System.out.println("Thank you for using the Supermarket Billing System by ProjectGurukul!");
                break;
            default:
                System.out.println("Invalid choice. Please try again.");
                break;
        }
    } while (choice != 7);
}

// Add an item to the cart based on user input
private static void addItemToCart() {
    System.out.print("Enter product name: ");
    String name = scanner.next();
    System.out.print("Enter price: ");
    double price = scanner.nextDouble();
    System.out.print("Enter quantity: ");
    int quantity = scanner.nextInt();

    Product product = new Product(name, price, quantity);
    cart.addItem(product);

    System.out.println("Item added to cart!");
}

// Remove an item from the cart based on user input
private static void removeItemFromCart() {
    System.out.print("Enter item number to remove: ");
    int index = scanner.nextInt();

    if (index >= 1 && index <= cart.getCartSize()) {
        cart.removeItem(index - 1);
        System.out.println("Item removed from cart!");
    } else {
        System.out.println("Invalid item number.");
    }
}

// View the current items in the cart along with the total cost
private static void viewCart() {
    if (cart.getCartSize() > 0) {
        System.out.println("------ Cart Items ------");
        for (int i = 0; i < cart.getCartSize(); i++) {
            Product item = cart.getItem(i);
            System.out.println((i + 1) + ". " + item.getName() + "\t$" + item.getPrice() + "\tQty: " + item.getQuantity());
        }
        System.out.println("------------------------");
        System.out.println("Total: $" + cart.calculateTotal());
        System.out.println("------------------------");
    } else {
        System.out.println("Cart is empty.");
    }
}

// Apply a discount to the total cost based on user input
private static void applyDiscount() {
    System.out.print("Enter discount amount: ");
    double discount = scanner.nextDouble();
    cart.setDiscount(discount);
    System.out.println("Discount applied!");
}

// Generate an invoice for the current cart and clear the cart
private static void generateInvoice() {
    if (cart.getCartSize() > 0) {
        cart.printInvoice();
        cart = new ShoppingCart();  // Clear the cart after generating the invoice
    } else {
        System.out.println("Cart is empty. Cannot generate invoice.");
    }
}

// Download the invoice as a file
private static void downloadInvoice() {
    if (cart.getCartSize() > 0) {
        cart.saveInvoiceToFile();
        cart = new ShoppingCart();  // Clear the cart after downloading the invoice
    } else {
        System.out.println("Cart is empty. Cannot download invoice.");
    }
}
}

Java Supermarket Billing System Output

Supermarket Billing System

Supermarket Billing System output

output Supermarket Billing System

Supermarket Billing

Supermarket Billing output

output Supermarket Billing

Supermarket Billing System java

Supermarket Billing System java output

Summary

The Supermarket Billing System, built on the foundation of Java programming, offers a powerful solution to simplify and enhance the process of managing supermarket transactions.

In this Java supermarket billing system project, we have thoroughly examined the diverse features and advantages offered by this groundbreaking system. We sincerely appreciate your participation in this exploration of the Supermarket Billing System.

Our hope is that this project has furnished you with valuable insights and motivation to leverage the potential of Java programming in transforming your supermarket’s billing procedures. Embrace the future of supermarket transactions and unlock the potential for growth and success.

Leave a Reply

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