SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
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);
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() {
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);
}
}

Contenu connexe

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

Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Damien Carbery
 
Program Specifications in c++ Develop an inventory management system f.docx
Program Specifications in c++ Develop an inventory management system f.docxProgram Specifications in c++ Develop an inventory management system f.docx
Program Specifications in c++ Develop an inventory management system f.docxsharold2
 
Program Specifications in c++ Develop an inventory management syste.docx
Program Specifications in c++    Develop an inventory management syste.docxProgram Specifications in c++    Develop an inventory management syste.docx
Program Specifications in c++ Develop an inventory management syste.docxsharold2
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfudit652068
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfflashfashioncasualwe
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfdeepaksatrker
 
Can someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdfCan someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdfaksachdevahosymills
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)GOG.com dev team
 
Here is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdfHere is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdfmallik3000
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfIan0J2Bondo
 
Working with WooCommerce Custom Fields
Working with WooCommerce Custom FieldsWorking with WooCommerce Custom Fields
Working with WooCommerce Custom FieldsAnthony Hortin
 

Similaire à In the last lesson you created a class called DiscountedIte.pdf (15)

Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
 
Program Specifications in c++ Develop an inventory management system f.docx
Program Specifications in c++ Develop an inventory management system f.docxProgram Specifications in c++ Develop an inventory management system f.docx
Program Specifications in c++ Develop an inventory management system f.docx
 
Program Specifications in c++ Develop an inventory management syste.docx
Program Specifications in c++    Develop an inventory management syste.docxProgram Specifications in c++    Develop an inventory management syste.docx
Program Specifications in c++ Develop an inventory management syste.docx
 
These are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdfThese are the outputs which should match they are 4 of them -outp.pdf
These are the outputs which should match they are 4 of them -outp.pdf
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
 
Can someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdfCan someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdf
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)
 
Here is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdfHere is the assignment5.java file -You are required, but not limi.pdf
Here is the assignment5.java file -You are required, but not limi.pdf
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
 
Patterns in PHP
Patterns in PHPPatterns in PHP
Patterns in PHP
 
Working with WooCommerce Custom Fields
Working with WooCommerce Custom FieldsWorking with WooCommerce Custom Fields
Working with WooCommerce Custom Fields
 
Chapter 3 - part1
Chapter 3 - part1Chapter 3 - part1
Chapter 3 - part1
 

Plus de adithiyaatextile

In the pedigree below persons with a hatched marking are af.pdf
In the pedigree below persons with a hatched marking are af.pdfIn the pedigree below persons with a hatched marking are af.pdf
In the pedigree below persons with a hatched marking are af.pdfadithiyaatextile
 
In terms of race and ethnicity a People who are White in ra.pdf
In terms of race and ethnicity a People who are White in ra.pdfIn terms of race and ethnicity a People who are White in ra.pdf
In terms of race and ethnicity a People who are White in ra.pdfadithiyaatextile
 
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdf
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdfIn the NPR Hidden Brain Podcast we listened to Professor Ban.pdf
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdfadithiyaatextile
 
In the following exercise we use data from HollywoodMovies.pdf
In the following exercise we use data from HollywoodMovies.pdfIn the following exercise we use data from HollywoodMovies.pdf
In the following exercise we use data from HollywoodMovies.pdfadithiyaatextile
 
In the movie Austin Powers International Man of Mystery a.pdf
In the movie Austin Powers International Man of Mystery a.pdfIn the movie Austin Powers International Man of Mystery a.pdf
In the movie Austin Powers International Man of Mystery a.pdfadithiyaatextile
 
In the past Ethernets predominantly used a medium access co.pdf
In the past Ethernets predominantly used a medium access co.pdfIn the past Ethernets predominantly used a medium access co.pdf
In the past Ethernets predominantly used a medium access co.pdfadithiyaatextile
 
In the initial Cournot oligopoly equilibrium both firms hav.pdf
In the initial Cournot oligopoly equilibrium both firms hav.pdfIn the initial Cournot oligopoly equilibrium both firms hav.pdf
In the initial Cournot oligopoly equilibrium both firms hav.pdfadithiyaatextile
 
