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 02:30
Daryl wouod like to open new checkings and savings accounts one pf his primary concerns is avoiding bank fees
Answers: 2
question
Advanced Placement (AP), 24.06.2019 11:00
The subject option isn't available. the topic is earth science!suppose you are a city engineer whose job it is to explore how to use more renewable energy in your locality. 1. consider the resources in your city and select one of the following: - hydropower -solar power -wind power -geothermal power 2. prepare a pamphlet or multimedia presentation to convince a committee of citizens and local leaders that the renewable energy source you have selected is best for you locality. in your pamphlet or presentation include: - a discussion of the renewable energy source that is best for your town and why -a discussion of the advantages and disadvantages of this energy source -a discussion of why you think your city should use more of this energy source -how the use of renewable energy will impact your city -pictures and/or charts and tables to strengthen your arguments
Answers: 1
question
Advanced Placement (AP), 25.06.2019 20:00
As a result of alcohol poisoning, . a. skin rashes may break out b. blood pressure may rise c. the respiratory system may function erratically d. the nervous system may become suppressed
Answers: 2
question
Advanced Placement (AP), 26.06.2019 00:00
The place or system in which goods are traded illegally it is two words
Answers: 1
You know the right answer?
Complete the class DiscountedItem below that inherits from Item and adds an discount instance variab...
Questions
question
Mathematics, 17.04.2020 20:43
question
Mathematics, 17.04.2020 20:43
question
Mathematics, 17.04.2020 20:43
question
Mathematics, 17.04.2020 20:43
question
Mathematics, 17.04.2020 20:43
Questions on the website: 13722367