In real-world applications, billing isn’t just multiplying quantity by price. You often need to include discounts, taxes, and final payable amounts.

Here’s how you can implement this in Java:


🧾 Example: Full Billing Program

public class AdvancedBilling {
    public static void main(String[] args) {
        // Number of items purchased
        int items = 50;

        // Cost of each item
        float costPerItem = 9.99f;

        // Discount and tax
        float discountPercent = 10.0f; // 10% discount
        float taxPercent = 8.0f;       // 8% tax

        // Currency symbol
        char currency = '$';

        // Step 1: Calculate subtotal
        float subtotal = items * costPerItem;

        // Step 2: Calculate discount
        float discountAmount = (subtotal * discountPercent) / 100;

        // Step 3: Calculate tax
        float taxAmount = ((subtotal - discountAmount) * taxPercent) / 100;

        // Step 4: Calculate final total
        float finalTotal = subtotal - discountAmount + taxAmount;

        // Print billing details
        System.out.println("Items Purchased: " + items);
        System.out.println("Cost per Item: " + costPerItem + currency);
        System.out.println("Subtotal: " + subtotal + currency);
        System.out.println("Discount (" + discountPercent + "%): -" + discountAmount + currency);
        System.out.println("Tax (" + taxPercent + "%): +" + taxAmount + currency);
        System.out.println("Final Total: " + finalTotal + currency);
    }
}

✅ Output:

Items Purchased: 50
Cost per Item: 9.99$
Subtotal: 499.5$
Discount (10.0%): -49.95$
Tax (8.0%): +35.96$
Final Total: 485.51$

💡 Key Takeaways:

  • subtotal is calculated before discount and tax.
  • discountAmount is subtracted, while taxAmount is added.
  • Variable names are descriptive for readability and maintenance.
  • Works as the foundation for real e-commerce or POS billing systems.

🚀 GoNimbus Pro Challenge 2:

Upgrade this billing program to:

  • Handle multiple items with different prices using arrays or List.
  • Accept user input for quantity, discount, and tax using Scanner.
  • Format the final price to 2 decimal places using String.format().

Scroll to Top