subject

In this challenge, you will write a method called int countDiscountedItems() in the ShoppingCart class. This method will use a loop to traverse the ArrayList of Items called order.

In the loop, you will test if each Item is a DiscountedItem by using the instanceof keyword ((object instanceof Class) returns true or false) similar to its use in the add(Item) method.

If it is a DiscountedItem, then you will count it.

At the end of the loop, the method will return the count.

Make sure you print out the number of discounted items in the main method or in printOrder(), so that you can test your method. Add more items to the order to test it.

Copy in your code for DiscountedItem below and then write a method called countDiscountedItems which traverses the polymorphic ArrayLists of Items. Use instanceOf to test items to see if they are a DiscountedItem.

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 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));
//cart. add(new DiscountedItem("ice cream", 4.50, 1.50));
//cart. add(new DiscountedItem("apples", 1.35, 0.25));

cart. printOrder();
}
}

class DiscountedItem extends Item
{
// Copy your code from the last lesson's challenge here!
}

// Add a method called countDiscountedItems()
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: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 23:00
Suppose s, t, and w are strings that have already been created inside main. write a statement or statements, to be added to main, that will determine if the lengths of the three strings are in order by length, smallest to largest. that is, your code should determine if s is strictly shorter than t, and if t is strictly shorter than w. if these conditions hold your code should print (the boolean value) true. if not, your code should print false. (strictly means: no ties) example: if s, t, and w are "cat", "hats", and "skies" your code should print true - their lengths are 3-4-5; but if s, t, and w are "cats" "shirt", and "trust", then print false - their lengths are 4-5-5 enter your code in the box below
Answers: 2
question
Computers and Technology, 24.06.2019 02:00
Write an expression that will cause the following code to print "equal" if the value of sensorreading is "close enough" to targetvalue. otherwise, print "not equal". ex: if targetvalue is 0.3333 and sensorreading is (1.0/3.0), output is:
Answers: 1
question
Computers and Technology, 24.06.2019 14:00
In the microsoft® access® and microsoft excel® programs, the ribbon contains tabs that are divided into with like tools in them. parts groups containers bunches
Answers: 1
question
Computers and Technology, 24.06.2019 14:30
Two students are discussing electricity that has a frequency of 60 hz. student a says that this type of electricity is referred to as ac. student b says that in this type of electricity, the electrons flow in only one direction. which of the following statements is correct? a. only student a is correct b. only student b is correct c. both of the two students are correct d. neither of the two students is correct
Answers: 1
You know the right answer?
In this challenge, you will write a method called int countDiscountedItems() in the ShoppingCart cla...
Questions
question
Mathematics, 15.01.2021 01:00
question
English, 15.01.2021 01:00
question
Mathematics, 15.01.2021 01:00
question
Chemistry, 15.01.2021 01:00