Grocery Bill Program

Grocery Bill Program

trying to write a program where the user is able to insert food price and whether or not it is on sale I'm supposed to use a while loop to see if they have more items or not then display the total tax and final bill. Im trying to find a way to print out all the values inputted by the user in the loop but can't find a way how I got it to ask until the user has no items just need to get all the items printed out in the output

 public static void main (String[] args)
   {
      double price;
      String decision;
      String item;

      do
      {
      Scanner scan = new Scanner(System.in);

      //Name of the item
      System.out.println("What is the name of your item?");
      item = scan.nextLine(); 

      //Price of the item
      System.out.println("What is the price of your item?");
      price = scan.nextDouble();

      //Whether item is on sale
      System.out.println("Is this item on sale?");
      String sale = scan.next();

      System.out.println("Do you have more items? (Yes/No)");
      decision = scan.next(); 
      }while (decision.equalsIgnoreCase("yes"));

      System.out.println("Grocery bill");
      System.out.println();
      System.out.println("purchases");
      System.out.print(item + ": " + "$" + price);

   }

so far this is what i have

2 Answers

How about you use a List and some class for state e.g.

class State{  
  public double price;
  public String decision;
  public String item;
}

Then make a new instance from your input and store the result in a list.

At the end loop through the list of items and print their results.

As the previous answer stated, creating a separate class to store the item details is a great approach. Since you do not know in advance how many items would be there, you can loop using do-while to collect item details and then keep adding the new item to the List till you are done and then finally pass that list to a method where you print the details and total. Here is a basic implementation that takes details of n items and then prints it.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        String itemName;
        Double itemPrice;
        String itemOnSale;
        String moreItems;

        List<Item> items = new ArrayList<>();

        try (Scanner scan = new Scanner(System.in)) {
            do {
                // Name of the item
                System.out.println("What is the name of your item?");
                itemName = scan.next();

                // Price of the item
                System.out.println("What is the price of your item?");
                itemPrice = scan.nextDouble();

                // Whether item is on sale
                System.out.println("Is this item on sale?(Yes/No)");
                itemOnSale = scan.next();

                System.out.println("Do you have more items? (Yes/No)");
                moreItems = scan.next();

                items.add(new Item(itemName, itemPrice, itemOnSale));

            } while (moreItems.equalsIgnoreCase("yes"));
        } finally {
            printBill(items);
        }

    }

    private static void printBill(List<Item> items) {
        double total = 0.00;

        System.out.println("**************  Grocery Bill  ******************");

        System.out.println("*** ITEM NAME **** ITEM PRICE **** ON SALE ? ****");

        for (Item item : items) {
            System.out.printf("    %s             %.2f           %s           \n", item.itemName, item.price,
                    item.itemOnSale);
            total += item.price;
        }

        System.out.println("************************************************");
        System.out.println("Total :" + total);
    }
}

class Item {

    String itemName;
    double price;
    String itemOnSale;

    public Item(String itemName, double price, String itemOnSale) {
        this.itemName = itemName;
        this.price = price;
        this.itemOnSale = itemOnSale;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.