Complete the class DiscountedItem below that inherits from Item and adds an discount instance variable with a constructor, get/set, and a toString method. Uncomment the testing code in main to add discounted items to the cart.
import .*;
/**
The ShoppingCart class has an ArrayList of Items.
You will write a new class DiscountedItem that extends Item.
This code is adapted from
*/
public class Tester
{
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
(new Item("bread", 3.25));
(new Item("milk", 2.50));
// Uncomment these to test
// (new DiscountedItem("ice cream", 4.50, 1.50));
// (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) {
(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() + "\nSub-total: " + valueToString(total) + "\nDiscount: " + valueToString(internalDiscount) + "\nTotal: " + valueToString(total - internalDiscount);
}
private String valueToString(double value) {
value = Math.rint(value * 100) / 100.0;
String result = "" + Math.abs(value);
if(result.indexOf(".") == () - 2) {
result += "0";
}
result = "$" + result;
return result;
}
public String orderToString() {
String build = "\nOrder Items:\n";
for(int i = 0; i < (); i++) {
build += " " + (i);
if(i != () - 1) {
build += "\n";
}
}
return build;
}
}
class Item {
private String name;
private double price;
public Item()
{
= "";
= 0.0;
}
public Item(String name, double price) {
= name;
= price;
}
public double getPrice() {
return price;
}
public String valueToString(double value) {
String result = "" + Math.abs(value);
if(result.indexOf(".") == () - 2) {
result += "0";
}
result = "$" + result;
return result;
}
public String toString() {
return name + " " + valueToString(price);
}
}