Knowee
Questions
Features
Study Tools

In the last lesson, you created a class called DiscountedItem as part of a Shopping Cart application. Please copy your solutions from the last lesson into the Active Code window below (or in repl or another IDE) before completing this challenge.The ShoppingCart contains a polymorphic ArrayList called order that you can use to add Items or DiscountedItems to the shopping cart. The Item class keeps track of the name and the price of each Item. The DiscountedItem class you wrote in the last lesson adds on a discount amount.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 ArrayList<Item>. Use instanceof to test each item to see if it is a DiscountedItem.Save & Run2024/3/18 13:34:41 - 2 of 2Show CodeLensReformat1import java.util.*;2​3/**4 * The ShoppingCart class has an ArrayList of Items. You will write a new class5 * DiscountedItem that extends Item. This code is adapted6 * https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill7 */8public class Tester9{10 public static void main(String[] args)11 {12 ShoppingCart cart = new ShoppingCart();13 cart.add(new Item("bread", 3.25));14 cart.add(new Item("milk", 2.50));15 // cart.add(new DiscountedItem("ice cream", 4.50, 1.50));16 // cart.add(new DiscountedItem("apples", 1.35, 0.25));17​18 cart.printOrder();19 }20}21​22class DiscountedItem extends Item23{24 // Copy your code from the last lesson's challenge here!25 // add an instance variable for the discount26 private double discount;27​28 // Add constructors that call the super constructor29 public DiscountedItem(String name, double price1, double discount1)30 {31 super(name,price1);32 discount = discount1;33 }34​35 // Add get/set methods for discount36 public double getDiscount()37 {38 return 9.5; // return discount here instead of 039 }40 public void setDiscount(double discountNew)41 {42 discount = discountNew;43 }44​45 // Add a toString() method that returns a call to the super toString46 public String toString()47 {48 return super.toString() + super.valueToString(discount); 49 }50 // and then the discount in parentheses using the super.valueToString() method51​52}53​54// Add a method called countDiscountedItems()55class ShoppingCart56{57 private ArrayList<Item> order;58 private double total;59 private double internalDiscount;60​61 public ShoppingCart()62 {63 order = new ArrayList<Item>();64 total = 0.0;65 internalDiscount = 0.0;66 }67​68 public void add(Item i)

Question

In the last lesson, you created a class called DiscountedItem as part of a Shopping Cart application. Please copy your solutions from the last lesson into the Active Code window below (or in repl or another IDE) before completing this challenge.The ShoppingCart contains a polymorphic ArrayList called order that you can use to add Items or DiscountedItems to the shopping cart. The Item class keeps track of the name and the price of each Item. The DiscountedItem class you wrote in the last lesson adds on a discount amount.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 ArrayList<Item>. Use instanceof to test each item to see if it is a DiscountedItem.Save & Run2024/3/18 13:34:41 - 2 of 2Show CodeLensReformat1import java.util.*;2​3/**4 * The ShoppingCart class has an ArrayList of Items. You will write a new class5 * DiscountedItem that extends Item. This code is adapted6 * https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill7 */8public class Tester9{10 public static void main(String[] args)11 {12 ShoppingCart cart = new ShoppingCart();13 cart.add(new Item("bread", 3.25));14 cart.add(new Item("milk", 2.50));15 // cart.add(new DiscountedItem("ice cream", 4.50, 1.50));16 // cart.add(new DiscountedItem("apples", 1.35, 0.25));17​18 cart.printOrder();19 }20}21​22class DiscountedItem extends Item23{24 // Copy your code from the last lesson's challenge here!25 // add an instance variable for the discount26 private double discount;27​28 // Add constructors that call the super constructor29 public DiscountedItem(String name, double price1, double discount1)30 {31 super(name,price1);32 discount = discount1;33 }34​35 // Add get/set methods for discount36 public double getDiscount()37 {38 return 9.5; // return discount here instead of 039 }40 public void setDiscount(double discountNew)41 {42 discount = discountNew;43 }44​45 // Add a toString() method that returns a call to the super toString46 public String toString()47 {48 return super.toString() + super.valueToString(discount); 49 }50 // and then the discount in parentheses using the super.valueToString() method51​52}53​54// Add a method called countDiscountedItems()55class ShoppingCart56{57 private ArrayList<Item> order;58 private double total;59 private double internalDiscount;60​61 public ShoppingCart()62 {63 order = new ArrayList<Item>();64 total = 0.0;65 internalDiscount = 0.0;66 }67​68 public void add(Item i)

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

69 {70 order.add(i);71 total += i.getPrice();72 if (i instanceof DiscountedItem)73 {74 internalDiscount += ((DiscountedItem) i).getDiscount();75 }76 }77​78 public int countDiscountedItems()79 {80 int count = 0;81 for(Item i : order)82 {83 if(i instanceof DiscountedItem)84 {85 count++;86 }87 }88 return count;89 }90​91 public void printOrder()92 {93 System.out.println("\nOrder Items:");94 for (int i = 0; i < order.size(); i++)95 {96 System.out.println(order.get(i));97 }98 System.out.println("\nTotal: " + valueToString(total));99 System.out.println("Discount: " + valueToString(internalDiscount));100 System.out.println("Total (with discount): " + valueToString(total - internalDiscount));101 System.out.println("Number of discounted items: " + countDiscountedItems());102 }103​104 private String valueToString(double value)105 {106 String str = "" + Math.abs(value);107 while (str.length() < 4)108 {109 str += "0";110 }111 return str;112 }113}114​115class Item116{117 private String name;118 private double price;119​120 public Item(String name, double price)121 {122 this.name = name;123 this.price = price;124 }125​126 public double getPrice()127 {128 return price;129 }130​131 public String toString()132 {133 return name + " " + valueToString(price);134 }135​136 protected String valueToString(double value)137 {138 String str = "" + Math.abs(value);139 while (str.length() < 4)140 {141 str += "0";142 }143 return str;144 }145}

This problem has been solved

Similar Questions

/** A simulated cash register that tracks the item count and the total amount due.*/public class CashRegister{ // private data--see the "Designing the Data Representation" section /** Adds an item to this cash register. @param price the price of this item */ public void addItem(double price) { // implementation--see the "Implementing Instance Methods" section } /** Gets the price of all items in the current sale. @return the total amount */ public double getTotal() { // implementation--see the "Implementing Instance Methods" section } /** Gets the number of items in the current sale. @return the item count */ public int getCount() { // implementation--see the "Implementing Instance Methods" section } /** Clears the item count and the total. */ public void clear() { // implementation--see the "Implementing Instance Methods" section }}

write a java program to define a class Item having item namepricequantityfind the total cost of the itemuse accept method to get the data from the user and toString method to display the detailscreate two objects and display the name of the item which is ordered more quantitydisplay total amount of items purchased.edited 10:37 AM

online shopping cartYou are required to implement a simple C++ program for an online shopping cart using a single class. The program should include a class ShoppingCart to represent the shopping cart. Each item in the cart has a name, price, and quantity.The ShoppingCart class should have the following:Private attributes for an array of item names, an array of item prices, an array of item quantities, the total number of items in the cart, and a flag indicating whether the customer is an old customer.A member function addItem that takes the item name, price, and quantity as arguments and adds them to the cart.A member function calculateTotal that calculates the total price of all items in the cart, taking into account a 10% discount for old customers,using the this pointer.A member function displayCart that displays the information of all items in the cart, including the total price and any applicable discounts or rewards. Award 1 reward point for every Rs.10 spent. This function should also demonstrate the usage of functions with objects as arguments.Testcase1:Sample input:2  // no of itemsKeypad  // item1000   // price2  // quantityEarphone501Y  // Old customerOutput:Rs.1845.00184.5  // RewardTestcase 2:Sample Input:3Keyboard5001Mouse3001Monitor10001NOutput:Rs.1800.00180

Write a program in Java using a method Discount( ), to calculate a single discount or a successive discount. Use overload methods Discount(int), Discount(int,int) and Discount(int,int,int) to calculate single discount and successive discount respectively. Calculate and display the amount to be paid by the customer after getting discounts on the printed price of an article.

You are required to implement a simple C++ program for an online shopping cart using a single class. The program should include a class ShoppingCart to represent the shopping cart. Each item in the cart has a name, price, and quantity.The ShoppingCart class should have the following:Private attributes for an array of item names, an array of item prices, an array of item quantities, the total number of items in the cart, and a flag indicating whether the customer is an old customer.A member function addItem that takes the item name, price, and quantity as arguments and adds them to the cart.A member function calculateTotal that calculates the total price of all items in the cart, taking into account a 10% discount for old customers,using the this pointer.A member function displayCart that displays the information of all items in the cart, including the total price and any applicable discounts or rewards. Award 1 reward point for every Rs.10 spent. This function should also demonstrate the usage of functions with objects as arguments.Testcase1:Sample input:2  // no of itemsKeypad  // item1000   // price2  // quantityEarphone501Y  // Old customerOutput:Rs.1845.00184.5  // RewardTestcase 2:Sample Input:3Keyboard5001Mouse3001Monitor10001NOutput:Rs.1800.00180

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.