Advanced Placement (AP)
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);
}
}

answer
Answers: 1

Other questions on the subject: Advanced Placement (AP)

image
Advanced Placement (AP), 24.06.2019 18:30, janiya73
Ascientist has predicted that the population of earth will continue to grow exponentially until the year 2300. at that point, we will reach carrying capacity of earth’s resources, and population growth will plateau. if the scientist’s prediction is correct, what can you predict about human energy use at that time? justify your answer by explaining the relationship between population growth and energy use. (5 points)
Answers: 1
image
Advanced Placement (AP), 25.06.2019 04:00, Horse0809
The tourism industry’s main job is to cater to customers who 1. travel for pleasure 2. seek the fanciest place to stay overnight 3. want an exquisite dining experience 4. desire to take part in a recreational activity.
Answers: 1
image
Advanced Placement (AP), 25.06.2019 20:00, dcttechgames
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
image
Advanced Placement (AP), 26.06.2019 02:10, alani0720
Camila took an interest inventory in her career explorations class. read her description of the results of the assessment. which two phrases clearly reveal her career interests? when i took the interest inventory in my career explorations class, it revealed that even though i really value the (1.) balance between work and home life, i like an (2.) engaging atmosphere at work, where i can stay busy. based on my (3.) fascination with technology, i should work in a field with computers. based on my (4.) attraction to working with mechanics, i should be working with machines. this leads me to believe that i should be working in robotics, which sounds like a lot of fun! (options are listed with the numbers 1-4)
Answers: 1
Do you know the correct answer?
Complete the class DiscountedItem below that inherits from Item and adds an discount instance variab...

Questions in other subjects:

Konu
Mathematics, 19.02.2021 04:20