import java.util.*;
public class Book {
protected static ArrayList<Book> library = new ArrayList<Book>();
protected int id;
protected static int bookCount = 0;
protected String title;
protected long shelfLife;
protected long time;
public static String getLibrary() {
return library.toString();
}
public boolean getExpired() {
boolean expired = this.getTimeLeft() <= 0;
// if (expired) {
// this.library.remove()
// }
return expired;
}
public long getShelfLife() {
return this.shelfLife;
}
public long getTime() {
return this.time;
}
public long getTimeLeft() {
return this.time + this.shelfLife - System.nanoTime();
}
public Book(String title) {
this.title = title;
this.id = this.bookCount;
this.bookCount++;
this.shelfLife = 0;
}
public String toString() {
return "{title: " + title + "}";
}
public static int getBookCount() {
return bookCount;
}
public static void tester() {
System.out.println("Libary Book Count: " + Book.getBookCount()); // Notice how this method exist and works before any books are created
Book[] books = { // Use Array iniitialization to add book
new Book("Barron's Computer Science \"A\""), // Set a new Book object as array element.
new Book("Angels and Demons"),
new Book("Lion, Witch, and a Wardrobe")
};
for (Book book : books) { // Use Foreach syntax to iterate over array
System.out.println(book); // this is same as book.toString()
}
System.out.println("Libary Book Count: " + Book.getBookCount());
}
}
Book.tester();
public class Novel extends Book {
private String author;
public void addShelfLife() {
super.shelfLife += (long)1000000000*10;
}
public Novel(String title) {
super(title);
this.time = System.nanoTime();
this.addShelfLife();
super.library.add(this);
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public void returnBook() {
this.addShelfLife();
}
public static void testerN() throws InterruptedException{
Novel novel1 = new Novel("novel1");
Novel novel2 = new Novel("novel2");
System.out.println(novel1.toString());
System.out.println(Novel.getBookCount());
novel1.setAuthor("me");
System.out.println(novel1.getAuthor());
System.out.println(novel1.getShelfLife());
System.out.println(novel1.getTime());
System.out.println(novel1.getTimeLeft());
System.out.println(novel1.getExpired());
Thread.sleep(5000);
System.out.println(novel1.getTimeLeft());
System.out.println(novel1.getExpired());
novel2.returnBook();
Thread.sleep(5500);
System.out.println(novel1.getTimeLeft());
System.out.println(novel1.getExpired());
System.out.println(novel2.getTimeLeft());
System.out.println(novel2.getExpired());
System.out.println(Novel.getLibrary());
}
}
Novel.testerN();
public class Textbook extends Book {
private String publishingCompany;
public void addShelfLife() {
super.shelfLife += (long)1000000000*10;
}
public Textbook(String title) {
super(title);
this.time = System.nanoTime();
this.addShelfLife();
super.library.add(this);
}
public String getPublishingCompany() {
return this.publishingCompany;
}
public void setPublishingCompany(String publishingCompany) {
this.publishingCompany = publishingCompany;
}
public static void testerT() throws InterruptedException {
Novel tb1 = new Novel("tb1");
Novel tb2 = new Novel("tb2");
System.out.println(tb1.toString());
System.out.println(tb1.getBookCount());
tb1.setAuthor("me");
System.out.println(tb1.getAuthor());
System.out.println(tb1.getShelfLife());
System.out.println(tb1.getTime());
System.out.println(tb1.getTimeLeft());
System.out.println(tb1.getExpired());
Thread.sleep(5000);
System.out.println(tb1.getTimeLeft());
System.out.println(tb1.getExpired());
Thread.sleep(5500);
System.out.println(tb1.getTimeLeft());
System.out.println(tb1.getExpired());
System.out.println(Novel.getLibrary());
}
}
Textbook.testerT();