JAVA!!!- Activity 2. Then in another java file, ManageBooks.java , you will impl
ID: 3696877 • Letter: J
Question
JAVA!!!-
Activity 2.
Then in another java file, ManageBooks.java, you will implement the following methods:
A method, readFromFile, that takes a file name as a string – this file contains book information, and returns an array of items of type Book. This method should handle file-reading errors via exception handling.
A method, sortByPublicationDate, that takes an array of items of type Book, and sorts them by publication date, from oldest to newest.
A method, cheapest, that takes an array of items of type Book,
and returns the cheapest book in the array.
A method, enjoyed, that takes an array of items of type Book, and returns the number of books in the array that you enjoyed.
A recursive method, recEnjoyed, that takes an array of items of type Book and an integer n, and returns the number of books in the array that you enjoyed, starting at index n in the array.
A main method that allows you to test all above methods.
Explanation / Answer
Hi I have modified Book.java so that it can be sort based on published date.
I have implemented all methods. Please test it.
import java.util.ArrayList;
import java.util.Date;
public class Book implements Comparable<Book>{
private int pages;
private double price;
private boolean enjoyed;
private ArrayList<String> authors;
private String title;
private Date date;
// Default constructor
public Book() {
}
/**
* @param pages
* @param price
* @param enjoyed
* @param authors
* @param title
* @param date
*/
public Book( String title,ArrayList<String> authors,int pages,String date, double price, boolean enjoyed) {
super();
this.pages = pages;
this.price = price;
this.enjoyed = enjoyed;
this.authors = authors;
this.title = title;
// getting year, month and day from string
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(4, 6));
int day = Integer.parseInt(date.substring(6));
this.date = new Date(year, month, day);
}
/**
* @return the pages
*/
public int getPages() {
return pages;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @return the enjoyed
*/
public boolean isEnjoyed() {
return enjoyed;
}
/**
* @return the authors
*/
public ArrayList<String> getAuthors() {
return authors;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param pages the pages to set
*/
public void setPages(int pages) {
this.pages = pages;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @param enjoyed the enjoyed to set
*/
public void setEnjoyed(boolean enjoyed) {
this.enjoyed = enjoyed;
}
/**
* @param authors the authors to set
*/
public void setAuthors(ArrayList<String> authors) {
this.authors = authors;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
// print method
public void print(){
System.out.println("Book: "+title+", of "+pages+" pages");
System.out.print("authors: {");
for(int i=0; i<authors.size(); i++){
System.out.print("""+authors.get(i)+""");
if(i != authors.size()-1)
System.out.print(", ");
}
System.out.println(" }");
System.out.println("First published on "+ date.getMonth()+"/"+date.getDate()+"/"+date.getYear());
System.out.println("Price: $"+String.format("%.2f", price));
if(enjoyed)
System.out.println("I enjoyed it very much!");
else
System.out.println("This book was not so great…");
}
private String getDateString(){
return ""+date.getYear()+""+date.getMonth()+""+date.getDate();
}
// compare method based on Published method
@Override
public int compareTo(Book o) {
String d1 = getDateString();
String d2 = getDateString();
return d1.compareTo(d2);
}
}
##################### ManageBooks.java ###########################
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class ManageBooks {
public ArrayList<Book> readFromFile(String fileName){
ArrayList<Book> books = new ArrayList<Book>();
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine(); // reading title
String authors;
int pages;
double price;
boolean enjoyed;
while(line != null && line.trim()!=""){
// creating book object and adding to list
Book b = new Book();
books.add(b);
b.setTitle(line);
// reading authors
authors = br.readLine();
String autrs[] = authors.split("\s+"); // splitting by spaces
ArrayList<String> a = new ArrayList<String>();
for(String s: autrs)
a.add(s);
b.setAuthors(a);
// reading pages
pages = Integer.parseInt(br.readLine().trim());
b.setPages(pages);
// reading price
price = Double.parseDouble(br.readLine().trim());
b.setPrice(price);
// reading enjoyed flag
enjoyed = Boolean.parseBoolean(br.readLine().trim());
b.setEnjoyed(enjoyed);
// reading next book's title
line = br.readLine();
}
}catch(IOException ex){
System.out.println(ex.getMessage());
}
return books;
}
public void sortByPublicationDate(ArrayList<Book> books){
Collections.sort(books);
}
public Book cheapest(ArrayList<Book> books){
if(books == null)
return null;
Book cheapest = books.get(0);
for(int i=1; i<books.size(); i++){
if(books.get(i).getPrice() < cheapest.getPrice())
cheapest = books.get(i);
}
return cheapest;
}
public ArrayList<Book> enjoyed(ArrayList<Book> books){
ArrayList<Book> enjoy = new ArrayList<Book>();
if(books == null)
return null;
for(int i=0; i<books.size(); i++)
if(books.get(i).isEnjoyed())
enjoy.add(books.get(i));
return enjoy;
}
public ArrayList<Book> recEnjoyed(ArrayList<Book> books,int index){
ArrayList result = new ArrayList<Book>();
return recEnjoyed(books,result, index);
}
public ArrayList<Book> recEnjoyed(ArrayList<Book> books, ArrayList<Book> result, int index){
if(index == books.size()) // base case
return result;
result.add(books.get(index));
return recEnjoyed(books, result, index+1);
}
}
#####################################################
Content of file should be:
title
authors (space separated)
pages
price
enjoyed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.