Java, the user selects an item from the text files ranging 1-15, repeats the loop until 'q'. the program provides a list of what they ordered including discounts, then calculates the total bill and how much they saved. The bottom is my the code I'm working on

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter5: Making Decisions
Section: Chapter Questions
Problem 5GZ
icon
Related questions
Question

Java, the user selects an item from the text files ranging 1-15, repeats the loop until 'q'. the program provides a list of what they ordered including discounts, then calculates the total bill and how much they saved.

The bottom is my the code I'm working on

class BillProcessor {
public static void prepareBill(LinkedHashMap<String, Integer> purchases, LinkedHashMap<String,Double>items,
LinkedHashMap<String,String> sales, ArrayList<String> itemNames){
double totalCost, totalDiscount, actualCost, discount; //note actualCost is the price of a single item

totalCost = 0.00;
totalDiscount = 0.00;
actualCost = 0.00;
discount = 0.00;

for(Map.Entry<String,Integer>set: purchases.entrySet()) {
if (sales.get(set.getKey()) != null) {

if (sales.get(set.getKey()).equals("bogo") == true) {
actualCost = set.getValue() * items.get(set.getKey());
totalCost += actualCost;
System.out.println("Item Name : " + set.getKey() + "\tItem Quantity(bogo) : " + (set.getValue() * 2
+ "\tItem Cost : " + actualCost));
} else {
discount = Double.valueOf(sales.get(set.getKey()));
actualCost = set.getValue() * (items.get(set.getKey()) - discount);
totalCost = totalCost + actualCost;
totalDiscount = totalDiscount + discount;
System.out.println("Item Name : " + set.getKey() + "\tItem Quantity(sale) : " + set.getValue()
+ "\tItem Cost : " + actualCost);
}
} else {
actualCost = set.getValue() * items.get(set.getKey());
totalCost += actualCost;
System.out.println("Item Name : " + set.getKey() + "\tItem Quantity : " + set.getValue()
+ "\tItem Cost : " + actualCost);
}
}
System.out.printf("Your total bill is $%.2f.\n", totalCost);
System.out.printf("You saved $%.2f by shopping with us today.", totalDiscount);
}
}

public class GarciaGroceryStore {
private static LinkedHashMap<String, Double> items;
private static LinkedHashMap<String, String> sales;
private static LinkedHashMap<String, Integer> purchases;
private static ArrayList<String>itemNames;

public static LinkedHashMap<String,Double> readItemsFromFile(String fname){
LinkedHashMap<String, Double> result = new LinkedHashMap<String, Double>();
itemNames = new ArrayList<String>();

try {
File file = new File(fname);
FileReader fr = new FileReader(file); //open the file
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null) {
String item[] = line.split(" ");
result.put(item[0], Double.valueOf(item[1]));
itemNames.add(item[0]);
}
fr.close();
Collections.sort(itemNames);
return result;
} catch (IOException e){
e.printStackTrace();
}
return result;
}

public static LinkedHashMap<String, String> readSalesFromFile(String fname){
LinkedHashMap<String, String> result = new LinkedHashMap<String,String>();
try {
File file = new File(fname);
FileReader fr = new FileReader(file); // reads the file
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
String item[] = line.split(" ");
result.put(item[0], item[1]);
}
fr.close();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

public static void printBanner() {
System.out.println("****************************************************************");
System.out.println("* CHARLIE'S PANTRY *");
System.out.println("****************************************************************");
System.out.println();
System.out.println("Welcome to your friendly neighborhood Charlie's Pantry. We sell\r\n"
+ "only the highest quality groceries and freshest produce around.\r\n"
+ "We have many great specials this week. The more you buy, the\r\n"
+ "more you save!\r\n"
+ "");
}

public static void showTheMenu() {
System.out.println("What would you like to buy?\n");
System.out.println("## Item Name Reg. Sale");
System.out.println("-------------------------------------------------------\n");

for (String item: itemNames) {
System.out.printf("%-10s %-10.2f %-10.2f", item, sales.get(item));
}
System.out.println("-------------------------------------------------------");
}

//The main
public static void main(String[] args) {
String choice;
char quit = 'q';
double totalCost, totalDiscount;
Scanner sc = new Scanner(System.in);
purchases = new LinkedHashMap<String, Integer>();
items = readItemsFromFile("items.txt");
sales = readSalesFromFile("sales.txt");


//present to the user the table

printBanner();
showTheMenu();

do {
System.out.print("");

//ask them to choose
System.out.println("Enter the number of your choice, or q to check out: ");
choice = (sc.nextLine()).charAt(0);


} while (quit != 'q');

sc.close();
BillProcessor.prepareBill(purchases, items, sales, itemNames);
System.out.println("\nThank you for your business. Come back soon!");
}
}

