Given completed classes forming a gui, Create 3 classes for a shopping cart ( I
ID: 3662533 • Letter: G
Question
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!
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 < 0) {
// disallow negative numbers
throw new NumberFormatException();
}
} catch (final NumberFormatException e) {
number = 0;
theQuantity.setText("");
}
myItems.add(new ItemOrder(theItem, number));
updateTotal();
}
/**
* 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!!!!!
Here is a screen shot of what the program could look like when the user has selected various items to order (note that the exact appearance may be platform dependent) Shopping Cart order total $137487 7 Silly Putty, $4.41 (6 for $10.04) Java Rules!' button, $0.95 (10 for $5.00) Java Rules! bumper sticker, $0.99 (20 for $8.95) Computer science pen, $2.00 Prices are expressed as real numbers and quantities are expressed as integers (i.e., you can't buy 2.345 units of something). Notice that some of the items have a discount when you buy more. For example, Silly Putty normally costs $4.41 per unit, but you can buy 6 for $10.04. These items have, in effect, two prices: a single item price and RUDIK's Cube, $9.99 a bulk item price for a bulk quantity. The bulk quantity discount is only used if the membership checkbox at the bottom of the GUI is selected. When computing the total with the bulk item discount, apply as many of the bulk quantity as you can and then use the single item price for any leftovers. For example, the user is ordering 12 buttons that cost S0.95 each but can be bought in bulk at 10 for S5.00. The first 10 are sold at that bulk price (S5.00) and the two extras are charged at164 inch Plaema HDTV, 51,207.00 the single item price ($0.95 each) for a total of $6.90. If the user were ordering 22 buttons, the total would be S11.90 (two bulk quantities plus two extras) 2 Mp3, Mp4, FM Music Player, $18.01 Arduino Uno Ultimate Starter Kit, $54.99 LEGO Mindstorms EV3, $349.95 Xbox One, $399.00 Play Station 4, $399.00 1 64 inch Plasma HDTV, $1,297.99 Clear customer has store membershi Shopping cart At the bottom of the GUI there is a checkbox for a discount for customers who have a store membership. If this box is checked, the bulk pricing discount is applied to any items with bulk pricing as described above order total 51,358.45 7 Silly Putty, $4.41 (6 for $10.04) Java Rules!' button, $0.95 (10 for $5.00) For example, if we select that checkbox, the GUI looks like this Java Rules!' bumper sticker, $0.99 (20 for $8.95) omputer science pen, $2.00 Implementation Guidelines: 1 Rubik's Cube, $9.99 2 Mp3, Mp4, FM Music Player, $18.01 Your task is to implement the three classes that are used to make this code work: rduino Uno Ultimate Starter Kit, $5499 LEGO Mindstorms EV3, $349.95 Xbox One, $399.00 PlayStation 4, $399.00 64 inch Plasma HDTV, $1,297.99 . Item ItemOrder .Shoppingcart 1 Clear customer has store membershExplanation / Answer
/** name: Griffin Howlett ** ** date: 1/23/13 **/ import java.util.*; public class ShoppingCart { //stores items a user has chosen public ShoppingCart() { orders = new ArrayList(); isDiscounted = false; } public void add (ItemOrder order) { orders.set(itemIndex(order), order); } //replaces a previous order of the same item when it exists int itemIndex (ItemOrder order) { for (int i= 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.