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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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!");
}
}

**Shopping List and Checkout Simulation**

**Available Items:**

| ## | Item Name     | Reg. Price | Sale Details                |
|----|---------------|------------|-----------------------------|
| 1  | Apples        | $0.99      |                             |
| 2  | Blueberries   | $1.29      |                             |
| 3  | Bread         | $1.99      | $0.20 discount              |
| 4  | Cereal        | $4.19      | Buy One, Get One            |
| 5  | Eggs          | $1.09      | Buy One, Get One            |
| 6  | Frozen Pizza  | $6.59      |                             |
| 7  | Ground Beef   | $2.49      |                             |
| 8  | Hamburgers    | $12.99     | $2.00 discount              |
| 9  | Milk          | $2.39      |                             |
| 10 | Mountain Dew  | $5.99      | $0.30 discount              |
| 11 | Peanut Butter | $3.99      |                             |
| 12 | Roast Beef    | $3.49      |                             |
| 13 | Sausage       | $2.99      |                             |
| 14 | String Cheese | $1.29      |                             |
| 15 | Tamales       | $7.19      | $0.80 discount              |

---

**Shopping Choices and Checkout:**

To make a purchase, enter the item number of your choice. You can continue shopping by entering the numbers corresponding to your desired items or check out by entering 'q'. Below is an example of a purchase process:

- Enter the number of your choice, or 'q' to check out: (List includes purchases like apples, eggs, bread, etc.)

**Summary of Purchased Items:**

- 2 Apples: $1.98 total (Regularly $0.99 each)
- 3 Eggs: $2.18 total (Buy One, Get One, $1.09 each, $1.09 discount)
- 1 Sausage: $2.99 total (Regularly $2.99 each)
- 1 Milk: $2.39 total (Regularly $2.39 each)
- 2 Hamburgers: $21.98 total (Regularly $12
Transcribed Image Text:**Shopping List and Checkout Simulation** **Available Items:** | ## | Item Name | Reg. Price | Sale Details | |----|---------------|------------|-----------------------------| | 1 | Apples | $0.99 | | | 2 | Blueberries | $1.29 | | | 3 | Bread | $1.99 | $0.20 discount | | 4 | Cereal | $4.19 | Buy One, Get One | | 5 | Eggs | $1.09 | Buy One, Get One | | 6 | Frozen Pizza | $6.59 | | | 7 | Ground Beef | $2.49 | | | 8 | Hamburgers | $12.99 | $2.00 discount | | 9 | Milk | $2.39 | | | 10 | Mountain Dew | $5.99 | $0.30 discount | | 11 | Peanut Butter | $3.99 | | | 12 | Roast Beef | $3.49 | | | 13 | Sausage | $2.99 | | | 14 | String Cheese | $1.29 | | | 15 | Tamales | $7.19 | $0.80 discount | --- **Shopping Choices and Checkout:** To make a purchase, enter the item number of your choice. You can continue shopping by entering the numbers corresponding to your desired items or check out by entering 'q'. Below is an example of a purchase process: - Enter the number of your choice, or 'q' to check out: (List includes purchases like apples, eggs, bread, etc.) **Summary of Purchased Items:** - 2 Apples: $1.98 total (Regularly $0.99 each) - 3 Eggs: $2.18 total (Buy One, Get One, $1.09 each, $1.09 discount) - 1 Sausage: $2.99 total (Regularly $2.99 each) - 1 Milk: $2.39 total (Regularly $2.39 each) - 2 Hamburgers: $21.98 total (Regularly $12
**Text Transcription and Explanation**

**Items for Sale:**
The items being sold come from a tab-delimited text file named `items.txt`, which lists products and their prices as shown below:

- Eggs: $1.09
- Roast Beef: $3.49
- Ground Beef: $2.49
- Milk: $2.39
- Cereal: $4.19
- Bread: $1.99
- Apples: $0.99
- Blueberries: $1.29
- Sausage: $2.99
- Mountain Dew: $5.99
- Frozen Pizza: $6.59
- Tamales: $7.19
- String Cheese: $1.29
- Hamburgers: $12.99
- Peanut Butter: $3.99

**Current Sales:**
Sales are specified in a separate tab-delimited text file called `sales.txt`. It includes discounts and promotions as follows:

- Mountain Dew: $0.30 off
- Cereal: Buy-one-get-one (bogo)
- Bread: $0.20 off
- Hamburgers: $2.00 off
- Tamales: $0.80 off
- Eggs: Buy-one-get-one (bogo)

**Explanation of Sales Information:**
- Lines ending with "bogo" indicate a buy-one-get-one-free offer for the specified item.
- Lines ending with a decimal number indicate a discount of that amount on the item's price.

**Program Functionality:**
The program simulates a modern grocery store cash register system, where each item is scanned one at a time. Although the system does not use barcode scanners, users will manually enter the item's number from their shopping cart, simulating the scanning process.
Transcribed Image Text:**Text Transcription and Explanation** **Items for Sale:** The items being sold come from a tab-delimited text file named `items.txt`, which lists products and their prices as shown below: - Eggs: $1.09 - Roast Beef: $3.49 - Ground Beef: $2.49 - Milk: $2.39 - Cereal: $4.19 - Bread: $1.99 - Apples: $0.99 - Blueberries: $1.29 - Sausage: $2.99 - Mountain Dew: $5.99 - Frozen Pizza: $6.59 - Tamales: $7.19 - String Cheese: $1.29 - Hamburgers: $12.99 - Peanut Butter: $3.99 **Current Sales:** Sales are specified in a separate tab-delimited text file called `sales.txt`. It includes discounts and promotions as follows: - Mountain Dew: $0.30 off - Cereal: Buy-one-get-one (bogo) - Bread: $0.20 off - Hamburgers: $2.00 off - Tamales: $0.80 off - Eggs: Buy-one-get-one (bogo) **Explanation of Sales Information:** - Lines ending with "bogo" indicate a buy-one-get-one-free offer for the specified item. - Lines ending with a decimal number indicate a discount of that amount on the item's price. **Program Functionality:** The program simulates a modern grocery store cash register system, where each item is scanned one at a time. Although the system does not use barcode scanners, users will manually enter the item's number from their shopping cart, simulating the scanning process.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education