Random Test - Part 1,2,3
import java.util.Random;
import java.lang.Thread;
public class Book{
private String titlle;
private int bookId;
private static int BookCounter = 0;
private long startTime;
public Book(String title){
titlle = title;
BookCounter++;
uniqueBookId();
startTime = System.nanoTime();
}
public String toString(){
return "Booktitle: " + titlle + " Id: " + bookId + " Super: " + super.toString();
}
public void testBook(){
System.out.println(this);
}
private int uniqueBookId(){
Random randid = new Random();
bookId = randid.nextInt(9999);
return bookId;
}
public static int getBookCount(){
return BookCounter;
}
public long getshelfLife()
{
return System.nanoTime() - startTime; // difference from start to current
}
public static void main(String[] args){
System.out.println("Bookcount: " + Book.getBookCount());
Book book1 = new Book("Book1");
Book book2 = new Book("Book2");
System.out.println(book1);
System.out.println(book2);
System.out.println("Bookcount: " + Book.getBookCount());
// testing for book 1
try{
Thread.sleep(5000); // milliseconds
System.out.println("Book1 shelflife: " + book1.getshelfLife());
}
catch (Exception e) {
// catching the exception if there is an interruption
System.out.println(e);
}
Book[] bookss = { // 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 : bookss) { // for each syntax to iterate over array
System.out.println(book); // same as book.toString()
}
System.out.println("Libary Book Count: " + Book.getBookCount());
//------------------------------------------------------------------------------
String [][] books = {
{ "e=MC^2 a Biography",
"Pan Books (January 1, 2001)"}, // row 0
{ "The Practice of Programming",
"Addison-Wesley Professional Computing" } // row 1
};
TextBook[] txtbooks= new TextBook[books.length];
for (int k =0; k<books.length; k++)
{
txtbooks[k] = new TextBook(books[k][0]);
txtbooks[k].setPublisher(books[k][1]);
try{
Thread.sleep(5000);
System.out.println("------Textbook------");
System.out.println(txtbooks[k]);
System.out.println("Publisher : " + txtbooks[k].getPublisher());
System.out.println("shelflife textbook: " + txtbooks[k].getshelfLife());
System.out.println("textbook expires: " + txtbooks[k].shelfLifeExpiry()); // only for textbook
}
catch (Exception e) {
// catching the exception
System.out.println(e);
}
}
//---------------------------------------------------------------------------------------
String [][] novelbooks = {
{ "novel1 ",
"authorofnovel1"}, // row 0
{ "novel2",
"authorofnovel2" } // row 1
};
Novel[] novbooks= new Novel[novelbooks.length] ;;
for (int k =0; k<novelbooks.length; k++)
{
novbooks[k] = new Novel(novelbooks[k][0]);
novbooks[k].setAuthor(novelbooks[k][1]);
}
for (int i =0; i<novbooks.length; i++)
{
System.out.println("------Novel------");
System.out.println(novbooks[i]);
System.out.println("Author: " + novbooks[i].getAuthor());
System.out.println("Expiry: " + novbooks[i].shelfLifeExpiry(4));
}
}
}
Book.main(null);
public class Novel extends Book{
private String author;
private final int SHELFLIFEEXPIRY = 3;
public Novel(String title){
super(title);
}
public void setAuthor(String authorName){
author = authorName;
}
public String getAuthor(){
return author;
}
public boolean shelfLifeExpiry(int returnstamp){
if (returnstamp < SHELFLIFEEXPIRY){
return false;
}
return true;
}
}
public class TextBook extends Book{
private String publishingcomp;
private final long SHELFLIFEEXPIRY = 3000;
public TextBook(String title){
super(title);
}
public void setPublisher(String publisher){
publishingcomp = publisher;
}
public String getPublisher(){
return publishingcomp;
}
public boolean shelfLifeExpiry(){
if (getshelfLife() < SHELFLIFEEXPIRY){
return false; // returns that it is not expired
}
return true;
}
}