In the fruit fly Drosophila a spineless no wing bristles.pdf
In the fruit fly Drosophila a spineless no wing bristles.pdfIn the fruit fly Drosophila a spineless no wing bristles.pdf
In the fruit fly Drosophila a spineless no wing bristles.pdfadithiyaatextile
 
In the implementation of the ordinary Queue if the dequeue .pdf
In the implementation of the ordinary Queue if the dequeue .pdfIn the implementation of the ordinary Queue if the dequeue .pdf
In the implementation of the ordinary Queue if the dequeue .pdfadithiyaatextile
 
In the formula IPAT which of the following would be repres.pdf
In the formula IPAT which of the following would be repres.pdfIn the formula IPAT which of the following would be repres.pdf
In the formula IPAT which of the following would be repres.pdfadithiyaatextile
 
In the game Sorry players draw a card and move around the.pdf
In the game Sorry players draw a card and move around the.pdfIn the game Sorry players draw a card and move around the.pdf
In the game Sorry players draw a card and move around the.pdfadithiyaatextile
 
In the following web login system Assuming that the applica.pdf
In the following web login system Assuming that the applica.pdfIn the following web login system Assuming that the applica.pdf
In the following web login system Assuming that the applica.pdfadithiyaatextile
 
In the following table provide a short explanation of how ea.pdf
In the following table provide a short explanation of how ea.pdfIn the following table provide a short explanation of how ea.pdf
In the following table provide a short explanation of how ea.pdfadithiyaatextile
 
In the field of anthropology primatology and genetics ther.pdf
In the field of anthropology primatology and genetics ther.pdfIn the field of anthropology primatology and genetics ther.pdf
In the field of anthropology primatology and genetics ther.pdfadithiyaatextile
 
In the film The Ghosts of Stonehenge it is suggested that.pdf
In the film The Ghosts of Stonehenge it is suggested that.pdfIn the film The Ghosts of Stonehenge it is suggested that.pdf
In the film The Ghosts of Stonehenge it is suggested that.pdfadithiyaatextile
 
In the fall of 2021 the United States enacted a 12 trilli.pdf
In the fall of 2021 the United States enacted a 12 trilli.pdfIn the fall of 2021 the United States enacted a 12 trilli.pdf
In the fall of 2021 the United States enacted a 12 trilli.pdfadithiyaatextile
 
In the early 2000s manatee numbers in Florida were hoverin.pdf
In the early 2000s manatee numbers in Florida were hoverin.pdfIn the early 2000s manatee numbers in Florida were hoverin.pdf
In the early 2000s manatee numbers in Florida were hoverin.pdfadithiyaatextile
 
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdf
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdfIn the 1960s the Supreme Court of Chief Justice Earl Warren.pdf
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdfadithiyaatextile
 
In Python I am struggling with getting the functions to wor.pdf
In Python I am struggling with getting the functions to wor.pdfIn Python I am struggling with getting the functions to wor.pdf
In Python I am struggling with getting the functions to wor.pdfadithiyaatextile
 
In the Bully leader election algorithm if a process receive.pdf
In the Bully leader election algorithm if a process receive.pdfIn the Bully leader election algorithm if a process receive.pdf
In the Bully leader election algorithm if a process receive.pdfadithiyaatextile
 

Plus de adithiyaatextile (20)

In the pedigree below persons with a hatched marking are af.pdf
In the pedigree below persons with a hatched marking are af.pdfIn the pedigree below persons with a hatched marking are af.pdf
In the pedigree below persons with a hatched marking are af.pdf
 
In terms of race and ethnicity a People who are White in ra.pdf
In terms of race and ethnicity a People who are White in ra.pdfIn terms of race and ethnicity a People who are White in ra.pdf
In terms of race and ethnicity a People who are White in ra.pdf
 
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdf
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdfIn the NPR Hidden Brain Podcast we listened to Professor Ban.pdf
In the NPR Hidden Brain Podcast we listened to Professor Ban.pdf
 
