Surprise Test Post
Surprise test blog post for AP CSA
import java.util.*;
import java.lang.Thread;
public class Book {
public int id;
public static int newID = 0;
public String title;
public int pages;
public long time;
public long expirationTime;
public int stampRenews;
public Book(String titleInput, int pageInput) {
title = titleInput;
pages = pageInput;
id = newID;
time = System.currentTimeMillis();
expirationTime = 5000;
newID++;
}
public String getTitle() {
return title;
}
public int getPage() {
return pages;
}
public int getID() {
return id;
}
public long getTime() {
return time;
}
public long getExpirationTime() {
return expirationTime;
}
public void setExpirationTime(int stampRenewsInput) {
this.expirationTime += 5000 * (stampRenewsInput/3);
}
public void setStampRenews(int stampRenewsInput) {
this.stampRenews = stampRenewsInput;
}
public int getStampRenews() {
return stampRenews;
}
public String getAuthor() { return ""; }
public void setAuthor(String authorInput) {}
public String getPublishingCompany() {return "";}
public void setPublishingCompany(String publishingCompanyInput) {}
}
public class Novel extends Book {
public String author;
public Novel(String title, int page, String authorInput) {
super(title, page);
author = authorInput;
}
public String getAuthor() {
return author;
}
public void setAuthor(String authorInput) {
this.author = authorInput;
}
}
public class Textbook extends Book {
public String publishingCompany;
public Textbook(String title, int page, String publishingCompanyInput) {
super(title, page);
publishingCompany = publishingCompanyInput;
}
public String getPublishingCompany() {
return publishingCompany;
}
public void setPublishingCompany(String publishingCompanyInput) {
this.publishingCompany = publishingCompanyInput;
}
}
public class Main {
public void toString(ArrayList<Book> bookList) {
String output = "[";
for (int i = 0; i < bookList.size(); i++) {
if (i == bookList.size() - 1) {
output += "{" + bookList.get(i).getTitle() + "-" + bookList.get(i).getPage() + "-" + bookList.get(i).getID() + "-" + bookList.get(i).getTime() + "-" + bookList.get(i).getExpirationTime() + "}";
} else {
output += "{" + bookList.get(i).getTitle() + "-" + bookList.get(i).getPage() + "-" + bookList.get(i).getID() + "-" + bookList.get(i).getTime() + "-" + bookList.get(i).getExpirationTime() + "},";
}
}
output += "]";
System.out.println(output);
}
public int getBookCount(ArrayList<Book> bookList) {
return bookList.size();
}
public long getShelfLife(Book bookInQuestion) {
long shelfLife = System.currentTimeMillis() - bookInQuestion.getTime();
return shelfLife;
}
public void checkIfExpired(Book bookInQuestion, long shelfLife) {
boolean expired = false;
if (shelfLife > bookInQuestion.getExpirationTime()) {
expired = true;
}
System.out.println("Expired: " + expired);
}
public static void main(String[] args) throws InterruptedException {
System.out.println("Part 1:");
System.out.println("-------");
ArrayList<Book> bookList = new ArrayList<Book>();
Book book1 = new Book("Maze Runner", 500);
Book book2 = new Book("Hunger Games", 700);
Book book3 = new Book("Fahrenheit 451", 800);
bookList.add(book1);
bookList.add(book2);
bookList.add(book3);
Main myObj = new Main();
System.out.println("All books in library:");
System.out.println(book1.getTitle());
System.out.println(book2.getTitle());
System.out.println(book3.getTitle());
System.out.println("");
System.out.println("Number of books in library:");
System.out.println(myObj.getBookCount(bookList));
System.out.println("");
System.out.println("toString():");
myObj.toString(bookList);
System.out.println();
System.out.println("Part 2:");
System.out.println("-------");
bookList.clear();
Book novel1 = new Novel("Computer Science Fundamentals", 800, "Mark Wahlberg");
Book textbook1 = new Textbook("How to be good", 700, "Generic Publishing Company");
bookList.add(novel1);
bookList.add(textbook1);
System.out.println("Author of novel: " + novel1.getAuthor());
textbook1.setPublishingCompany("Specific Publishing Company");
System.out.println("Publishing company of textbook: " + textbook1.getPublishingCompany());
System.out.println("Date and time novel was added to library: " + novel1.getTime());
myObj.toString(bookList);
System.out.println();
System.out.println("Part 3:");
System.out.println("-------");
Book newBook = new Book("Baron's Computer Science Adventures", 500);
bookList.add(newBook);
myObj.toString(bookList);
Thread.sleep(3500);
System.out.println(myObj.getShelfLife(newBook));
myObj.checkIfExpired(newBook, myObj.getShelfLife(newBook));
Thread.sleep(1500);
System.out.println(myObj.getShelfLife(newBook));
myObj.checkIfExpired(newBook, myObj.getShelfLife(newBook));
Book newBookAgain = new Book("Generic Book", 700);
bookList.add(newBookAgain);
newBookAgain.setExpirationTime(3);
Thread.sleep(6000);
System.out.println(myObj.getShelfLife(newBookAgain));
myObj.checkIfExpired(newBookAgain, myObj.getShelfLife(newBookAgain));
}
}
Main.main(null);