Pop Quiz Test
import java.util.*;
public class Book {
// argument constructor title?
public String title;
// unique id
public String id;
private static long idCounter = 0;
// book count
public static int bookCount;
public Book (){
this.title = null;
}
public Book (String title){
this.title = title;
this.id = createID();
}
public static synchronized String createID() {
return String.valueOf(idCounter++);
}
public String toString() {
return("Book Title: "+title+" \nBook ID: "+id+"\n");
}
}
class Novel extends Book {
public Novel (String title){
this.title = title;
this.id = createID();
}
}
class Textbook extends Book {
public Textbook (String title){
this.title = title;
this.id = createID();
}
}
// driver class
public class Tester
{
public static void main(String args[])
{
Book first = new Book("Coding Stuff");
Novel second = new Novel("Fiction");
Textbook third = new Textbook("Nonfiction");
Book[] books = { // Use Array initialization to add book
new Book("Barron's Computer Science"), // 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()
}
Book.bookCount = books.length;
System.out.println("Library Book Count: " + Book.bookCount);
}
}
Tester.main(null);
import java.util.*;
public class Book {
// argument constructor title?
public String title;
// unique id
public String id;
private static long idCounter = 0;
// book count
public static int bookCount;
// shelf life
public static int shelfLife;
// age that determines shelf life
public static int age;
// # of checkouts
public int checkouts;
private static ArrayList<Book> library = new ArrayList<Book>(); // Library as static ArrayList
public static final long YEAR = 1000; // year in milliseconds, final as it does not change
public Book (){
this.title = null;
}
public Book (String title){
this.title = title;
this.id = createID();
Book.library.add(this); // library size used to set
this.shelfLife = 0;
}
public static synchronized String createID() {
return String.valueOf(idCounter++);
}
public String toString() {
return("Book Title: "+title+" \nBook ID: "+id+" \nBook Shelf Life: "+shelfLife);
}
// checking whether its still fit to be on shelves
public static boolean checkShelfLife() {
if (shelfLife < 50) {
return false; // book's shelf life is expired
} else {
return true; // book is still fine
}
}
public static int ageShelfLife() {
if (shelfLife > 50) {
shelfLife -= 20;
}
return (shelfLife);
}
/* size is used as unique id, deletes can't occur in this implementation */
public static int getBookCount() { // Part 1.4, Create a public getter that has Book Count
return Book.library.size();
}
public static List<Book> getLibrary() {
return Collections.unmodifiableList(Book.library); // ArrayList should not be changed outside of this Book class
}
/* prints "active" books in library */
public static void printLibrary() {
// Here are save books, but where could this process go if I want a library of books across subclasses
System.out.println("Active books");
for (Book book: Book.library) {
if (book.checkShelfLife()) // Active book check
System.out.println(book+"\n");
}
}
}
class Novel extends Book {
public String author;
public Novel (String title){
super(title);
this.id = createID();
}
public Novel (String title, String author, int checkouts){
super(title);
this.id = createID();
this.author = author;
this.checkouts = checkouts;
}
@Override
public String toString() {
return (super.toString()+
"\nAuthor: "+author);
}
public int checkLife() {
shelfLife = 250 - (checkouts * 2);
return (shelfLife);
}
}
class Textbook extends Book {
public String publisher;
public Textbook (String title){
super(title);
this.id = createID();
}
public Textbook (String title, String publisher, int age){
super(title);
this.id = createID();
this.publisher = publisher;
this.age = age;
}
@Override
public String toString() {
return (super.toString()+
"\nPublisher: "+publisher);
}
public int checkLife() {
shelfLife = 200 - (age * 3);
return (shelfLife);
}
}
// driver class
public class Tester
{
public static void main(String args[])
{
Book first = new Book("Coding Stuff");
Novel second = new Novel("Fiction", "Higgins", 77);
Textbook third = new Textbook("Nonfiction", "Willoughby", 39);
System.out.println("Library Book Count: " + Book.getBookCount()); // Notice how this method exist and works before any books are created
// This shows intializing Book Array with new Book objects
Book[] books = { // Uses an Array to store the creation of 3 books
new Book("Barron's Computer Science \"A\""), // Set a new Book object as array element.
new Book("Angels and Demons")
};
Novel[] novels = { // Same technique as in Book tester
new Novel("The Da Vinci Code", "Dan Brown", 63),
new Novel("Pride and Prejudice", "Jane Austen", 44)
};
String [][] bookss = {
{ "e=MC^2 a Biography",
"Pan Books"}, // row 0
{ "The Practice of Programming",
"Addison-Wesley Professional Computing" } // row 1
};
// Iterates through new Book objects
for (Book book : books) { // Use Foreach syntax to iterate over array
System.out.println(book); // this is same as book.toString()
}
System.out.println("Library Book Count: " + Book.getBookCount()+"\n");
System.out.println(first.toString()+"\n");
System.out.println(second.toString());
System.out.println("Shelf Life: "+second.checkLife());
System.out.println("It stay come on the shelf: "+second.checkShelfLife()+"\n");
System.out.println(third.toString());
System.out.println("Shelf Life: "+third.checkLife());
System.out.println("It stay come on the shelf: "+third.checkShelfLife()+"\n");
}
}
Tester.main(null);
// Library Simulation
public class LibraryManager
{
// Part 3.1, Build a Tester Method that does a Simulation.
public static void main(String[] args) throws InterruptedException {
int years = 5; // Simulation years
Novel second = new Novel("Fiction", "Higgins", 77);
Textbook third = new Textbook("Nonfiction", "Willoughby", 39);
// Sleep in loop creates delay for simulation
for (int i = 0; i < years; i++) {
System.out.println("Year: " + i);
Book.printLibrary();
System.out.println();
Thread.sleep(Book.YEAR); // Part 3.4, Use a sleep in Java to assist with simulation
// Loop through books in Library
for (Book book : Book.getLibrary()) {
if (book instanceof Novel){
second.checkLife();
book.ageShelfLife();
} else if (book instanceof Textbook) {
third.checkLife();
book.ageShelfLife();
} else {
book.ageShelfLife();
}
}
}
}
}
LibraryManager.main(null);