Trying to create a Library using Java 8: How would I code these methods? - a ren
ID: 3798346 • Letter: T
Question
Trying to create a Library using Java 8: How would I code these methods?
- a renewal method which allows the card holder to get a new due date for that book. Renewal periods are in two-week increments.
- a method which takes a date as a parameter and returns a list of books that are due on or before that date.
- a method that checks if any books are overdue. (Note: although in a real-world application, this would check for overdue books using today's date, ALSO create an additional method that takes a date as a parameter for ease of testing.)
- a method that returns a list of all the books checked out, sorted by due date.
Explanation / Answer
Hi,
Please see below the java classes and Sample output. Please comment for any queries/feedbacks.
Thanks,
Anita
Book.java
import java.util.Calendar;
import java.util.Date;
public class Book {
private String name;
private Date duedate;
private boolean isCheckedOut;
//Constructor
public Book(String name, Date duedate, boolean checkedout){
this.name=name;
this.duedate = duedate;
this.isCheckedOut = checkedout;
}
//Renew for Book
public void renew(){
Calendar cal = Calendar.getInstance();
cal.setTime(duedate);
cal.add(Calendar.DAY_OF_YEAR, 14); //Adding to weeks
Date newDueDate = cal.getTime();
setDuedate(newDueDate); //setting new due date to duedate
}
//Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDuedate() {
return duedate;
}
public void setDuedate(Date duedate) {
this.duedate = duedate;
}
public boolean isCheckedOut() {
return isCheckedOut;
}
public void setCheckedOut(boolean isCheckedOut) {
this.isCheckedOut = isCheckedOut;
}
//Overriding toString()
public String toString(){
String retString ="";
retString = "Name : "+this.getName()+ ", Due : "+this.getDuedate();
return retString;
}
}
Library.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
public class Library {
private ArrayList<Book> booklist ;
//Constructorto craete booklist
public Library(){
this.booklist = new ArrayList();
}
//Renewal method
public void renew(Book book){
book.renew(); //Calling book.renew() to increment the duedate for two weeks
System.out.println("Due Date is renewed as "+book.getDuedate()+" for "+book.getName());
}
//This method will return the list of books due on given date
public ArrayList<Book> getBooksDueOn(Date date){
ArrayList<Book> dueBooks = new ArrayList<Book>();
for(Book book : this.getBooklist()){ //Looping through each book in the list
if(book.getDuedate().before(date) && book.isCheckedOut() ){
dueBooks.add(book);
}
}
System.out.println("Books Due on date "+date+" are : ");
System.out.println(Arrays.asList(dueBooks));
return dueBooks;
}
//Checking if book is overdue or not
public boolean overdue(Book book, Date dt){
if(book.getDuedate().before(dt)){
return true;
}
return false;
}
//This method will return the list of books which are checked out
public ArrayList<Book> checkeOutBooks(){
ArrayList<Book> checkedBooks = new ArrayList<Book>();
for(Book book : this.getBooklist()){ //Looping through each book in the list
if(book.isCheckedOut()){
checkedBooks.add(book);
}
}
//Sorting based on the due date using Comparator
Collections.sort(checkedBooks, new Comparator<Book>() {
public int compare(Book b1, Book b2){
return b1.getDuedate().compareTo(b2.getDuedate());
}
});
System.out.println("Checked out books are : ");
System.out.println(Arrays.asList(checkedBooks));
return checkedBooks;
}
//Getters and Setters
public ArrayList<Book> getBooklist() {
return booklist;
}
public void setBooklist(ArrayList<Book> booklist) {
this.booklist = booklist;
}
}
LibraryTester.java
import java.util.Date;
public class LibraryTester {
public static void main (String [] args){
Book book1 = new Book("Book1", new Date(), true);
Book book2 = new Book("Book2", new Date(), true);
Book book3 = new Book("Book3", new Date(), false);
//Adding Books to the library
Library library = new Library();
library.getBooklist().add(book1);
library.getBooklist().add(book2);
library.getBooklist().add(book3);
//renew
library.renew(book1);
library.getBooksDueOn(new Date());
System.out.println("Checking over due for book1 :"+library.overdue(book1, new Date()));
System.out.println("Checking over due for book2 :"+library.overdue(book2, new Date()));
library.checkeOutBooks();
}
}
Sample output:
Due Date is renewed as Sun Mar 12 03:47:19 IST 2017 for Book1
Books Due on date Sun Feb 26 03:47:19 IST 2017 are :
[[Name : Book2, Due : Sun Feb 26 03:47:19 IST 2017]]
Checking over due for book1 :false
Checking over due for book2 :true
Checked out books are :
[[Name : Book2, Due : Sun Feb 26 03:47:19 IST 2017, Name : Book1, Due : Sun Mar 12 03:47:19 IST 2017]]