Write a program that prints the total amount owed for a cart full of groceries t
ID: 3887249 • Letter: W
Question
Write a program that prints the total amount owed for a cart full of groceries to a tex file, and saves the file to a folder on your computers desktop named receipts.the program must satisfy the following requirnments. 1.asks the user to enter the name for the file. 2.continuosly asks the user to enter name and name and weight of the product until he typed done 4the program formats the output so that it lists easch item on the left and subtotoal for that item aigned some space to the right. 5.if the quantitiy of the item is greater than oone then the program I)prints the quantity followed by"@" and the price of one item in parenthesis ii)list the price for one item to the left of the subtotal iii)the subtotal reflects number of items iv)the total is printed at the end of the list,seperated by two new lines the text file with the prices are as follows Popcorn Butter 6 pkg 3.99 for this you need to use java regular expressions
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.september;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
*
* @author Sam
*/
public class ShopingCart {
class Item {
String name;
int quantity;
double rate;
public Item(String name, int quantity, double rate) {
this.name = name;
this.quantity = quantity;
this.rate = rate;
}
}
ArrayList<Item> items;
void fillCart() throws IOException {
Pattern p = Pattern.compile("done");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name;
int quantity;
double rate;
while (true) {
System.out.println("Enter Item name: ");
name = br.readLine();
if (p.matcher(name).matches())
break;
System.out.println("Enter Item quaintity: ");
quantity = Integer.parseInt(br.readLine());
System.out.println("Enter Item rate: ");
rate = Double.parseDouble(br.readLine());
items.add(new Item(name, quantity, rate));
}
}
void printBill() {
PrintWriter pw = new PrintWriter(System.out);
double total = 0;
for (Item item : items ){
pw.print(item.name + " ");
if (item.quantity != 1)
pw.print("("+item.quantity+"@$"+item.rate+")");
else
pw.print(" ");
double cost = item.rate*item.quantity;
pw.println("$" + cost);
total += cost;
}
pw.println("-------------------------------------------");
pw.println(" total: " + total);
pw.close();
}
public ShopingCart() {
items = new ArrayList<>();
}
public static void main(String[] args) throws IOException {
ShopingCart sc = new ShopingCart();
sc.fillCart();
sc.printBill();
}
}
chk this. If you have any trouble, comment below.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.