Menu

[Solved] Exercise Expand Existing Application Seen Many Similar Examples Class Item Subclasses Clas Q37200376

In this exercise, we expand on an existing application. We haveseen many similar examples to the class Item and its subclasses inclass. This store is expanding to do online sales. The classShoppingCart represents the shopping cart of a customer using theonline site. It has an array list of Item (a customer can addmultiple items to the cart), in addition to two methods to add anitem to the cart and display the cart (similar to the class Shipthat holds multiple Container objects).

The goal of this assignment is to add a price attribute toitems, and then calculate the total price of items in the shoppingcart. We are also interested in breaking down the total price tothe price of grocery/non-grocery items. Once you implement thechanges, you can uncomment the lines in Main and run, you should anoutput similar to:

A virtual item cannot have a quantity more than 1Pencil 10 $0.99Notepad 3 $1.99Apple 5 $0.49 2017-11-11Mango 10 $1.25 2017-12-1 imported from MexicoWonder Woman 1 $9.99 2000.0MBTotal price of all items is $40.81Total price of grocery items is $14.95Total price of non grocery items is $25.860000000000003

  1. Add an attribute price (double) to Item. Add a setter method.Note that since Item is the Base class of GroceryItem,ImportedGroceryItem, and VirtualItem, the attribute and the settermethod are automatically inherited (how wonderful!)
  2. Modify the method toString to add the price to the result
  3. Next implement a method calculateTotal, also in item. Thismethod returns the price * quantity of the item
  4. In ShoppingCart, implement the method getTotal. This methodreturns the total price of all items in the cart
  5. Now, implement the method getGroceryTotal. This method returnsthe total price of grocery items in the cart
  6. Similarly, implement the method getNonGroceryTotal. This methodreturns the total price of non-grocery items in the cart

Hint to differentiate between groceryand non-grocery items, you can use the instanceof operator objectinstanceof Class evaluates to true or false. For example, if(iteminstanceof GroceryItem) {….}

Expert Answer


Answer to In this exercise, we expand on an existing application. We have seen many similar examples to the class Item and its sub… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *