Question
The file menu.txt contains a menu of food items and prices in the format: price item 1.25 hot dog ... 10.00 pizza The class MenuItem
The file menu.txt contains a menu of food items and prices in the format:
price item
1.25 hot dog
...
10.00 pizza
The class MenuItem represents a single item in this file. It does not read from the file (that is handled by the Menu class, but it stores the name of the item and its price as a BigDecimal object. We use BigDecimal rather than float or double for currency calculations in order to avoid round-off errors. See:
Why You Should Never Use Float and Double for Monetary Calculations Salami slicing...delicious and malicious
You are given two constructors for MenuItem, one of which creates a BigDecimal appropriate for currency use. Complete the rest of the methods. The toString method must return a string in the form:
- item $price
such as:
- hot dog $ 1.25
The class Menu stores a List of MenuItems. Its constructor reads from a file like menu.txt. (Its main method is a simple test method that reads from menu.txt by default, and prints the contents of its List.)
The Menu toString method must return a string in the form:
- number) item $price
such as:
- ... 5) poutine $ 3.75
- 6) pizza $10.00 ...
where number is the index of the MenuItem in the Menu List.
The class Order represents a food order made from the Foodorama menu. Order stores a HashMap of MenuItems. For each entry in the HashMap, a MenuItem is the key, and the quantity of MenuItems ordered is the associated value. A HashMap allows only one occurrence of a key. Only items that have a quantity greater than zero should be stored in the HashMap.
The Order toString method must return a string in the form:
- drink 4 @ $ 1.50 = $ 6.00
- hot dog 3 @ $ 1.25 = $ 3.75
- pizza 2 @ $10.00 = $ 20.00
- Subtotal: $ 29.75
- Taxes: $ 3.87
- Total: $ 33.62
The order of the menu items is not significant. The alignment of the values is significant, and you may assume that no value is larger than $999.99.
Complete the rest of the public methods of the Order class.
The class Cashier generates an Order. Its takeOrder method accepts user input from the keyboard to generate the Order. This is a sample run (the user inputs are underlined ):
- Welcome to WLU Foodorama! Menu:
- 1) hot dog $ 1.25
- 2) hamburger $ 2.00
- 3) cheeseburger $ 2.75
- 4) fries $ 1.75
- 5) poutine $ 3.75
- 6) pizza $10.00
- 7) drink $ 1.50
- Press 0 when done.
- Press any other key to see the menu again.
- Command: 1
- How many do you want? 2
- Command: 3
- How many do you want? 4
- Command: 1
- How many do you want? 3
- Command: 6
- How many do you want? what?
- Not a valid number Command: bad entry!
- Not a valid number
- Menu:
- 1) hot dog $ 1.25
- 2) hamburger $ 2.00
- 3) cheeseburger $ 2.75
- 4) fries $ 1.75
- 5) poutine $ 3.75
- 6) pizza $10.00
- 7) drink $ 1.50
- Press 0 when done.
- Press any other key to see the menu again.
- Command: 9 Menu:
- 1) hot dog $ 1.25
- 2) hamburger $ 2.00
- 3) cheeseburger $ 2.75
- 4) fries $ 1.75
- 5) poutine $ 3.75
- 6) pizza $10.00
- 7) drink $ 1.50
- Press 0 when done.
- Press any other key to see the menu again.
- Command: 0
- ----------------------------------------
- Receipt
- hot dog 5 @ $ 1.25 = $ 6.25
- cheeseburger 4 @ $ 2.75 = $ 11.00
- Subtotal: $ 17.25
- Taxes: $ 2.24
- Total: $ 19.49
The interface must meet the following requirements:
- The menu is displayed at the start of the input, and after that only when requested.
- Inputs end with the Enter key.
- Entering 0 as a command ends the order.
- Entering a valid item number causes the Cashier to ask for the quantity of that item.
- Entering anything else as a command causes the menu to reprint.
- Items may be requested many times, and their quantities are incremented with each request - i.e. they should appear only once in the receipt.
- Only quantities greater than 0 are recorded. Any other value entered for a quantity is ignored.
- Print Not a valid number on any bad command or quantity input.
- Print a receipt when the order ends.
- An order may be empty.
------------------------------------------------------------------------------------------------------------------------
A05Main.java
package cp213;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* Class testing.
*
* @author your name here
* @author Abdul-Rahman Mawlood-Yunis
* @author David Brown
* @version 2022-11-20
*/
public class A05Main {
// Constants
private static final String LINE = "-".repeat(40);
private static final String TEST_LINE = "=".repeat(80);
/**
* For testing. Reads contents of "menu.txt" from root of project.
*
* @param args Unused.
*/
public static void main(String[] args) {
System.out.println("Assignment 5 Class Tests");
testMenuItem();
testMenu();
}
/**
* Simple MenuItem tests.
*/
public static void testMenuItem() {
System.out.println(TEST_LINE);
System.out.println("Testing MenuItem");
System.out.println(LINE);
System.out.println("Test double Constructor:");
String item = "hot dog";
double doublePrice = 1.25;
MenuItem menuItem = new MenuItem(item, doublePrice);
System.out.println(String.format("menuItem = new MenuItem(\"%s\", %.2f);", item, doublePrice));
System.out.println(LINE);
String name = menuItem.getName();
System.out.println(String.format("menuItem.getName(): {\"hot dog\"}: \"%s\"", name));
System.out.println(LINE);
BigDecimal price = menuItem.getPrice();
System.out.println(String.format("menuItem.getPrice(): {1.25}: %s", price));
System.out.println(LINE);
String string = menuItem.toString();
System.out.println(String.format("menuItem.toString(): {\"hot dog $ 1.25\"}: \"%s\"", string));
System.out.println(LINE);
System.out.println("Test BigDecimal Constructor:");
BigDecimal bigPrice = new BigDecimal(doublePrice);
menuItem = new MenuItem(item, bigPrice);
System.out.println(String.format("menuItem = new MenuItem(\"%s\", %s);", item, bigPrice));
System.out.println(LINE);
name = menuItem.getName();
System.out.println(String.format("menuItem.getName(): {\"hot dog\"}: \"%s\"", name));
System.out.println(LINE);
price = menuItem.getPrice();
System.out.println(String.format("menuItem.getPrice(): {1.25}: %s", price));
System.out.println(LINE);
string = menuItem.toString();
System.out.println(String.format("menuItem.toString(): {\"hot dog $ 1.25\"}: \"%s\"", string));
System.out.println();
}
/**
* Simple Menu tests.
*/
public static void testMenu() {
System.out.println(TEST_LINE);
System.out.println("Testing Menu");
System.out.println(LINE);
Menu menu = null;
String filename = "menu.txt";
try {
Scanner fileScanner = new Scanner(new File(filename));
menu = new Menu(fileScanner);
System.out.println("Menu menu = new Menu(fileScanner);");
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println("Cannot open menu file");
}
System.out.println(LINE);
int size = menu.size();
System.out.println(String.format("menu.size(): {7}: %d", size));
System.out.println(LINE);
MenuItem item = menu.getItem(3);
System.out.println(String.format("menu.getItem(3): {\"fries $ 1.75\"}: \"%s\"", item));
System.out.println(LINE);
System.out.println("menu.toString():");
System.out.println(menu.toString());
System.out.println(LINE);
MenuItem[] itemsArray = { new MenuItem("hot dog", 1.25), new MenuItem("fries", 1.75) };
System.out.println("List of items:");
System.out.println(Arrays.toString(itemsArray));
List
Menu menuFromList = new Menu(items);
System.out.println("Menu menuFromList = new Menu(items);");
System.out.println("menuFromList.toString():");
System.out.println(menuFromList.toString());
}
/**
* Simple Menu tests.
*/
public static void testOrder() {
System.out.println(TEST_LINE);
System.out.println("Testing Menu");
System.out.println(LINE);
Order order = new Order();
String item = "hot dog";
double doublePrice = 1.25;
MenuItem menuItem = new MenuItem(item, doublePrice);
order.add(menuItem, 1);
}
}
Cashier.java
package cp213;
/**
* Wraps around an Order object to ask for MenuItems and quantities.
*
* @author your name here
* @author Abdul-Rahman Mawlood-Yunis
* @author David Brown
* @version 2022-11-20
*/
public class Cashier {
// Attributes
private Menu menu = null;
/**
* Constructor.
*
* @param menu A Menu.
*/
public Cashier(Menu menu) {
this.menu = menu;
}
/**
* Prints the menu.
*/
private void printCommands() {
System.out.println(" Menu:");
System.out.println(menu.toString());
System.out.println("Press 0 when done.");
System.out.println("Press any other key to see the menu again. ");
}
/**
* Asks for commands and quantities. Prints a receipt when all orders have been
* placed.
*
* @return the completed Order.
*/
public Order takeOrder() {
System.out.println("Welcome to WLU Foodorama!");
// your code here
return null;
}
}
Menu.java
package cp213;
import java.util.Collection;
import java.util.Scanner;
/**
* Stores a List of MenuItems and provides a method return these items in a
* formatted String. May be constructed from an existing List or from a file
* with lines in the format:
*
*
1.25 hot dog
10.00 pizza
...
*
*
* @author your name here
* @author Abdul-Rahman Mawlood-Yunis
* @author David Brown
* @version 2022-11-20
*/
public class Menu {
// Attributes.
// your code here
/**
* Creates a new Menu from an existing Collection of MenuItems. MenuItems are
* copied into the Menu List.
*
* @param items an existing Collection of MenuItems.
*/
public Menu(Collection
// your code here
}
/**
* Constructor from a Scanner of MenuItem strings. Each line in the Scanner
* corresponds to a MenuItem. You have to read the Scanner line by line and add
* each MenuItem to the List of items.
*
* @param fileScanner A Scanner accessing MenuItem String data.
*/
public Menu(Scanner fileScanner) {
// your code here
}
/**
* Returns the List's i-th MenuItem.
*
* @param i Index of a MenuItem.
* @return the MenuItem at index i
*/
public MenuItem getItem(int i) {
// your code here
return null;
}
/**
* Returns the number of MenuItems in the items List.
*
* @return Size of the items List.
*/
public int size() {
// your code here
return 0;
}
/**
* Returns the Menu items as a String in the format:
*
*
5) poutine $ 3.75
6) pizza $10.00
*
*
* where n) is the index + 1 of the MenuItems in the List.
*/
@Override
public String toString() {
// your code here
return null;
}
}
MenuItem.java (complete)
package cp213;
import java.math.BigDecimal; import java.math.RoundingMode;
/** * Defines the name and price of a menu item. Price is stored as a BigDecimal to * avoid rounding errors. * * @author your name here * @author Abdul-Rahman Mawlood-Yunis * @author David Brown * @version 2022-11-20 */ public class MenuItem {
// Attributes private static final String itemFormat = "%-12s $%5.2f"; private String name = null; private BigDecimal price = null;
/** * Constructor. Must set price to 2 decimal points for calculations. * * @param name Name of the menu item. * @param price Price of the menu item. */ public MenuItem(final String name, final BigDecimal price) {
this.name = name; this.price = price;
this.price.setScale(2, RoundingMode.HALF_EVEN);
}
/** * Alternate constructor. Converts a double price to BigDecimal. * * @param name Name of the menu item. * @param price Price of the menu item. */ public MenuItem(final String name, final double price) {
this(name, new BigDecimal(price));
}
/** * name getter * * @return Name of the menu item. */ public String getName() { return this.name; }
/** * price getter * * @return Price of the menu item. */ public BigDecimal getPrice() { return this.price; }
/** * Returns a MenuItem as a String in the format: * *
hot dog $ 1.25 pizza $10.00 **/ @Override public String toString() {
return String.format(itemFormat, this.name, this.price); } }
Order.java
package cp213;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.math.BigDecimal;
/**
* Stores a HashMap of MenuItem objects and the quantity of each MenuItem
* ordered. Each MenuItem may appear only once in the HashMap.
*
* @author your name here
* @author Abdul-Rahman Mawlood-Yunis
* @author David Brown
* @version 2022-11-20
*/
public class Order implements Printable {
/**
* The current tax rate on menu items.
*/
public static final BigDecimal TAX_RATE = new BigDecimal(0.13);
// Attributes
// your code here
/**
* Increments the quantity of a particular MenuItem in an Order with a new
* quantity. If the MenuItem is not in the order, it is added.
*
* @param item The MenuItem to purchase - the HashMap key.
* @param quantity The number of the MenuItem to purchase - the HashMap value.
*/
public void add(final MenuItem item, final int quantity) {
// your code here
}
/**
* Calculates the total value of all MenuItems and their quantities in the
* HashMap.
*
* @return the total price for the MenuItems ordered.
*/
public BigDecimal getSubTotal() {
// your code here
return null;
}
/**
* Calculates and returns the total taxes to apply to the subtotal of all
* MenuItems in the order. Tax rate is TAX_RATE.
*
* @return total taxes on all MenuItems
*/
public BigDecimal getTaxes() {
// your code here
return null;
}
/**
* Calculates and returns the total price of all MenuItems order, including tax.
*
* @return total price
*/
public BigDecimal getTotal() {
// your code here
return null;
}
/*
* Implements the Printable interface print method. Prints lines to a Graphics2D
* object using the drawString method. Prints the current contents of the Order.
*/
@Override
public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
throws PrinterException {
int result = PAGE_EXISTS;
if (pageIndex == 0) {
final Graphics2D g2d = (Graphics2D) graphics;
g2d.setFont(new Font("MONOSPACED", Font.PLAIN, 12));
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// Now we perform our rendering
final String[] lines = this.toString().split(" ");
int y = 100;
final int inc = 12;
for (final String line : lines) {
g2d.drawString(line, 100, y);
y += inc;
}
} else {
result = NO_SUCH_PAGE;
}
return result;
}
/**
* Returns a String version of a receipt for all the MenuItems in the order.
*/
@Override
public String toString() {
// your code here
return null;
}
/**
* Replaces the quantity of a particular MenuItem in an Order with a new
* quantity. If the MenuItem is not in the order, it is added. If quantity is 0
* or negative, the MenuItem is removed from the Order.
*
* @param item The MenuItem to update
* @param quantity The quantity to apply to item
*/
public void update(final MenuItem item, final int quantity) {
// your code here
}
}
Orderpanel.java
package cp213;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* The GUI for the Order class.
*
* @author your name here
* @author Abdul-Rahman Mawlood-Yunis
* @author David Brown
* @version 2022-11-20
*/
@SuppressWarnings("serial")
public class OrderPanel extends JPanel {
// Attributes
private Menu menu = null; // Menu attached to panel.
private final Order order = new Order(); // Order to be printed by panel.
// GUI Widgets
private final JButton printButton = new JButton("Print");
private final JLabel subtotalLabel = new JLabel("0");
private final JLabel taxLabel = new JLabel("0");
private final JLabel totalLabel = new JLabel("0");
private JLabel nameLabels[] = null;
private JLabel priceLabels[] = null;
// TextFields for menu item quantities.
private JTextField quantityFields[] = null;
/**
* Displays the menu in a GUI.
*
* @param menu The menu to display.
*/
public OrderPanel(final Menu menu) {
this.menu = menu;
this.nameLabels = new JLabel[this.menu.size()];
this.priceLabels = new JLabel[this.menu.size()];
this.quantityFields = new JTextField[this.menu.size()];
this.layoutView();
this.registerListeners();
}
/**
* Implements an ActionListener for the 'Print' button. Prints the current
* contents of the Order to a system printer or PDF.
*/
private class PrintListener implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
// your code here
}
}
/**
* Implements a FocusListener on a quantityField. Requires appropriate
* FocusListener methods.
*/
private class QuantityListener implements FocusListener {
// your code here
}
/**
* Layout the panel.
*/
private void layoutView() {
// your code here
}
/**
* Register the widget listeners.
*/
private void registerListeners() {
// Register the PrinterListener with the print button.
this.printButton.addActionListener(new PrintListener());
// your code here
}
}
PLEASE HELP ME COMPLETE ALL THE BOLD PARTS. THANK YOU!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started