Question: These are my four classes, I need help with the shoppingCart class that extends ArrayList to get it to work. I need to have a
These are my four classes, I need help with the shoppingCart class that extends ArrayList
/** * @author Reges and Stepp, Building Java Programs * modified by W.P. Iverson, Bellevue College, * January 2017for CS211 class */ // Stuart Reges // 3/28/07 // // Class ShoppingFrame provides the user interface for a simple shopping // program, starting with Building Java Programs, chapter 10, project 1. //
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*;
@SuppressWarnings("serial") public class ShoppingFrame extends JFrame { private ShoppingCart selections; private JTextField total;
public ShoppingFrame(ArrayList
p = new JPanel(new GridLayout(products.size(), 1)); removeDuplicates(products); for (Sku i : products) { addItem(i, p); // add selections to panel } add(p, BorderLayout.CENTER); // add panel to frame
p = new JPanel(); add(makeCheckBoxPanel(), BorderLayout.SOUTH);
// adjust size to just fit pack(); }
// Should probably use Set rather than Array List, but this is Chapter 10 private void removeDuplicates(ArrayList
// Sets up the "discount" checkbox for the frame private JPanel makeCheckBoxPanel() { JPanel p = new JPanel(); p.setBackground(Color.blue); final JCheckBox cb = new JCheckBox("qualify for discount"); p.add(cb); cb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { selections.setDiscount(cb.isSelected()); updateTotal(); } }); return p; }
// adds a product to the panel, including a textfield for user input of // the quantity private void addItem(final Sku product, JPanel p) { JPanel sub = new JPanel(new FlowLayout(FlowLayout.LEFT)); sub.setBackground(new Color(0, 180, 0)); final JTextField quantity = new JTextField(3); quantity.setHorizontalAlignment(SwingConstants.CENTER); quantity.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updateItem(product, quantity); quantity.transferFocus(); } }); quantity.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { updateItem(product, quantity); } }); sub.add(quantity); JLabel l = new JLabel("" + product); l.setForeground(Color.white); sub.add(l); p.add(sub); }
// When the user types a new value into one of the quantity fields, // parse the input and update the ShoppingCart. Display an error // message if text is not a number or is negative. private void updateItem(Sku product, JTextField quantity) { int number; String text = quantity.getText().trim(); try { number = Integer.parseInt(text); } catch (NumberFormatException error) { number = 0; } if (number <= 0 && text.length() > 0) { Toolkit.getDefaultToolkit().beep(); quantity.setText(""); number = 0; } selections.add(new NumSelected(product, number)); updateTotal(); }
// reset the text field for order total private void updateTotal() { double amount = selections.getTotal(); total.setText(NumberFormat.getCurrencyInstance().format(amount)); } // Below used to be separate ShoppingMain, now an easier entry point: public static void main(String[] args) { // the Catalog is a simple Array List of Items: ArrayList
ShoppingFrame f = new ShoppingFrame(list); f.setVisible(true); }
import java.util.ArrayList;
public class ShoppingCart extends ArrayList
//Create a class variables.
private ArrayList
private double total = 0;
private double discount = 0;
// Constructor that creates an empty list of orders
public ShoppingCart(){
super();
total = 0;
}
// Boolean method to return true when an item added successfully.
public boolean add(NumSelected yes) {
for(int i =0 ; i NumSelected temp = NumSelected.get(i); if(temp.getSku(i).equals(yes.getSku(i))){ NumSelected.set(i, yes); return true; } } NumSelected.add(yes); return true; } // Method to return the total cost of the shopping cart. public double getTotal() { total = 0; for(NumSelected yes : NumSelected){ total +=yes.getPrice(); } return total - discount; } // Method to set discount if order gets discount public void setDiscount(boolean selected) { if(selected){ discount = total * 0.1; } } } import java.text.*; public class Sku { // To create class variables. private String name; private double price; private int bulkQuantity; private double bulkPrice; // Constructor That takes a name and single sku-price and a bulk quantity // and a bulk price as arguments. public Sku(String name, double price, int quantity, double bulk) { this.name = name; this.price = price; if(price<0){ throw new IllegalArgumentException("price must be positive"); } this.bulkQuantity = quantity; if(quantity<0){ throw new IllegalArgumentException("quantity must be positive"); } this.bulkPrice = bulk; if(bulk<0){ throw new IllegalArgumentException("bulk price must be positive"); } } // Constructor that takes a name a price as arguments. public Sku(String name, double price) { if(price<0){ throw new IllegalArgumentException("price must be positive"); } this.name =name; this.price = price; } // Method to return the price for the giving quantity. public double priceFor(int quan){ double first = 0; if(quan < 0){ throw new IllegalArgumentException("price must be positive"); } else{ if(bulkQuantity !=0){ first = (quan/bulkQuantity)*bulkPrice + (quan % bulkQuantity)*price; } else{ first=quan*price; } } return first; } // Return a string representation of sku. public String toString(){ NumberFormat nf = NumberFormat.getCurrencyInstance(); String s = ""; s = name + "," + nf.format(price); if(bulkPrice !=0){ s = name + "," + nf.format(price)+ "(" + bulkQuantity + "for" + nf.format(bulkPrice) + ")"; } return s; } // Boolean to help if two sku's are the same. public boolean equals(Sku boo){ if(this.name.equals(boo.name)){ return true; } return false; } } public class NumSelected { // Create a class variables. private Sku product; private int quantity; // Constructor that creates an order for the given sku and given quantity. public NumSelected(Sku product, int quantity) { this.product = product; this.quantity = quantity; } // Returns the cost for this order. public double getPrice(){ return product.priceFor(quantity); } // Returns a reference (Sku reference) to this order. public Sku getSku(int i){ return product; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
