Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given completed classes forming a gui, Create 3 classes for a shopping cart ( I have done below). Then fill in the missing methods IN

Given completed classes forming a gui, Create 3 classes for a shopping cart ( I have done below). Then fill in the missing methods IN THE CLASSES BELOW!!! I have already created the methods. more instructions found in the images below. Thanks! be sure to follow checkstyle requirements! FINISH THE Item.Java, ItemOrder.java and ShoppingCart.java classes below. Do NOT change any code in the other 3 classes (GUI). PERFECT ANSWERS GET A HIGH RATING!

image text in transcribedimage text in transcribedimage text in transcribed

Classes GIVEN : Note : CLASSES are SEPERATED BY Dashes (------------------------------------------------------------------------------)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; import java.util.Scanner;

import model.Item;

/** * A utility class for The shopping cart application. * */ public final class FileLoader { /** * A private constructor, to prevent external instantiation. */ private FileLoader() { } /** * Reads item information from a file and returns a List of Item objects. * @param theFile the name of the file to load into a List of Items * @return a List of Item objects created from data in an input file */ public static List readItemsFromFile(final String theFile) { final List items = new LinkedList(); try (Scanner input = new Scanner(Paths.get(theFile))) { // Java 7! while (input.hasNextLine()) { final Scanner line = new Scanner(input.nextLine()); line.useDelimiter(";"); final String itemName = line.next(); final BigDecimal itemPrice = line.nextBigDecimal(); if (line.hasNext()) { final int bulkQuantity = line.nextInt(); final BigDecimal bulkPrice = line.nextBigDecimal(); items.add(new Item(itemName, itemPrice, bulkQuantity, bulkPrice)); } else { items.add(new Item(itemName, itemPrice)); } line.close(); } } catch (final IOException e) { e.printStackTrace(); } // no finally block needed to close 'input' with the Java 7 try with resource block return items; } /** * Reads item information from a file and returns a List of Item objects. * @param theFile the name of the file to load into a List of Items * @return a List of Item objects created from data in an input file */ public static List readConfigurationFromFile(final String theFile) { final List results = new LinkedList(); try (Scanner input = new Scanner(Paths.get(theFile))) { // Java 7! while (input.hasNextLine()) { final String line = input.nextLine(); //if (!line.startsWith("#")) { if (!(line.charAt(0) == '#')) { results.add(line); } } } catch (final IOException e) { e.printStackTrace(); } // no finally block needed to close 'input' with the Java 7 try with resource block return results; } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; //import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; //import java.awt.image.BufferedImage; import java.text.NumberFormat; import java.util.LinkedList; import java.util.List; import java.util.Map;

//import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; //import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.SwingConstants;

import model.Item; import model.ItemOrder; import model.ShoppingCart;

/** * ShoppingFrame provides the user interface for a shopping cart program. * */ public final class ShoppingFrame extends JFrame { /** * The Serialization ID. */ private static final long serialVersionUID = 0; // constants to capture screen dimensions /** A ToolKit. */ private static final Toolkit KIT = Toolkit.getDefaultToolkit(); /** The Dimension of the screen. */ private static final Dimension SCREEN_SIZE = KIT.getScreenSize();

/** * The width of the text field in the GUI. */ private static final int TEXT_FIELD_WIDTH = 12; /** * The color for some elements in the GUI. */ private static final Color COLOR_1 = new Color(199, 153, 0); // UW Gold

/** * The color for some elements in the GUI. */ private static final Color COLOR_2 = new Color(57, 39, 91); // UW Purple

/** * The shopping cart used by this GUI. */ private final ShoppingCart myItems; /** * The map that stores each campus name and the campus's bookstore inventory. */ private final Map> myCampusInventories;

/** * The map that stores each campus name and the campus's bookstore inventory. */ private final String myCurrentCampus; /** * The text field used to display the total amount owed by the customer. */ private final JTextField myTotal; /** * The panel that holds the item descriptions. Needed to add and remove on * the fly when the radio buttons change. */ // private JPanel myItemsPanel;

/** * A List of the item text fields. */ private final List myQuantities; /** * Initializes the shopping cart GUI. * * @param theCampusInventories The list of items. * @param theCurrentCampus The campus that is originally selected when * the application starts. */ public ShoppingFrame(final Map> theCampusInventories, final String theCurrentCampus) { // create frame and order list super(); // No title on the JFrame. We can set this later. myItems = new ShoppingCart();

// set up text field with order total myTotal = new JTextField("$0.00", TEXT_FIELD_WIDTH); myQuantities = new LinkedList(); myCampusInventories = theCampusInventories; myCurrentCampus = theCurrentCampus; setupGUI(); }

/** * Setup the various parts of the GUI. * */ private void setupGUI() { // hide the default JFrame icon //final Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE); // replace the default JFrame icon final ImageIcon img = new ImageIcon("files/w.gif"); setIconImage(img.getImage()); setTitle("Shopping Cart"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // the following was used in autumn 2015, not in winter 2016 add(makeTotalPanel(), BorderLayout.NORTH); final JPanel itemsPanel = makeItemsPanel(myCampusInventories.get(myCurrentCampus)); add(itemsPanel, BorderLayout.CENTER); add(makeCheckBoxPanel(), BorderLayout.SOUTH);

// adjust size to just fit pack(); // make the GUI so that it cannot be resized by the user dragging a corner setResizable(false); // position the frame in the center of the screen setLocation(SCREEN_SIZE.width / 2 - getWidth() / 2, SCREEN_SIZE.height / 2 - getHeight() / 2); setVisible(true); } // /** // * Creates the panel to hold the campus location radio buttons. // * // * @return The created JPanel // */ // private JPanel makeCampusPanel() { // final JPanel p = new JPanel(); // p.setBackground(COLOR_2); // // final ButtonGroup g = new ButtonGroup(); // for (final Object campus : myCampusInventories.keySet()) { // final JRadioButton rb = new JRadioButton(campus.toString()); // rb.setForeground(Color.WHITE); // rb.setBackground(COLOR_2); // rb.setSelected(campus.equals(myCurrentCampus)); // g.add(rb); // p.add(rb); // // //Java 8 use of Lambda Expression instead // // of an anonymous inner ActionListener object // rb.addActionListener(ae -> { // myCurrentCampus = rb.getText(); // // //remove the old panel and add the new one // remove(myItemsPanel); // myItemsPanel = makeItemsPanel(myCampusInventories.get(myCurrentCampus)); // add(myItemsPanel, BorderLayout.CENTER); // // //clear previous data from the ShppingCart and // //update the total in the GUI // myItems.clear(); // updateTotal(); // // //redraw the UI with the new panel // pack(); // revalidate(); // } // ); // } // return p; // }

/** * Creates a panel to hold the total. * * @return The created panel */ private JPanel makeTotalPanel() { // tweak the text field so that users can't edit it, and set // its color appropriately

myTotal.setEditable(false); myTotal.setEnabled(false); myTotal.setDisabledTextColor(Color.BLACK);

// create the panel, and its label

final JPanel totalPanel = new JPanel(); totalPanel.setBackground(COLOR_2); final JLabel l = new JLabel("order total"); l.setForeground(Color.WHITE); totalPanel.add(l); totalPanel.add(myTotal); final JPanel p = new JPanel(new BorderLayout()); //p.add(makeCampusPanel(), BorderLayout.NORTH); p.add(totalPanel, BorderLayout.CENTER); return p; }

/** * Creates a panel to hold the specified list of items. * * @param theItems The items * @return The created panel */ private JPanel makeItemsPanel(final List theItems) { final JPanel p = new JPanel(new GridLayout(theItems.size(), 1)); for (final Item item : theItems) { addItem(item, p); }

return p; }

/** * Creates and returns the checkbox panel. * * @return the checkbox panel */ private JPanel makeCheckBoxPanel() { final JPanel p = new JPanel(); p.setBackground(COLOR_2); final JButton clearButton = new JButton("Clear"); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent theEvent) { myItems.clear(); for (final JTextField field : myQuantities) { field.setText(""); } updateTotal(); } }); p.add(clearButton); final JCheckBox cb = new JCheckBox("customer has store membership"); cb.setForeground(Color.WHITE); cb.setBackground(COLOR_2); cb.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent theEvent) { myItems.setMembership(cb.isSelected()); updateTotal(); } }); p.add(cb); return p; }

/** * Adds the specified product to the specified panel. * * @param theItem The product to add. * @param thePanel The panel to add the product to. */ private void addItem(final Item theItem, final JPanel thePanel) { final JPanel sub = new JPanel(new FlowLayout(FlowLayout.LEFT)); sub.setBackground(COLOR_1); final JTextField quantity = new JTextField(3); myQuantities.add(quantity); quantity.setHorizontalAlignment(SwingConstants.CENTER); quantity.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent theEvent) { quantity.transferFocus(); } }); quantity.addFocusListener(new FocusAdapter() { @Override public void focusLost(final FocusEvent theEvent) { updateItem(theItem, quantity); } }); sub.add(quantity); final JLabel l = new JLabel(theItem.toString()); l.setForeground(COLOR_2); sub.add(l); thePanel.add(sub); }

