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:
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.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);}}