Hey there! First-time visitor? Contact Us About

Write a Java program to create a simple shopping cart application with the following requirements.

For this question, we are going to create a simple shopping cart application with the following requirements.

1) Your program will ask the customer for the name of the product and then the price of the product. 
2) Your program should then ask the customer if they would like to buy more products. 
3) If the customer responds "Yes", then your program should repeat step 1 and then step 2. 
4) If the customer responds "No", then your program should calculate the total price for the shopping cart. 
5) Finally, your program should print the item list, i.e, the name of the products and the price of the product (one product and its price in each line), and lastly the total amount. 
6) A customer can buy as many products as they want. The program should use lists and loops to achieve the task.
Ans: 
import java.util.ArrayList;
import java.util.Scanner;

public class ShoppingCart {
    public static void main(String[] args) {
        ArrayList<String> itemList = new ArrayList<>();
        ArrayList<Double> priceList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        String moreProduct = "yes";

        while (moreProduct.equalsIgnoreCase("yes")) {
            System.out.print("Enter the name of the product: ");
            String productName = scanner.nextLine();
            System.out.print("Enter the price of the product: ");
            double productPrice = scanner.nextDouble();
            scanner.nextLine();
            itemList.add(productName);
            priceList.add(productPrice);
            System.out.print("Do you want to buy more products? (yes/no) ");
            moreProduct = scanner.nextLine();
        }

        double total = 0;
        for (double price : priceList) {
            total += price;
        }

        System.out.println("Item List:");
        for (int i = 0; i < itemList.size(); i++) {
            System.out.println(itemList.get(i) + " - $" + priceList.get(i));
        }
        System.out.println("Total: $" + total);
    }
}
This program uses two ArrayLists, one to store the names of the products and one to store the prices of the products. The program uses a while loop to repeatedly ask the user if they would like to add more products to the cart, and within the while loop, it prompts the user for the name and price of each product, then adds them to the appropriate ArrayLists. Once the user indicates that they are finished adding products, the program calculates the total price by summing all of the prices in the priceList ArrayList, and then prints out the item list and the total price.

About the Author

Experienced YouTuber with a demonstrated history of working in the Information Technology industry. Strong media and communication professional with a BCA focused in Science, Technology, and Society from Purbanchal University. Skilled in Graphics de…

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.