/** * Updates the set of items by changing the quantity of the specified * product to the specified quantity. * * @param theItem The product to update. * @param theQuantity The new quantity. */ private void updateItem(final Item theItem, final JTextField theQuantity) { final String text = theQuantity.getText().trim(); int number; try { number = Integer.parseInt(text); if (number

/** * Updates the total displayed in the window. */ private void updateTotal() { final double total = myItems.calculateTotal().doubleValue(); myTotal.setText(NumberFormat.getCurrencyInstance().format(total)); } }

// end of class ShoppingFrame

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.EventQueue; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map;

import model.Item; import utility.FileLoader;

/** * ShoppingMain provides the main method for a simple shopping cart GUI * displayer and calculator. */

public final class ShoppingMain { /** * The filename of the file containing the items to display in the cart. */ private static final String CONFIG_FILE = "config.txt"; /** * The local path of the configuration files. */ private static final String FILE_LOCATION = "files/"; /** * The map that stores each campus name and the campus's bookstore invintory. */ private static final Map> CAMPUS_INVINTORIES = new HashMap();

/** * A private constructor, to prevent external instantiation. */ private ShoppingMain() { }

/** * The main() method - displays and runs the shopping cart GUI. * * @param theArgs Command line arguments, ignored by this program. */ public static void main(final String... theArgs) { final List campusNames = FileLoader.readConfigurationFromFile(FILE_LOCATION + CONFIG_FILE); String mainCampus = null; for (final String campusName : campusNames) { //used to remove a warning and allow campusName to be final String alteredName = campusName; //as defined in config.txt the campus with a * should be the "main" campus if (campusName.charAt(0) == '*') { //remove the * alteredName = campusName.substring(1); mainCampus = alteredName; } CAMPUS_INVINTORIES.put( alteredName, FileLoader.readItemsFromFile(FILE_LOCATION + alteredName.toLowerCase(Locale.ENGLISH) + ".txt")); } final String currentStore = mainCampus; EventQueue.invokeLater(new Runnable() { @Override public void run() { new ShoppingFrame(CAMPUS_INVINTORIES, currentStore); } }); } // end main()

} // end class ShoppingMain

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

NOW THESE ARE THE CLASSES I CREATED WITH THE CORRECT METHODS. YOU CANNOT CHANGE ANY OF THE METHOD NAMES I HAVE SET BECAUSE THEY ARE ALL CORRECT TO THE SPEC OF THE PROGRAM. THANKS!

MY CLASSES: NOTE : seperated by dashes ( -------------------------------------------------------------------)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.math.BigDecimal;

public final class Item { public Item(final String theName, final BigDecimal thePrice) {

}

public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity, final BigDecimal theBulkPrice) {

}

public BigDecimal getPrice() { return null; }

public int getBulkQuantity() { return 0; }

public BigDecimal getBulkPrice() { return null; }

public boolean isBulk() { return false; }

@Override public String toString() { return null; }

@Override public boolean equals(final Object theOther) { return false; }

@Override public int hashCode() { return 0; }

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public final class ItemOrder {

public ItemOrder(final Item theItem, final int theQuantity) {

}

public Item getItem() { return null; } public int getQuantity() { return 0; }

@Override public String toString() {

return null; }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.math.BigDecimal;

public class ShoppingCart {

public ShoppingCart() {

}

public void add(final ItemOrder theOrder) { }

public void setMembership(final boolean theMembership) { }

public BigDecimal calculateTotal() {

return null; } public void clear() { }

@Override public String toString() { return null; }

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PLEASE FINISH CLASSES : Item.java, ItemOrder.java and shoppingCart.java ( Last three classes posted above) Thanks! NOTE : in the GUI the "discount" button should give the appropriate discount!!!!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago