Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi here is the code for Grocery billing.I have to get the output in sorted(based on item name ) and write the output to a

Hi here is the code for Grocery billing.I have to get the output in sorted(based on item name ) and write the output to a file.I am stuck for these .kindly help me with the code to get desired output

//Main.java

import java.util.Scanner;

import java.text.NumberFormat;

public class Main

{

public static void main (String[] args)

{

Scanner scan = new Scanner (System.in);

NumberFormat fmt = NumberFormat.getCurrencyInstance();

int capacity = 0;

String name = "";

int quantity;

double price;

ShoppingCart3 cart1 = new ShoppingCart3();

System.out.print ("\fWelcome! Would you like to begin shopping? (Y/N): ");

char begin = scan.nextLine ().charAt(0);

if ( begin == 'N' || begin == 'n' )

{

System.out.println ("Thank you for visiting!");

}

if ( begin != 'n' || begin != 'n' )

{

while ( begin != 'n' && begin != 'N' )

{

System.out.print ("To begin, type in the name of the item you "

+ "wish to buy: ");

name = scan.nextLine ();

System.out.print ("What is the item's price? ");

price = scan.nextDouble ();

System.out.print ("How many of the item would you like to buy? ");

quantity = scan.nextInt ();

System.out.println ();

System.out.println ("You have completed your " + (capacity + 1) +

" entry.");

capacity++;

scan.nextLine();

cart1.addToCart (name, price, quantity);

System.out.println ("-----------------------------");

System.out.println (cart1);

System.out.println ("-----------------------------");

System.out.print ("Would you like to continue shopping? (Y/N): ");

begin = scan.nextLine().charAt(0);

}

System.out.println ("Thank you for shopping. Here is your final item list: ");

System.out.println ();

System.out.println ("------------------------------");

System.out.println (cart1);

System.out.println ("------------------------------");

System.out.println ();

System.out.println ("Please pay: " + fmt.format (cart1.getTotalPrice()));

}

}

}

//Item.java

import java.text.NumberFormat;

public class Item

{

private String name;

private double price;

private int quantity;

public Item (String itemName, double itemPrice, int numPurchased)

{

name = itemName;

price = itemPrice;

quantity = numPurchased;

}

public String toString ()

{

NumberFormat fmt = NumberFormat.getCurrencyInstance();

return (name + "\t\t" + fmt.format(price) + "\t\t" + quantity + "\t\t"

+ fmt.format(price*quantity));

}

public double getPrice()

{

return price;

}

public String getName()

{

return name;

}

public int getQuantity()

{

return quantity;

}

}

//ShoppingCart3.java

import java.text.NumberFormat;

import java.util.ArrayList;

import java.util.*;

public class ShoppingCart3

{

private double totalPrice;

private ArrayList cart;

public ShoppingCart3()

{

totalPrice = 0.0;

cart = new ArrayList();

}

public void addToCart(String itemName, double price, int quantity)

{

cart.add(new Item (itemName, price, quantity));

totalPrice += price * quantity;

}

public String toString()

{

NumberFormat fmt = NumberFormat.getCurrencyInstance();

String contents = " Shopping Cart ";

contents += " Item\t\tUnit Price\tQuantity\tTotal ";

for (int i = 0; i < cart.size(); i++)

contents += cart.get(i).toString() + " ";

contents += " Total Price: " + fmt.format(totalPrice);

contents += " ";

return contents;

}

public double getTotalPrice()

{

return totalPrice;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions