subject
Advanced Placement (AP), 13.03.2021 05:00 Dweath50

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 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() + "\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(".") == result. length() - 2) {
result += "0";
}
result = "$" + result;
return result;
}

public String orderToString() {
String build = "\nOrder Items:\n";
for(int i = 0; i < order. size(); i++) {
build += " " + order. get(i);
if(i != order. size() - 1) {
build += "\n";
}
}
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);
}
}

ansver
Answers: 1

Another question on Advanced Placement (AP)

question
Advanced Placement (AP), 23.06.2019 13:00
Who wants free brainliest and free points for this drivers ed question! if answered correctly! in the u.s. highway numbering system, north-south routes have numbers that end in a. "0" b. "1" c. a letter d. an even number
Answers: 1
question
Advanced Placement (AP), 24.06.2019 04:00
Astudent is revising the sentence below: i know brendan was upset, but i feel he overreacted. you would have thought he'd just lost his best friend, the way he would bemoan his fate. based on the context of this sentence, which is the best synonym for bemoan? a. complain: express dissatisfaction or annoyance b. grieve: cause distress to someone c. nag: annoy with persistent fault-finding d. provoke: stimulate or incite
Answers: 1
question
Advanced Placement (AP), 24.06.2019 20:30
During the decline of the harappan civilization, which group came to power in the indus valley
Answers: 1
question
Advanced Placement (AP), 25.06.2019 03:30
Why is the road most slick when it first begins to rain? a. oils the road has absorbed begin to float. b. the water has not had time to run off. c. a higher concentration of dirt makes the road more slick. d. none of the above
Answers: 1
You know the right answer?
Complete the class DiscountedItem below that inherits from Item and adds an discount instance variab...
Questions
Questions on the website: 13722367