In: Computer Science
Java iteration method, I need a method which iterates through a collection of books and adjusts the price of all books published between the given parameters to give a 20% discount off the price of each book published between the given years?
Book class
public class Book
{
private String title;
private String author;
private int yearPublished;
private double bookPriceInCAD;
public Book(String inputTitle, String
inputAuthor, int inputYearPublished, double
inputBookPriceInCAD){
setTitle(inputTitle);
setAuthor(inputAuthor);
setYearPublished(inputYearPublished);
setBookPriceInCAD(inputBookPriceInCAD);
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public int getYearPublished(){
return
yearPublished;
}
public double getBookPriceInCAD(){
return
bookPriceInCAD;
}
public void setTitle(String title){
if(title !=null
&& !title.isEmpty()){
this.title = title;
} else if(title ==
null){
throw new IllegalArgumentException("title cannot be null");
} else
if(title.isEmpty()){
throw new IllegalArgumentException("title cannot be an empty
String");
}
}
public void setAuthor(String
author){
if(author !=null
&& !author.isEmpty()){
this.author = author;
} else if(author ==
null){
throw new IllegalArgumentException("author cannot be null");
} else
if(author.isEmpty()){
throw new IllegalArgumentException("author cannot be an empty
String");
}
}
public void setYearPublished(int
yearPublished){
if(yearPublished >
0){
this.yearPublished = yearPublished;
} else {
throw new IllegalArgumentException("year published cannot be
negative");
}
}
public void setBookPriceInCAD(double
bookPriceInCAD){
if(bookPriceInCAD >
0){
this.bookPriceInCAD = bookPriceInCAD;
} else {
throw new IllegalArgumentException("book price in CAD cannot be
negative");
}
}
BookStore Class
import java.util.ArrayList;
import java.util.Iterator;
public class BookStore
{
private ArrayList<Book> bookList;
private String businessName;
public BookStore()
{
// initialise instance
variables
bookList = new
ArrayList<Book>();
businessName = "Book
Store";
}
public BookStore(String
inputBusinessName){
setBusinessName(inputBusinessName);
bookList = new
ArrayList<Book>();
}
public void setBusinessName(String
businessName){
if(businessName !=null
&& !businessName.isEmpty()){
this.businessName = businessName;
} else if(businessName
== null){
throw new IllegalArgumentException("business Name cannot be
null");
} else
if(businessName.isEmpty()){
throw new IllegalArgumentException("business Name cannot be an
empty String");
}
}
public String getBusinessName(){
return
businessName;
}
public ArrayList<Book>
getBookList(){
return bookList;
}
public void addBook(Book book){
if(book!=null){
bookList.add(book);
}
}
public void getBook(int index) {
if((index >= 0) && (index <=
bookList.size())) {
Book oneBook =
bookList.get(index);
oneBook.displayDetails();
}
else{
System.out.println("Invalid index position");
}
}
public void searchBook(String title){
for(Book b:
bookList){
String bookTitle =
b.getTitle();
if(bookTitle.equalsIgnoreCase(title)){
b.displayDetails();
} else{
System.out.println("book not found");
}
}
}
public void displayBookDetails(){
for(Book oneBook:
bookList){
oneBook.displayDetails();
}
}
public static void main(String[] args){
BookStore list = new
BookStore();
Book b1 = new
Book("hello world","steven segal",2005,20.00);
Book b2 = new
Book("goodbye world","Jeff country", 208,10.00);
Book b3 = new Book("no
world","Bill Nye",202,30.00);
list.addBook(b1);
list.addBook(b2);
list.addBook(b3);
list.getBook(4);
list.getBook(2);
list.displayBookDetails();
}
public int donateBook(int
yearPublished){
Iterator<Book>
iter = bookList.iterator();
int count = 0;
while(iter.hasNext()){
Book aBook = iter.next();
if(yearPublished <= aBook.getYearPublished()){
iter.remove();
count++;
}
}
return count;
}
public void applyDiscountToBook(int
beginYear, int endYear){
}
Hi,
Please find the apply discount method:
Steps:
Java Method:
public void applyDiscountToBook(int beginYear, int
endYear){
Iterator<Book> iter =
bookList.iterator();
while(iter.hasNext()){
Book aBook = iter.next();
if(beginYear <= aBook.getYearPublished() && endYear
>= aBook.getYearPublished()){
double beforeDiscountPrice = aBook.getBookPriceInCAD();
//getBookPrice
double afterDiscountPrice= beforeDiscountPrice * 0.8 ;//20%
off
aBook.setBookPriceInCAD(afterDiscountPrice);//set discounted
price
}
}
}
Screenshot:
Hope this helps.