What's new

Shopping calculator java

Sure, I can help you with that. Here's the code for the Shopping Calculator java program:

Code:
import java.util.Scanner;

public class ShoppingCalculator {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of groceries you want to compute: ");
        int numItems = sc.nextInt();

        String[] items = new String[numItems];
        double[] prices = new double[numItems];

        System.out.println("Enter the Grocery Items and its price: ");
        for (int i = 0; i < numItems; i++) {
            System.out.print("Enter item " + (i + 1) + ": ");
            items[i] = sc.next();
            System.out.print("Enter the price of " + items[i] + ": ");
            prices[i] = sc.nextDouble();
        }

        System.out.println("\nGrocery Items and Prices:");
        double subtotal = 0;
        for (int i = 0; i < numItems; i++) {
            System.out.println(items[i] + " - " + prices[i]);
            subtotal += prices[i];
        }

        double vat = computeVAT(subtotal);
        double totalDue = computeTotalDue(subtotal, vat);
        double lowestPrice = findLowestPrice(prices);

        System.out.println("\nSubtotal: " + subtotal);
        System.out.println("VAT: " + vat);
        System.out.println("Total Due: " + totalDue);
        System.out.println("Lowest Price: " + lowestPrice);
    }

    public static double findLowestPrice(double[] prices) {
        double lowest = prices[0];
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < lowest) {
                lowest = prices[i];
            }
        }
        return lowest;
    }

    public static double computeSubtotal(double[] prices) {
        double subtotal = 0;
        for (int i = 0; i < prices.length; i++) {
            subtotal += prices[i];
        }
        return subtotal;
    }

    public static double computeVAT(double subtotal) {
        double vat = subtotal * 0.1;
        return vat;
    }

    public static double computeTotalDue(double subtotal, double vat) {
        double totalDue = subtotal + vat;
        return totalDue;
    }
}

Explanation:

1. We first ask the user to input the number of groceries to compute using Scanner class.
2. We declare two arrays to store the name and prices of the groceries entered by the user.
3. We then ask the user to input the name and price of each grocery item using a for-loop.
4. After getting all the inputs, we display the grocery items and prices using another for-loop and calculate the subtotal by adding up all the individual prices.
5. We then call the following four methods to compute the lowest price, subtotal, VAT, and Total Due, passing the appropriate parameters.
6. Finally, we display the computed values for Subtotal, VAT, Total Due, and Lowest Price using System.out.println().

I hope this helps! Let me know if you have any questions.
 

Similar threads

Back
Top