What would you like to buy?
##
Reg. Sale
Item Name
аples
blueberries
0.99
2
1.29
1.99 $0.20 discount
4.19 Buy One, Get One
1.09 Buy One, Get One
3
bread
4
cereal
eggs
frozen pizza
6.59
ground beef
hamburgers
milk
7
2.49
8
12.99 $2.00 discount
9
2.39
5.99 $0.30 discount
mountain dew
peanut butter
roast beef
10
11
3.99
12
3.49
13
2.99
sausage
string cheese
tamales
14
1.29
15
7.19 $0.80 discount
Enter the number of your choice, or g to check out: 1
Enter the number of your choice, or g to check out: 5
Enter the number of your choice, or q to check out: 13
Enter the number of your choice, or q to check out: 9
Enter the number of your choice, or q to check out: 8
Enter the number of your choice, orq to check out: 7
Enter the number of your choice, or g to check out: 5
Enter the number of your choice, or q to check out: 4
Enter the number of your choice, or q to check out: 4
Enter the number of your choice, or q to check out: 1
Enter the number of your choice, or q to check out: 4
Enter the number of your choice, or q to check out: 2
Enter the number of your choice, or g to check out: 5
Enter the number of your choice, or q to check out: 3
Enter the number of your choice, or q to check out: 8
Enter the number of your choice, or q to check out: 6
Enter the number of your choice, or q to check out: 4
Enter the number of your choice, or q to check out: 9
Here is what you purchased:
2 apples, regularly $0.99 each, total $1.98
3 eggs, regularly $1.09 each, total $2.18 ($1.09 discount)
1 sausage, regularly $2.99 each, total $2.99
1 milk, regularly $2.39 each, total $2.39
2 hamburgers, regularly $12.99 each, total $21.98 ($4.00 discount)
1 ground beef, regularly $2.49 each, total $2.49
4 cereal, regularly $4.19 each, total $8.38 ($8.38 discount)
1 blueberries, regularly $1.29 each, total $1.29
1 bread, regularly $1.99 each, total $1.79 ($0.20 discount)
1 frozen pizza, regularly $6.59 each, total $6.59
Your total bill is $52.06.
You saved $13.67 by shopping with us today.
Transcribed Image Text:What would you like to buy? ## Reg. Sale Item Name аples blueberries 0.99 2 1.29 1.99 $0.20 discount 4.19 Buy One, Get One 1.09 Buy One, Get One 3 bread 4 cereal eggs frozen pizza 6.59 ground beef hamburgers milk 7 2.49 8 12.99 $2.00 discount 9 2.39 5.99 $0.30 discount mountain dew peanut butter roast beef 10 11 3.99 12 3.49 13 2.99 sausage string cheese tamales 14 1.29 15 7.19 $0.80 discount Enter the number of your choice, or g to check out: 1 Enter the number of your choice, or g to check out: 5 Enter the number of your choice, or q to check out: 13 Enter the number of your choice, or q to check out: 9 Enter the number of your choice, or q to check out: 8 Enter the number of your choice, orq to check out: 7 Enter the number of your choice, or g to check out: 5 Enter the number of your choice, or q to check out: 4 Enter the number of your choice, or q to check out: 4 Enter the number of your choice, or q to check out: 1 Enter the number of your choice, or q to check out: 4 Enter the number of your choice, or q to check out: 2 Enter the number of your choice, or g to check out: 5 Enter the number of your choice, or q to check out: 3 Enter the number of your choice, or q to check out: 8 Enter the number of your choice, or q to check out: 6 Enter the number of your choice, or q to check out: 4 Enter the number of your choice, or q to check out: 9 Here is what you purchased: 2 apples, regularly $0.99 each, total $1.98 3 eggs, regularly $1.09 each, total $2.18 ($1.09 discount) 1 sausage, regularly $2.99 each, total $2.99 1 milk, regularly $2.39 each, total $2.39 2 hamburgers, regularly $12.99 each, total $21.98 ($4.00 discount) 1 ground beef, regularly $2.49 each, total $2.49 4 cereal, regularly $4.19 each, total $8.38 ($8.38 discount) 1 blueberries, regularly $1.29 each, total $1.29 1 bread, regularly $1.99 each, total $1.79 ($0.20 discount) 1 frozen pizza, regularly $6.59 each, total $6.59 Your total bill is $52.06. You saved $13.67 by shopping with us today.
The items they are selling come from a tab-delimited text file called items.txt that looks like this:
Eggs 1.09
Roast Beef
Ground Beef 2.49
3.49
Milk
2.39
Cereal 4.19
Bread 1.99
Apples 0.99
Blueberries 1.29
Sausage
Mountain Dew 5.99
2.99
Frozen Pizza 6.59
Tamales
String Cheese1.29
Hamburgers
7.19
12.99
Peanut Butter3.99
The sales they are currently offering are defined in a tab-delimited text file called sales.txt. It looks like
this:
Mountain Dew 0.30
Cereal bogo
Bread 0.20
Hamburgers
Tamales
Eggs bogo
2.00
0.80
Lines that end in "bogo" are buy-one-get-one for that item. Lines that end in a decimal number indicate
that that item is discounted by that amount.
Your program will function like a modern grocery store cash register, where the cashier scans an item at
a time. Of course, our computers aren't connected to bar code scanners, but we will mimic that
situation by having the user type the number of the next item in their shopping cart, one at a time, as if
it were being scanned.
Transcribed Image Text:The items they are selling come from a tab-delimited text file called items.txt that looks like this: Eggs 1.09 Roast Beef Ground Beef 2.49 3.49 Milk 2.39 Cereal 4.19 Bread 1.99 Apples 0.99 Blueberries 1.29 Sausage Mountain Dew 5.99 2.99 Frozen Pizza 6.59 Tamales String Cheese1.29 Hamburgers 7.19 12.99 Peanut Butter3.99 The sales they are currently offering are defined in a tab-delimited text file called sales.txt. It looks like this: Mountain Dew 0.30 Cereal bogo Bread 0.20 Hamburgers Tamales Eggs bogo 2.00 0.80 Lines that end in "bogo" are buy-one-get-one for that item. Lines that end in a decimal number indicate that that item is discounted by that amount. Your program will function like a modern grocery store cash register, where the cashier scans an item at a time. Of course, our computers aren't connected to bar code scanners, but we will mimic that situation by having the user type the number of the next item in their shopping cart, one at a time, as if it were being scanned.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,