You need to create a console application that keeps track of a person’s grocery
ID: 3636075 • Letter: Y
Question
You need to create a console application that keeps track of a person’s grocery items and their prices.
Application Musts:
? The user does not know how many items they will purchase.
? Ask the user for the name of each item.
? Ask the user for the price of each item.
Calculate the subtotal, 5.5% tax, and total cost. Output a list of the names of all items sold to a text file called “InventorySold.txt”.
Display on the console a receipt, similar to:
************************************** ---WELCOME TO THE OOP GROCERY STORE--- ************************************** Item Price Bouquet of Roses $12.99 Gallon of Milk $1.98 Bananas, 0.8 lb. $0.50
-------------------------------------- Subtotal: $15.47 Tax (0.08%): $0.85 Total: $16.32 **************************************
Explanation / Answer
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Scanner; public class shopping { public static void main(String[] a) { String input=""; Scanner scan = new Scanner(System.in); double subtotal = 0; String Receipt="************************************** ---WELCOME TO THE OOP GROCERY STORE--- ************************************** Item Price "; System.out.print("Enter the name of your first item"); try { System.out.println(input); PrintStream out = new PrintStream(new FileOutputStream( "InventorySold.txt")); out.print("Items Sold ************ "); input= scan.nextLine(); while(!input.matches("checkout")) { out.println(input); Receipt=Receipt+input+" "; System.out.print("Enter the price of the item. $"); input= scan.nextLine(); subtotal=+Double.parseDouble(input); Receipt=Receipt+input+" "; System.out.println("Enter the next item or enter checkout to finish"); input= scan.nextLine(); } Receipt=Receipt+"-------------------------------------- "; Receipt=Receipt+"Subtotal: $"+subtotal+" "; Receipt=Receipt+"Tax (5.5%): $" + subtotal*.055+" "; Receipt=Receipt+ "Total: $"+ subtotal*1.055+" "; Receipt=Receipt+"**************************************"; System.out.println(Receipt); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.