Question

1. First create the books object array with data for 20 books(Don't use meaningless data like...

1. First create the books object array with data for 20 books(Don't use meaningless data like "abc" etc). Make sure there are books from Canada, USA, UK and maybe a few other countries. (define this in global scope)

Make sure that you have the following information for the books in the books object:

  • name, author, price & origin

Origin will be the name of the country from which the book was published. So examples are "US", "Canada", "UK" etc.

3. Sample book object:

  • name: "Harry Potter and the Prisoner of Azkaban"
  • author: "J. K. Rowling"
  • price: 200.65
  • origin: "UK"

3. Using the forEach method, iterate through this array and

  • get the price of each book
  • determine the discount amount
    • If the country of origin is US, discount % will be 10%
    • If the country of origin is UK, discount % will be 5%
    • If the country of origin is Canada, discount % will be 12%
    • If the country of origin is other than the above 3 countries, the discount % will be 15%
  • Find the sales price after discount and add it as a property called "salesprice" to the book object (see bmi example)
  • write the name, author, price and salesprice of each book in the console.log
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package mis1;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

public class BookTester {

public static void writeToFile(String fullpath,String res) throws IOException{

//wtite the give string at specified file path

BufferedWriter writer = new BufferedWriter(new FileWriter(fullpath));

writer.write(res);

writer.close();

System.out.println("Filw written successfully. please check at "+fullpath);

}

public static void main(String[] args) throws IOException {

ArrayList<Book> book=new ArrayList(); //list to contain book object

//book object

Book b=new Book("Who I am","UK","Paolo",344.0);

Book b1=new Book("This is us","Canada","Jiya",765.0);

Book b2=new Book("Room 21","UK","Ruby",543.0);

Book b3=new Book("Breath","UK","jhon",765.0);

Book b4=new Book("Fallling","USA","Moni J",67.0);

Book b5=new Book("wating","USA","mark T",431.0);

Book b6=new Book("221 Hall","UK","Paolo",7654.0);

Book b7=new Book("Us","UK","K.k",89.0);

Book b8=new Book("Missing","UK","Aurth",100.0);

Book b9=new Book("searching","UK","G.l",9987.0);

Book b10=new Book("Monty","USA","P.n",456.0);

Book b11=new Book("Aaj nahi","India","Dinesh",344.0);

Book b12=new Book("Part","UK","Paolo",344.0);

Book b13=new Book("First 1","USA","M.A",45.0);

Book b14=new Book("No means no","Canada","Paolo",567.0);

Book b15=new Book("Two and half","Canada","M.jhon",344.0);

Book b16=new Book("I do","Russia","d.d",344.0);

Book b17=new Book("JO","UK","Paolo",344.0);

Book b18=new Book("Do Goa gone","India","Me",344.0);

Book b19=new Book("Who I am","UK","Paolo",344.0);

//put book object to list

book.add(b);

book.add(b1);

book.add(b2);

book.add(b3);

book.add(b4);

book.add(b5);

book.add(b6);

book.add(b7);

book.add(b8);

book.add(b9);

book.add(b10);

book.add(b12);

book.add(b13);

book.add(b14);

book.add(b15);

book.add(b16);

book.add(b17);

book.add(b18);

book.add(b19);

String res="";

for(Book bb:book){ //for each loop

//calculate price of the book as given in ques

if(bb.getOrigin().equalsIgnoreCase("USA")){

double sellPrice=bb.getPrice()+bb.getPrice()*.10;

bb.setSellprice(sellPrice);

}

if(bb.getOrigin().equalsIgnoreCase("UK")){

double sellPrice=bb.getPrice()+bb.getPrice()*.05;

bb.setSellprice(sellPrice);

}

if(bb.getOrigin().equalsIgnoreCase("Canada")){

double sellPrice=bb.getPrice()+bb.getPrice()*.12;

bb.setSellprice(sellPrice);

}

else{

double sellPrice=bb.getPrice()+bb.getPrice()*.15;

bb.setSellprice(sellPrice);

}

res+=bb.toString()+"\n"; //store all properties to string

}

writeToFile("/Users/snehkuma/HomeworkLib/HomeworkLib/console.log",res); //write string to console.log file

}

}

class Book{

//all book properties

String name;

String origin;

String auther;

double price;

double sellprice;

//constructor to initialize the values

public Book(String name, String origin, String auther, double price) {

super();

this.name = name;

this.origin = origin;

this.auther = auther;

this.price = price;

}

//getter setter methods

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getOrigin() {

return origin;

}

public void setOrigin(String origin) {

this.origin = origin;

}

public String getAuther() {

return auther;

}

public void setAuther(String auther) {

this.auther = auther;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public double getSellprice() {

return sellprice;

}

public void setSellprice(double sellprice) {

this.sellprice = sellprice;

}

@Override

public String toString() {

return " [name=" + name + ", origin=" + origin + ", auther=" + auther + ", price=" + price + ", sellprice="

+ sellprice + "]";

}

}

output

console.log

[name=Who I am, origin=UK, auther=Paolo, price=344.0, sellprice=395.6]

[name=This is us, origin=Canada, auther=Jiya, price=765.0, sellprice=856.8]

[name=Room 21, origin=UK, auther=Ruby, price=543.0, sellprice=624.45]

[name=Breath, origin=UK, auther=jhon, price=765.0, sellprice=879.75]

[name=Fallling, origin=USA, auther=Moni J, price=67.0, sellprice=77.05]

[name=wating, origin=USA, auther=mark T, price=431.0, sellprice=495.65]

[name=221 Hall, origin=UK, auther=Paolo, price=7654.0, sellprice=8802.1]

[name=Us, origin=UK, auther=K.k, price=89.0, sellprice=102.35]

[name=Missing, origin=UK, auther=Aurth, price=100.0, sellprice=115.0]

[name=searching, origin=UK, auther=G.l, price=9987.0, sellprice=11485.05]

[name=Monty, origin=USA, auther=P.n, price=456.0, sellprice=524.4]

[name=Part, origin=UK, auther=Paolo, price=344.0, sellprice=395.6]

[name=First 1, origin=USA, auther=M.A, price=45.0, sellprice=51.75]

[name=No means no, origin=Canada, auther=Paolo, price=567.0, sellprice=635.04]

[name=Two and half, origin=Canada, auther=M.jhon, price=344.0, sellprice=385.28]

[name=I do, origin=Russia, auther=d.d, price=344.0, sellprice=395.6]

[name=JO, origin=UK, auther=Paolo, price=344.0, sellprice=395.6]

[name=Do Goa gone, origin=India, auther=Me, price=344.0, sellprice=395.6]

[name=Who I am, origin=UK, auther=Paolo, price=344.0, sellprice=395.6]

Add a comment
Know the answer?
Add Answer to:
1. First create the books object array with data for 20 books(Don't use meaningless data like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT