Random Test
import java.util.*;
public class Book {
private String title;
private static int bookCount = 0;
private int id;
private long timeOfCreation;
public Book(String title) {
this.title = title;
this.id = bookCount;
this.timeOfCreation = System.nanoTime();
bookCount++;
}
public String toString() {
return "title: " + this.title + ", id: " + id;
}
public int getBookCount() {
return bookCount;
}
public String getTitle() {
return this.title;
}
public double getShelfLife() {
return System.nanoTime() - this.timeOfCreation;
}
public long getTimeOfCreation() {
return this.timeOfCreation;
}
public boolean isExpired() {
return false;
}
public static void main(String[] args) {
Book harryPotter = new Book("Harry Potter and the Sorcerers Stone");
Book lordOfRings = new Book("Lord of the Rings");
System.out.println(harryPotter);
System.out.println(lordOfRings);
System.out.println("book count: " + harryPotter.getBookCount());
System.out.println(lordOfRings.getShelfLife());
}
}
Book.main(null);
public class Novel extends Book {
private String author;
private int checkouts = 0;
private long timeRenewed;
public Novel(String title, String author) {
super(title);
this.author = author;
this.timeRenewed = super.getTimeOfCreation();
}
// override shelflife which averages a certain number of checkouts over shelf life
// simulate being checked out 3 times a year??
@Override
public double getShelfLife() {
return this.getTimeOfCreation() + 5*(int) Math.pow(10, 9);
}
public boolean isExpired() {
return this.getShelfLife() < System.nanoTime();
}
public void checkout() {
this.checkouts++;
if (this.checkouts == 3) {
this.timeRenewed = System.nanoTime();
this.checkouts = 0;
}
}
public int getCheckouts() {
return this.checkouts;
}
@Override
public String toString() {
return super.toString() + ", author: " + this.author;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public static void main(String[] args) {
Novel harryPotter = new Novel("Harry Potter and the Sorcerers Stone", "J.K. Rowling");
Novel lordOfRings = new Novel("Lord of the Rings", "J.R.R. Tolkien");
System.out.println(harryPotter);
System.out.println(lordOfRings);
harryPotter.checkout();
harryPotter.checkout();
harryPotter.checkout();
System.out.println("checkouts: " + harryPotter.getCheckouts());
System.out.println("is expired after 3 checkouts: " + harryPotter.isExpired());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
harryPotter.checkout();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
harryPotter.checkout();
System.out.println("is expired after 6 seconds and 2 checkouts: " + harryPotter.isExpired());
}
}
Novel.main(null);
public class Textbook extends Book {
private String publishingCompany;
public Textbook(String title, String publishingCompany) {
super(title);
this.publishingCompany = publishingCompany;
}
// fixed shelf life based on date/time of constructor
@Override
public double getShelfLife() {
return this.getTimeOfCreation() + 1*(int) Math.pow(10, 9);
}
public double timeCheckedOut() {
return System.nanoTime() - this.getTimeOfCreation();
}
public String getPublishingCompany() {
return this.publishingCompany;
}
public void setPublishingCompany(String publishingCompany) {
this.publishingCompany = publishingCompany;
}
public boolean isExpired() {
return System.nanoTime() > this.getShelfLife();
}
public String comeOffShelf() {
return "Title: " + super.getTitle() + ", isExpired: " + this.isExpired();
}
@Override
public String toString() {
return super.toString() + ", publishing company: " + this.publishingCompany;
}
public static void main(String[] args) {
Textbook harryPotter = new Textbook("Harry Potter and the Sorcerers Stone", "Scholastic");
Textbook lordOfRings = new Textbook("Lord of the Rings", "Houghton Mifflin Harcourt");
System.out.println(harryPotter);
System.out.println(lordOfRings);
System.out.println("book count: " + harryPotter.getBookCount());
System.out.println("is expired after 0 seconds: " + lordOfRings.isExpired());
System.out.println(lordOfRings.comeOffShelf());
// sleep for 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("is expired after 1 seconds: " + lordOfRings.isExpired());
System.out.println(lordOfRings.comeOffShelf());
}
}
Textbook.main(null);
public class Library {
private ArrayList<Book> books = new ArrayList<Book>();
public void addBook(Book book) {
this.books.add(book);
}
public void removeBook(Book book) {
this.books.remove(book);
}
public ArrayList<Book> getBooks() {
return this.books;
}
public String comeOffShelf(Book book) {
return "Title: " + book.getTitle() + ", isExpired: " + book.isExpired();
}
public static void main(String[] args) {
Library library = new Library();
library.addBook(new Novel("Harry Potter and the Sorcerers Stone", "J.K. Rowling"));
library.addBook(new Novel("Lord of the Rings", "J.R.R. Tolkien"));
library.addBook(new Textbook("AP CSA Textbook", "Scholastic"));
library.addBook(new Textbook("The American Pageant", "Houghton Mifflin Harcourt"));
library.addBook(new Book("The Great Gatsby"));
for (Book book : library.getBooks()) {
System.out.println(book);
}
}
}
Library.main(null);