In the following exercise we use data from HollywoodMovies.pdf
In the following exercise we use data from HollywoodMovies.pdfIn the following exercise we use data from HollywoodMovies.pdf
In the following exercise we use data from HollywoodMovies.pdf
 
In the movie Austin Powers International Man of Mystery a.pdf
In the movie Austin Powers International Man of Mystery a.pdfIn the movie Austin Powers International Man of Mystery a.pdf
In the movie Austin Powers International Man of Mystery a.pdf
 
In the past Ethernets predominantly used a medium access co.pdf
In the past Ethernets predominantly used a medium access co.pdfIn the past Ethernets predominantly used a medium access co.pdf
In the past Ethernets predominantly used a medium access co.pdf
 
In the initial Cournot oligopoly equilibrium both firms hav.pdf
In the initial Cournot oligopoly equilibrium both firms hav.pdfIn the initial Cournot oligopoly equilibrium both firms hav.pdf
In the initial Cournot oligopoly equilibrium both firms hav.pdf
 
In the fruit fly Drosophila a spineless no wing bristles.pdf
In the fruit fly Drosophila a spineless no wing bristles.pdfIn the fruit fly Drosophila a spineless no wing bristles.pdf
In the fruit fly Drosophila a spineless no wing bristles.pdf
 
In the implementation of the ordinary Queue if the dequeue .pdf
In the implementation of the ordinary Queue if the dequeue .pdfIn the implementation of the ordinary Queue if the dequeue .pdf
In the implementation of the ordinary Queue if the dequeue .pdf
 
In the formula IPAT which of the following would be repres.pdf
In the formula IPAT which of the following would be repres.pdfIn the formula IPAT which of the following would be repres.pdf
In the formula IPAT which of the following would be repres.pdf
 
In the game Sorry players draw a card and move around the.pdf
In the game Sorry players draw a card and move around the.pdfIn the game Sorry players draw a card and move around the.pdf
In the game Sorry players draw a card and move around the.pdf
 
In the following web login system Assuming that the applica.pdf
In the following web login system Assuming that the applica.pdfIn the following web login system Assuming that the applica.pdf
In the following web login system Assuming that the applica.pdf
 
In the following table provide a short explanation of how ea.pdf
In the following table provide a short explanation of how ea.pdfIn the following table provide a short explanation of how ea.pdf
In the following table provide a short explanation of how ea.pdf
 
In the field of anthropology primatology and genetics ther.pdf
In the field of anthropology primatology and genetics ther.pdfIn the field of anthropology primatology and genetics ther.pdf
In the field of anthropology primatology and genetics ther.pdf
 
In the film The Ghosts of Stonehenge it is suggested that.pdf
In the film The Ghosts of Stonehenge it is suggested that.pdfIn the film The Ghosts of Stonehenge it is suggested that.pdf
In the film The Ghosts of Stonehenge it is suggested that.pdf
 
In the fall of 2021 the United States enacted a 12 trilli.pdf
In the fall of 2021 the United States enacted a 12 trilli.pdfIn the fall of 2021 the United States enacted a 12 trilli.pdf
In the fall of 2021 the United States enacted a 12 trilli.pdf
 
In the early 2000s manatee numbers in Florida were hoverin.pdf
In the early 2000s manatee numbers in Florida were hoverin.pdfIn the early 2000s manatee numbers in Florida were hoverin.pdf
In the early 2000s manatee numbers in Florida were hoverin.pdf
 
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdf
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdfIn the 1960s the Supreme Court of Chief Justice Earl Warren.pdf
In the 1960s the Supreme Court of Chief Justice Earl Warren.pdf
 
In Python I am struggling with getting the functions to wor.pdf
In Python I am struggling with getting the functions to wor.pdfIn Python I am struggling with getting the functions to wor.pdf
In Python I am struggling with getting the functions to wor.pdf
 
In the Bully leader election algorithm if a process receive.pdf
In the Bully leader election algorithm if a process receive.pdfIn the Bully leader election algorithm if a process receive.pdf
In the Bully leader election algorithm if a process receive.pdf
 

Dernier

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 

Dernier (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

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); } }