Pop Test
import java.util.*
public class Book {
private String title;
private int id;
private static int bookCount = 0;
public Book(String title) {
this.title = title;
this.id = ++bookCount;
}
public static int getBookCount() {
return bookCount;
}
public String toString(){
return ("Book title: '" + this.title + "'. Book ID: " + this.id);
}
public static void main(String args[]) {
Book bookone = new Book("Learn Java");
Book booktwo = new Book("CSA");
System.out.println(bookone);
System.out.println(booktwo);
System.out.println(booktwo.getBookCount());
}
}
Book.main(null);
import java.time.LocalDateTime;
public class Book {
public String title;
public int id;
public static int bookCount = 0;
public static LocalDateTime entryTime;
public Book() {
this.title = null;
this.id = 0;
}
public Book(String title) {
this.title = title;
this.id = ++bookCount;
}
public static int getBookCount() {
return bookCount;
}
public String toString(){
return ("Book title: '" + this.title + "'. Book ID: " + this.id);
}
public static void main(String[] args) {
Book book1 = new Book("book1");
Book book2 = new Book("book2");
System.out.println(book1);
System.out.println(book2);
}
}
class Novel extends Book {
private String author;
Novel() {
super();
}
Novel(String title, String author) {
this.title = title;
this.author = author;
this.id = ++bookCount;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return ("Book title: '" + this.title + "'. Book ID: " + this.id + ". Author: " + this.author);
}
}
class Textbook extends Book {
public String publisher;
Textbook() {
super();
}
Textbook(String title, String publisher) {
this.title = title;
this.publisher = publisher;
this.id = ++bookCount;
}
public String getPublisher() {
return this.publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
@Override
public String toString() {
return ("Book title: '" + this.title + "'. Book ID: " + this.id + ". Publisher: " + this.publisher);
}
}
class Tester {
public static void main(String args[]) {
Book bookone = new Book("CSA Hacks");
Novel booktwo = new Novel("The Great Gatsby","F Scott Fitzgerald");
Textbook bookthree = new Textbook("American Pageant", "Publisher Company Inc.");
System.out.println(bookone);
System.out.println(booktwo);
System.out.println(bookthree);
System.out.println(bookone.getBookCount());
}
}
Tester.main(null);
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Book {
private String title;
private LocalDateTime entryTime;
public Book(String title) {
this.title = title;
this.entryTime = LocalDateTime.now();
}
public String getTitle() {
return title;
}
}
class Novel extends Book {
private String author;
private int returnStamps = 0;
public Novel(String title, String author) {
super(title);
}
public int getReturnStamps() {
return returnStamps;
}
public void addReturnStamp() {
returnStamps++;
}
public boolean isExpired() {
return returnStamps >= 3;
}
}
class TextBook extends Book {
private String publisher;
public TextBook(String title, String publisher) {
super(title);
}
public boolean isExpired() {
return getShelfLife() >= 3_000_000L;
}
}
public class LibrarySimulation {
private static final Random RANDOM = new Random();
public static void main(String[] args) throws InterruptedException {
List<Book> books = new ArrayList<>();
books.add(new Novel("Pride and Prejudice", "Jane Austen"));
books.add(new Novel("To Kill a Mockingbird", "Harper Lee"));
books.add(new TextBook("Introduction to Computer Science", "MIT Press"));
books.add(new TextBook("Calculus", "Pearson"));
System.out.println("Starting simulation...");
for (int i = 0; i < 4; i++) {
System.out.println("Day " + (i + 1) + ":");
for (Book book : books) {
if (book instanceof Novel) {
Novel novel = (Novel) book;
if (!novel.isExpired()) {
if (RANDOM.nextDouble() < 0.05) {
novel.addReturnStamp();
System.out.println(novel.getTitle() + " got a return stamp.");
} else {
System.out.println(novel.getTitle() + " is still on the shelf.");
}
} else {
System.out.println(novel.getTitle() + " has expired and needs to be removed.");
}
} else if (book instanceof TextBook) {
TextBook textbook = (TextBook) book;
if (!textbook.isExpired()) {
System.out.println(textbook.getTitle() + " is still on the shelf.");
} else {
System.out.println(textbook.getTitle() + " has expired and needs to be removed.");
}
}
}
System.out.println();
Thread.sleep(1000);
}
System.out.println("Simulation ended.");
}
}
LibrarySimulation.main(null)