Super Cool Random Test!
So Awesome!
public class Book {
private String title;
private int id;
private static int bookCount;
private long time;
// private long currentTimeMillis;
public Book(String title)
{
this.title = title;
this.id = (int)(Math.random()*100);
bookCount = bookCount;
time = System.currentTimeMillis();
}
public String getTitle(){
return title;
}
public static int getBookCount(){
return bookCount;
}
public static void setBookCount(int bookCount){
bookCount++;
}
public int getBookID(){
return id;
}
public long shelfLife() {
return (System.currentTimeMillis() - time)/1000;
}
public boolean isExpired() {
if(shelfLife()/1000 >= 3) {
return true;
}
return false;
}
public String toString() {
return ("Books: " + "ID: " + id + ", Title: " + title + ", " +"Shelf Life: " + shelfLife() + " year(s)" + ", " + "Expired? " + isExpired());
}}
public class Textbook extends Book{
private String author;
private double price;
private String publication;
private int edition;
private long time;
public Textbook(String title, String publication, double price, int edition)
{
super(title);
this.price = price;
this.publication = publication;
this.edition = edition;
this.time = System.currentTimeMillis();
}
public double getPrice(){
return price;
}
public long shelfLife() {
return (System.currentTimeMillis() - time)/1000;
}
public String getPublication() {
return publication;
}
public boolean isExpired() {
if(shelfLife()/1000 >= 3) {
return true;
}
return false;
}
public int getEdition(){
return edition;
}
public String toString()
{
return ("Textbooks: " + "ID: " + super.getBookID() + ", Title: " + super.getTitle() + ", " + shelfLife() + " years" + ", " + "Expired? " + isExpired() + ", " + "price: " + price + ", " + "publication: " + getPublication() + ", " + "edition: "+ edition);
}
}
public class Novel extends Book{
private double price;
private int shelfLife;
private int checkOuts;
private long time;
public Novel(int checkOuts, String title, String author, double price)
{
super(title);
this.price = price;
this.shelfLife = 1;
this.checkOuts = checkOuts;
this.time = System.currentTimeMillis();
}
public long shelfLife() {
return (System.currentTimeMillis() - time)/1000;
}
public long checkPopularity(int checkOuts) {
if (checkOuts > 10) {
return this.shelfLife + (System.currentTimeMillis() - time) / 1000;
}//adds 1yr to shelflife
return this.shelfLife;
}
public boolean isExpired() {
if(shelfLife()/1000 >= 3) {
return true;
}
return false;
}
public double getPrice(){
return price;
}
public String toString()
{
return ("Novels: " + "ID: " + super.getBookID() + ", Title: " + super.getTitle() + ", " + checkPopularity(checkOuts) + " years" + ", " + "Expired? " + isExpired() + ", " + "price: " + price);
}
}
public class myClass{
public static void main(String[] args ) {
// create an object
ArrayList<Book> books = new ArrayList<>();
Book Book1 = new Book("The Great Gatsby");
Book Book2 = new Book("Candide");
Book Book3 = new Book("Lolita");
Book Book4 = new Book("To Kill a Mockingbird");
Book Book5 = new Book("1984");
Textbook Textbook1 = new Textbook("Myers 8th Edition Psychology", "Myers", 90, 8);
Novel novel1 = new Novel(47, "Animal Farm", "Orwell", 200.0);
try {
Thread.sleep(3000); //waits 3s
} catch (InterruptedException e) { //handle any exceptions that
//may be thrown if the sleep is interrupted before it completes
e.printStackTrace(); //used to print the stack trace of an exception
//When an exception occurs, shows the sequence of method calls that led up to the exception
}
books.add(Book1);
books.add(Book2);
books.add(Book3);
books.add(Book4);
books.add(Book5);
books.add(Textbook1);
books.add(novel1);
int bookCount = 0;
for(int i = 0; i <books.size(); i++){
bookCount++;
System.out.println(books.get(i).toString());
// Thread.sleep() method can throw an InterruptedException if the thread is interrupted while it is sleeping or waiting
//pauses execution of thread for 1000 milliseconds
}
System.out.println("Total # of books: " + bookCount);
}}
myClass.main(null);