Billing Program Java

 import java.util.Scanner;


public class SimpleBillingProgram {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        // Variables for shop details

        String shopName;

        String billNumber;

        double discountPercent;

        double taxPercent;


        // Get shop details

        System.out.print("Enter shop name: ");

        shopName = scanner.nextLine();

        System.out.print("Enter bill number: ");

        billNumber = scanner.nextLine();

        System.out.print("Enter discount percentage: ");

        discountPercent = scanner.nextDouble();

        System.out.print("Enter tax percentage: ");

        taxPercent = scanner.nextDouble();

        scanner.nextLine();


        // Arrays to store item details

        String[] itemNames = new String[100];

        double[] itemPrices = new double[100];

        int[] itemQuantities = new int[100];

        int itemCount = 0;


        // Menu loop

        int choice = 0;

        while (choice != 4) {

            System.out.println("\n╔════════════════════════════════╗");

            System.out.println("║      BILLING SYSTEM MENU       ║");

            System.out.println("╠════════════════════════════════╣");

            System.out.println("║ 1. Add Item                    ║");

            System.out.println("║ 2. View Items                  ║");

            System.out.println("║ 3. Print Bill                  ║");

            System.out.println("║ 4. Exit                        ║");

            System.out.println("╚════════════════════════════════╝");

            System.out.print("Choose an option: ");

            choice = scanner.nextInt();

            scanner.nextLine();


            // Choice 1: Add Item

            if (choice == 1) {

                if (itemCount < 100) {

                    System.out.print("Enter item name: ");

                    itemNames[itemCount] = scanner.nextLine();

                    System.out.print("Enter price: ");

                    itemPrices[itemCount] = scanner.nextDouble();

                    System.out.print("Enter quantity: ");

                    itemQuantities[itemCount] = scanner.nextInt();

                    scanner.nextLine();

                    itemCount = itemCount + 1;

                    System.out.println("✓ Item added successfully!");

                } else {

                    System.out.println("✗ Cannot add more items!");

                }

            }


            // Choice 2: View Items

            else if (choice == 2) {

                if (itemCount == 0) {

                    System.out.println("\n⚠ No items in bill!");

                } else {

                    System.out.println("\n" + "─".repeat(60));

                    System.out.println(String.format("%-30s %12s %10s %10s",

                            "Item", "Price", "Qty", "Subtotal"));

                    System.out.println("─".repeat(60));


                    int i = 0;

                    while (i < itemCount) {

                        double subtotal = itemPrices[i] * itemQuantities[i];

                        String formattedPrice = String.format("%.2f", itemPrices[i]);

                        String formattedSubtotal = String.format("%.2f", subtotal);


                        // Truncate item name if too long

                        String displayName = itemNames[i];

                        if (displayName.length() > 30) {

                            displayName = displayName.substring(0, 27) + "...";

                        }


                        System.out.println(String.format("%-30s %12s %10d %10s",

                                displayName,

                                "₹" + formattedPrice,

                                itemQuantities[i],

                                "₹" + formattedSubtotal));

                        i = i + 1;

                    }

                    System.out.println("─".repeat(60));

                }

            }


            // Choice 3: Print Bill

            else if (choice == 3) {

                if (itemCount == 0) {

                    System.out.println("\n⚠ No items in bill!");

                } else {

                    // Calculate totals

                    double subtotal = 0.0;

                    int i = 0;

                    while (i < itemCount) {

                        subtotal = subtotal + (itemPrices[i] * itemQuantities[i]);

                        i = i + 1;

                    }


                    double discount = subtotal * (discountPercent / 100.0);

                    double afterDiscount = subtotal - discount;

                    double tax = afterDiscount * (taxPercent / 100.0);

                    double finalAmount = afterDiscount + tax;


                    // Print bill header

                    System.out.println("\n" + "═".repeat(80));


                    // Center shop name

                    int padding = (80 - shopName.length()) / 2;

                    int j = 0;

                    while (j < padding) {

                        System.out.print(" ");

                        j = j + 1;

                    }

                    System.out.println(shopName);


                    // Center "CASH BILL"

                    padding = (80 - 9) / 2;

                    j = 0;

                    while (j < padding) {

                        System.out.print(" ");

                        j = j + 1;

                    }

                    System.out.println("CASH BILL");


                    System.out.println("═".repeat(80));


                    // Bill details line

                    System.out.println("Bill No: " + billNumber + String.format("%50s", "Date: 06-07-2026"));

                    System.out.println("─".repeat(80));


                    // Table header

                    System.out.println(String.format("%-35s %12s %10s %18s",

                            "ITEM", "PRICE", "QTY", "SUBTOTAL"));

                    System.out.println("─".repeat(80));


                    // Print items

                    i = 0;

                    while (i < itemCount) {

                        double itemSubtotal = itemPrices[i] * itemQuantities[i];

                        String formattedPrice = String.format("%.2f", itemPrices[i]);

                        String formattedSubtotal = String.format("%.2f", itemSubtotal);


                        String displayName = itemNames[i];

                        if (displayName.length() > 35) {

                            displayName = displayName.substring(0, 32) + "...";

                        }


                        System.out.println(String.format("%-35s %12s %10d %18s",

                                displayName,

                                "₹" + formattedPrice,

                                itemQuantities[i],

                                "₹" + formattedSubtotal));

                        i = i + 1;

                    }


                    System.out.println("─".repeat(80));


                    // Summary section

                    String formattedSubtotal = String.format("%.2f", subtotal);

                    String formattedDiscount = String.format("%.2f", discount);

                    String formattedAfterDiscount = String.format("%.2f", afterDiscount);

                    String formattedTax = String.format("%.2f", tax);

                    String formattedFinal = String.format("%.2f", finalAmount);


                    System.out.println(String.format("%-48s %32s", 

                            "Subtotal:", 

                            "₹" + formattedSubtotal));


                    if (discountPercent > 0) {

                        String discountLabel = "Discount (" + String.format("%.1f", discountPercent) + "%):";

                        System.out.println(String.format("%-48s %32s", 

                                discountLabel, 

                                "-₹" + formattedDiscount));

                    }


                    System.out.println(String.format("%-48s %32s", 

                            "After Discount:", 

                            "₹" + formattedAfterDiscount));


                    if (taxPercent > 0) {

                        String taxLabel = "Tax (" + String.format("%.1f", taxPercent) + "%):";

                        System.out.println(String.format("%-48s %32s", 

                                taxLabel, 

                                "₹" + formattedTax));

                    }


                    System.out.println("═".repeat(80));

                    System.out.println(String.format("%-48s %32s", 

                            "TOTAL AMOUNT:", 

                            "₹" + formattedFinal));

                    System.out.println("═".repeat(80));


                    // Footer

                    padding = (80 - 25) / 2;

                    j = 0;

                    while (j < padding) {

                        System.out.print(" ");

                        j = j + 1;

                    }

                    System.out.println("Thank you for your purchase!");


                    padding = (80 - 15) / 2;

                    j = 0;

                    while (j < padding) {

                        System.out.print(" ");

                        j = j + 1;

                    }

                    System.out.println("Visit again soon!");


                    System.out.println("═".repeat(80) + "\n");

                }

            }


            // Choice 4: Exit

            else if (choice == 4) {

                System.out.println("Thank you for using Billing System!");

            }


            // Invalid choice

            else {

                System.out.println("✗ Invalid choice. Please try again.");

            }

        }


        scanner.close();

    }

}


Comments

Popular Posts