Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA // GUI // JAR.FILE I NEED HELP CREATING A PROGRAM BELOW. IT HAS TO BE IN A

ID: 3687776 • Letter: J

Question

JAVA // GUI // JAR.FILE

I NEED HELP CREATING A PROGRAM BELOW. IT HAS TO BE IN A GUI, AND LOAD UP IN A JAR FILE

Point of sale interfaces are designed to simplify the process of making transactions, often in a retail environment. We often see them used in restaurants where managers can input the menu and waiters can quickly enter customer orders, which are transmitted to the kitchen and recorded. Modern systems usually include a touchscreen interface, which we will simulate with a mouse-based GUI.

The program you should design and build will read a menu from a text file formatted with a menu item and a price separated by a |. To simplify your text-parsing code, we will omit the dollar sign from the price.

For example:

The program should load the file at launch and create a panel full of large buttons (ideal for a touchscreen) for each menu item. A waiter should be able to click on each menu item to add it to the current order. This should add the item to a receipt panel which displays the full order and the total cost. The total cost should be accurate at all times, updated as each item is added (not only when the order is finalized).

The system only takes credit card as payment type however it can handle/validate multiple types of credit cards. (Please see credit card section below).

The waiter should be able to click a “Place Order” button that simulates transmitting the order to the kitchen by printing the order to System.out (in addition to showing the confirmation on screen). You should also include a “Clear” button that will clear the current order (used when a waiter makes a mistake and needs to start over).

Explanation / Answer

import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.BorderFactory; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * This class represents the graphical user interface (GUI) for the shopping * program, allowing the user to select items to purchase in his/her shopping cart. * @author Marty Stepp * @version Tue Mar 29 2011 */ public class ShoppingGui { private JFrame frame; private JLabel total; private JCheckBox discount; private ShoppingCart cart; /** * Constructs a new GUI to display items from the given catalog. * @param catalog The store catalog to use * @pre catalog != null */ public ShoppingGui(Catalog catalog) { cart = new ShoppingCart(); createComponents(); setupEvents(); performLayout(catalog); frame.setVisible(true); } // Constructs all of the graphical components to reside in the window frame private void createComponents() { // label to display the ORDER TOTAL total = new JLabel(); total.setHorizontalAlignment(SwingConstants.CENTER); updateTotal(); // checkbox for applying a bulk discount to the entire shopping cart discount = new JCheckBox(); discount.setText(ShoppingCart.getDiscountPercentage() + "% off when you buy " + ShoppingCart.getDiscountQuantity() + " or more items"); discount.setSelected(cart.hasDiscount()); discount.setHorizontalAlignment(SwingConstants.CENTER); // window frame for the overall display frame = new JFrame("CSE 331 Shopping"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // Sets up event handlers on all relevant components in the window. private void setupEvents() { // when the user checks/unchecks the Discount box, inform the shopping cart discount.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { cart.setDiscount(discount.isSelected()); updateTotal(); } }); } // Constructs containers to do the layout and positioning of the components // in the window. Also creates the display components for the catalog items. private void performLayout(Catalog catalog) { // create a display panel to show each item in the store JPanel center = new JPanel(new GridLayout(0, 1)); center.setBorder(BorderFactory.createTitledBorder(catalog.getStoreName())); for (Item item : catalog) { center.add(new ItemPanel(item)); } // south panel stores the ORDER TOTAL label and discount checkbox JPanel south = new JPanel(new GridLayout(0, 1)); south.add(total); south.add(discount); // frame's content pane stores overall layout for the window Container contentPane = frame.getContentPane(); contentPane.add(center, BorderLayout.CENTER); contentPane.add(south, BorderLayout.SOUTH); frame.pack(); center(frame); } // Updates the ORDER TOTAL label's text when a purchase in the window changes. private void updateTotal() { total.setText(String.format("$%.2f ORDER TOTAL", cart.getTotal())); } // Positions the given window in the center of the screen. private static void center(Window window) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation((screen.width - window.getWidth()) / 2, (screen.height - window.getHeight()) / 2); } /** * Represents one panel container to hold the information about a single item * available for purchase in the store, including the graphical components * for that item, such as its text box to type in how many to buy, and the * label to show the item's description. */ private class ItemPanel extends JPanel { private static final long serialVersionUID = 0; private Item item; private JTextField textField; private JLabel label; public ItemPanel(Item item) { this.item = item; setLayout(new FlowLayout(FlowLayout.LEFT)); textField = new JTextField(3); textField.setHorizontalAlignment(SwingConstants.CENTER); label = new JLabel(item.toString()); add(textField); add(label); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { update(); } }); textField.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent event) { update(); } }); } // Called when the text in this item's text field changes. // Informs the shopping cart of the update to the item's purchase quantity. private void update() { String text = textField.getText().replaceAll("[^0-9-]+", ""); textField.setText(text); int quantity = 0; if (!text.isEmpty()) { try { quantity = Integer.parseInt(text); } catch (NumberFormatException nfe) { System.err.println(nfe); } } Purchase purchase = new Purchase(item, quantity); cart.add(purchase); updateTotal(); if (textField.hasFocus()) { textField.transferFocus(); } } } }