Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Shopping Cart, Programming Project #1. You are to write a set of supporting classes for a simple shopping program. This assignment uses ArrayList two ways,

Shopping Cart, Programming Project #1.

You are to write a set of supporting classes for a simple shopping program. This assignment uses ArrayList two ways, you are given a program that HASA (database language for has a) ArrayList, then you create a ShoppingCart Class where ISA (yes, thats is a) ArrayList. The GUI program (Graphical User Interface) provides the front end to the program, you are writing the back end which is often referred to as the domain specific code, or the data structures that work behind the scenes. Heres the front end after some user selections:

image text in transcribed

Above is a screen shot of what the program might look like when the user has selected various items to order. The terms item and sku can be considered interchangeable, like when you go to the store and select and item, you really place a sku into your cart. And if you buy more, we need to know the number selected. Prices are expressed using doubles and quantities are expressed as simple integers (e.g., you cant buy 2.345 of something). Notice that some of the items have a discount when you buy more. For example, silly putty normally costs $3.95 each, but you can buy 10 for $19.99. These items have, in effect, two prices: a single item price and a bulk item price for a bulk quantity. When computing the price for such an item, 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 $0.99 each but can be bought in bulk 10 for $5.00. The first 10 are sold at that bulk price ($5.00) and the two extras are charged at the single item price ($0.99 each) for a total of $6.98.

At the bottom of the frame you will find a checkbox for an overall discount. If this box is checked, the user is given a 10% discount off the total price. Un-check removes discount. This is computed using simple double arithmetic, computing a price that is 90% of what it would be otherwise.

You need to add 3 classes that are used to make this code work:

Sku (Stock Keeping Unit) will store information about the individual items. SKU is the term for what a barcode means in the store. This Class must have the following public methods and fields.

image text in transcribed

image text in transcribed

The project in our text has little specifications, so I've wrote my own, in the hopes we can learn about "event driven programming." We implement the ActionListener (in the extends JFrame code provided) so each "event" like a click or tab or enter key runs a method that calls your method that totals our shopping cart.

GETTING STARTED: Some people like to start with just console code, so here are some simple tests that might be helpful by just getting the main() method to run: SimpleConsoleTest.javaimage text in transcribed You can also create nearly empty classes and methods that are required to pass the compiler, doing trivial operations like "return 42;" when you see the real solution might be difficult. Just get the JFrame to show up first, then start adding code to complete the project........

Add classes to a Shopping Cart GUI, and make this GUI work: ShoppingFrame.javaimage text in transcribed

If you change the provided ShoppingFrame driver, as we often do during development and debugging, be certain that the original ShoppingFrame.javaimage text in transcribed still works, else your code will not meet my specifications, and won't work when I test your solutions. Note the requirement "extends ArrayList" in many places: This will result in all or nothing grading for this Assignment:

Either correctly use "extends ArrayList" which means ShoppingCart ISA ArrayList (full credit if things work well).

Or ShoppingCart HASA ArrayList, meaning it's a field and you have not used inheritance concepts (zero credit).***

Attach your 3 (THREE) .java files that you wrote to complete this assignment. I will load each of your classes into my own Eclipse project for grading, and use slightly different numbers for cost and items. I will add items into each box, check to see both discounts (bulk and check) work, then removing a few items, and in the end we should all get the same total.

A computation example: One silly putty cost $3.95 and 11 are $23.94, plus one of everything else on the list totals $552.87, but $497.58 if I check the discount, and $479.59 if I remove the Barbie.

Required Files to complete this assignment:

ShoppingCart.pdf

Settings

ShoppingFrame.java

SimpleConsoleTest.javaimage text in transcribed

Do no leave System.out calls in your final products (use as debug tool OK), and be sure your name is in all files submitted.

Submit three Classes:

ShoppingCart.java (extends ArrayList, contains NumSelected objects)

NumSelected.java (tells us how many Sku's we've decided to purchase)

Sku.java (a single item that can be selected in multiples)

No place in your ShoppingCart.java should there be the "new" keyword, and no new object is needed for any of this assignment. To make an ArrayList correctly with ISA, you simply call super() in your ShoppingCart constructor.

No place in any of these Classes should you have "System.out" at all. You can use System.out for debugging and tests, but then remove it, as I should see a clean console free run of my JFrame GUI using your Classes, a real Java application.

import java.io.FileNotFoundException;

// CS211 BC, W.P. Iverson

// June 2018

