Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

9.5.4. Programming Challenge : Shopping Cart The following code contains a class called ShoppingCart that simulates a grocery store or an online stores shopping cart.

9.5.4. Programming Challenge : Shopping Cart

The following code contains a class called ShoppingCart that simulates a grocery store or an online stores shopping cart. It has an ArrayList called order that you can use to add Items to the shopping cart. The Item class keeps track of the name and the price of each Item. If you run the code below, you will see that it adds 2 items to the cart and then prints out the total order. It may be easier to follow and change the code in this repl.it link. We encourage you to work in pairs.

In this challenge, you will add a new class called DiscountedItem that extends the Item class. The ArrayList of Items will still work since it can hold the subclasses of Items too! The ShoppingCart printOrder() method will work with Items and DiscountedItems but note that it has an if statement that treats DiscountedItems differently.

In the DiscountedItem subclass,

Add an instance variable for the discount amount.

Add constructors that call the super constructor Item.

Add get/set methods for discount. The get method is given below but you should modify it.

Add a toString() method that returns a string that includes a call to the super toString() method that will print out the price as well as the discount amount using the super.valueToString() method to format it. You could put the discount in parentheses with a minus sign in front of it like (- $.50).

Uncomment the code in the main method to test adding DiscountedItems to the cart.

If you used repl.it or another IDE to complete this challenge, copy the code for DiscountedItem into the ActiveCode below so that it is saved for the next lesson.

given code:

import java.util.*;

/** The ShoppingCart class has an ArrayList of Items. You will write a new class DiscountedItem that extends Item. This code is adapted from https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill */

public class Tester { public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.add(new Item("bread", 3.25)); cart.add(new Item("milk", 2.50));

// Uncomment these to test //cart.add(new DiscountedItem("ice cream", 4.50, 1.50)); //cart.add(new DiscountedItem("apples", 1.35, 0.25));

cart.printOrder(); } }

// DiscountedItem inherits from Item class DiscountedItem extends Item { // add an instance variable for the discount

// Add constructors that call the super constructor

// Add get/set methods for discount public double getDiscount() { return 0.0; // return discount here instead of 0 }

// Add a toString() method that returns a call to the super toString // and then the discount in parentheses using the super.valueToString() method

}

class ShoppingCart { private ArrayList order; private double total; private double internalDiscount;

public ShoppingCart() { order = new ArrayList(); total = 0.0; internalDiscount = 0.0; }

public void add(Item i) { order.add(i); total += i.getPrice(); if (i instanceof DiscountedItem) internalDiscount += ((DiscountedItem) i).getDiscount(); }

/** printOrder() will call toString() to print */ public void printOrder() { System.out.println(this); }

public String toString() { return discountToString(); }

public String discountToString() { return orderToString() + " Sub-total: " + valueToString(total) + " Discount: " + valueToString(internalDiscount) + " Total: " + valueToString(total - internalDiscount); }

private String valueToString(double value) { value = Math.rint(value * 100) / 100.0; String result = "" + Math.abs(value); if(result.indexOf(".") == result.length() - 2) { result += "0"; } result = "$" + result; return result; }

public String orderToString() { String build = " Order Items: "; for(int i = 0; i < order.size(); i++) { build += " " + order.get(i); if(i != order.size() - 1) { build += " "; } } return build; } }

class Item { private String name; private double price;

public Item() { this.name = ""; this.price = 0.0; }

public Item(String name, double price) { this.name = name; this.price = price; }

public double getPrice() { return price; }

public String valueToString(double value) { String result = "" + Math.abs(value); if(result.indexOf(".") == result.length() - 2) { result += "0"; } result = "$" + result; return result; }

public String toString() { return name + " " + valueToString(price); } }

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

More Books

Students also viewed these Databases questions

Question

3. Who would the members be?

Answered: 1 week ago

Question

4. Who would lead the group?

Answered: 1 week ago