Anzeige
In the last lesson you created a class called DiscountedIte.pdf
In the last lesson you created a class called DiscountedIte.pdf
In the last lesson you created a class called DiscountedIte.pdf
In the last lesson you created a class called DiscountedIte.pdf
Nächste SlideShare
You are to create a grocery store application that uses Object Oriente.docxYou are to create a grocery store application that uses Object Oriente.docx
Wird geladen in ... 3
1 von 4
Anzeige

Más contenido relacionado

Similar a In the last lesson you created a class called DiscountedIte.pdf(20)

Más de adithiyaatextile(20)

Anzeige

In the last lesson you created a class called DiscountedIte.pdf

  1. 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 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 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 { private double discount; // add an instance variable for the discount public DiscountedItem(String name, double price, double discount) { super(name, price);
  2. this.discount = discount; } // Add get/set methods for discount public double getDiscount() { return discount; // return discount here instead of 0 } public void setDiscount(double discount) { this.discount = discount; } // Add a toString() method that returns a call to the super toString and then // the discount in parentheses using the super.valueToString() method public String toString() { return super.toString() + (super.valueToString(discount)); } } // Add a method called countDiscountedItems() class ShoppingCart { private ArrayList<Item> order; private double total; private double internalDiscount; public ShoppingCart() { order = new ArrayList<Item>(); 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() {
  3. 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";
  4. } result = "$" + result; return result; } public String toString() { return name + " " + valueToString(price); } }
Anzeige