
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!");
}
}



Step by stepSolved in 2 steps with 1 images

- A for construct is a loop that processes a list of items. As a consequence, it continues to run as long as there are items to process. Is this statement true or false?arrow_forwardC#Write a method MyMethod which takes an int parameter named x. This method calculates the sum of integers from 1 to x and returns the sum. Use a for loop.arrow_forwardUsing a Sentinel Value to Control a while Loop in Java Summary In this lab, you write a while loop that uses a sentinel value to control a loop in a Java program that has been provided. You also write the statements that make up the body of the loop. The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guide’s featured movie of the week. The program executes continuously until the theater manager enters a negative number to quit. At the end of the program, you should display the average star rating for the movie. Instructions Ensure the file named MovieGuide.java is open. Write the while loop using a sentinel value to control the loop, and write the statements that make up the body of the loop to calculate the average star rating. Execute the program by clicking Run. Input the following: 0, 3, 4, 4, 1, 1, 2, -1 Check that the…arrow_forward
- Knight's Tour: The Knight's Tour is a mathematical problem involving a knight on a chessboard. The knight is placed on the empty board and, moving according to the rules of chess, must visit each square exactly once. There are several billion solutions to the problem, of which about 122,000,000 have the knight finishing on the same square on which it begins. When this occurs the tour is said to be closed. Your assignment is to write a program that gives a solution to the Knight's Tour problem recursively. You must hand in a solution in C++ AND Java. The name of the C++ file should be "main.cc" and the name of the Java file should be "Main.java". Write C++ only with a file name of main.cc Please run in IDE and check to ensure that there are no errors occuring Output should look similar to: 1 34 3 18 49 32 13 16 4 19 56 33 14 17 50 31 57 2 35 48 55 52 15 12 20 5 60 53 36 47 30 51 41 58 37 46 61 54 11 26 6 21 42 59 38 27 64 29 43 40 23 8 45 62 25 10 22 7 44 39 24 9 28 63arrow_forwardPrimeAA.java Write a program that will tell a user if their number is prime or not. Your code will need to run in a loop (possibly many loops) so that the user can continue to check numbers. A prime is a number that is only divisible by itself and the number 1. This means your code should loop through each value between 1 and the number entered to see if it’s a divisor. If you only check for a small handful of numbers (such as 2, 3, and 5), you will lose most of the credit for this project. Include a try/catch to catch input mismatches and include a custom exception to catch negative values. If the user enters 0, the program should end. Not only will you tell the user if their number is prime or not, you must also print the divisors to the screen (if they exist) on the same line as shown below AND give a count of how many divisors there are. See examples below. Your program should run the test case exactly as it appears below, and should work on any other case in general. Output…arrow_forwardComputer Science Part C: Interactive Driver Program Write an interactive driver program that creates a Course object (you can decide the name and roster/waitlist sizes). Then, use a loop to interactively allow the user to add students, drop students, or view the course. Display the result (success/failure) of each add/drop.arrow_forward
- IN JS PROGRAMMING LANGUAGE Your local bank has decided to upgrade its ATM machines by incorporating motion sensor technology. The machines now interpret a series of consecutive dance moves in place of a PIN number. Create a program that converts a customer's PIN number to its dance equivalent. There is one dance move per digit in the PIN number. A list of dance moves is given in the code. Examples danceConvert("0000") danceConvert("3856") ["Shimmy", "Shake", "Pirouette", "Slide"] → ["Slide", "Arabesque", "Pop", "Arabesque" ] -arrow_forwarduse the JCreator program for code in picture and show the outputarrow_forwardInteger userValue is read from input. Assume userValue is greater than 1000 and less than 99999. Assign tensDigit with userValue's tens place value. Ex: If the input is 15876, then the output is: The value in the tens place is: 7 2 3 public class ValueFinder { 4 5 6 7 8 9 10 11 12 13 GHE 14 15 16} public static void main(String[] args) { new Scanner(System.in); } Scanner scnr int userValue; int tensDigit; int tempVal; userValue = scnr.nextInt(); Your code goes here */ 11 System.out.println("The value in the tens place is: + tensDigit);arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