public class SimpleConsoleTest {

public static void main(String[] args) throws FileNotFoundException {

Sku one = new Sku("book",13); // sku #49284

Sku two = new Sku("book",13); // a different book

Sku three = new Sku("another",42,10,399);

System.out.println(one.equals(one)); // true

System.out.println(one.equals(two)); // false

System.out.println(one); // book, $13.00

System.out.println(two.priceFor(123)); // 1599.0

ShoppingCart basket = new ShoppingCart();

System.out.println(basket.size()); // 0

NumSelected five = new NumSelected(two,5);

NumSelected six = new NumSelected(three,4);

basket.add(five);

basket.add(six);

System.out.println(basket.size()); // 2

System.out.println(three); // another, $42.00 (10 for $399.00)

System.out.println(three.getSKU()); // 49286

System.out.println(basket.getTotal()); // 233.0

basket.add(new NumSelected(three,1));

System.out.println(basket.getTotal()); // 107.0

basket.add(new NumSelected(three,11)); // remove the 1, add 11

System.out.println(basket.getTotal()); // 506.0

basket.setDiscount(true);

System.out.println(basket.getTotal()); // 455.4

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.*;

import java.util.*;

@SuppressWarnings("serial")

public class ShoppingFrame extends JFrame {

// data FIELDS

private ShoppingCart selections; // assigned

private JTextField total; // standard Oracle JAVA GUI

final JCheckBox cb = new JCheckBox("qualify for discount");

// This INNER CLASS is just to make some anonymous objects

// done this way because nothing outside this class needs it

private class EventExample implements ActionListener {

public void actionPerformed(ActionEvent e) {

selections.setDiscount(cb.isSelected());

updateTotal();

}

}

public ShoppingFrame(ArrayList products) {

// create frame and order list

setTitle("CS Gift Catalog");

setDefaultCloseOperation(EXIT_ON_CLOSE);

selections = new ShoppingCart();

// Just making sure we're using the Iverson version:

if (!(selections instanceof ArrayList))

throw new RuntimeException("This quarter I require an ArrayList");

// set up text field with order total

total = new JTextField("$0.00", 12);

total.setEditable(false);

total.setEnabled(false);

total.setDisabledTextColor(Color.BLACK);

JPanel p = new JPanel();

p.setBackground(Color.blue);

JLabel l = new JLabel("Order Total");

l.setForeground(Color.YELLOW);

p.add(l);

p.add(total);

add(p, BorderLayout.NORTH);

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 products) {

for (Sku i: products) {

if (products.indexOf(i) != products.lastIndexOf(i))

products.remove(i);

}

}

// Sets up the "discount" checkbox for the frame

private JPanel makeCheckBoxPanel() {

JPanel p = new JPanel();

p.setBackground(Color.blue);

p.add(cb);

cb.addActionListener(new EventExample());

return p;

}

// adds a product to the panel, including a textfield for user input of

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);

// Below is a trick to make an anonymous object from an anonymous class

// The addActionListener needs something that implements ActionListener

// So we make an object from a class that does not exist as follows:

quantity.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

updateItem(product, quantity);

quantity.transferFocus();

}

});

// same trick

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) {

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 list = new ArrayList();

list.add(new Sku("silly putty", 3.95, 10, 19.99));

list.add(new Sku("silly string", 3.50, 10, 14.95));

list.add(new Sku("bottle o bubbles", 0.99));

list.add(new Sku("Nintendo Wii system", 389.99));

list.add(new Sku("Mario Computer Science Party 2 (Wii)", 49.99));

list.add(new Sku("Don Knuth Code Jam Challenge (Wii)", 49.99));

list.add(new Sku("Computer Science pen", 3.40));

list.add(new Sku("Rubik's cube", 9.10));

list.add(new Sku("Computer Science Barbie", 19.99));

list.add(new Sku("'Java Rules!' button", 0.99, 10, 5.0));

list.add(new Sku("'Java Rules!' bumper sticker", 0.99, 20, 8.95));

ShoppingFrame f = new ShoppingFrame(list);

f.setVisible(true);

}

}

CS Gift Catalog order total $479.59 silly putty, $3.95 (10 for $19.99) 1silly string, $3.50 (10 for $14.95) bottle o bubbles, $0.99 1Nintendo Wii system, $389.99 Mario Computer Science Party 2 (Wi), $49.99 Don Knuth Code Jam Challenge (Wii), $49.99 Computer Science pen, $3.40 Rubik's cube, $9.10 Computer Science Barbie, $19.99 Java Rules!' button, $0.99 (10 for $5.00) Java Rules!' bumper sticker, $0.99 (20 for $8.95) qualify for 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

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions