// Part 1
public class Book {
private String title;
private int id;
private static int nextId = 1;
public static int bookCount = 0;
public Book(String title) {
this.title = title;
this.id = nextId;
nextId++;
bookCount++;
}
// Getter and setter for title and id
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return this.id + " " + this.title;
}
public static void setNextId(int id) {
Book.nextId = id;
}
// Other fields, constructors, and methods for the Book class
}
public class Novel extends Book {
// fields, constructors, and methods for the Novel class
public Novel(String title) {
super(title);
}
}
public class Textbook extends Book {
// fields, constructors, and methods for the Textbook class
public Textbook(String title) {
super(title);
}
}
public class BookTester {
public static void main(String[] args) {
Book.bookCount = 0;
Book.setNextId(1);
// Test the Book class
Book book1 = new Book("White Fang");
Book book2 = new Book("Uncle Tom's Cabin");
Book book3 = new Book("Percy Jackson");
Book book4 = new Book("Harry Potter");
Book book5 = new Book("To Kill a Mockingbird");
// Test the getTitle method
String title1 = book1.getTitle();
String title2 = book2.getTitle();
String title3 = book3.getTitle();
String title4 = book4.getTitle();
String title5 = book5.getTitle();
System.out.println(book1.toString());
System.out.println(book2.toString());
System.out.println(book3.toString());
System.out.println(book4.toString());
System.out.println(book5.toString());
// Test the bookCount field
System.out.println("Book count: " + book1.bookCount);
}
}
BookTester.main(null);
// Part 2
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
class Book {
private String title;
private String id;
private LocalDate date;
private int checkoutCount;
public static int bookCount = 0;
public Book(String title) {
this.title = title;
this.id = generateUniqueId();
this.date = LocalDate.now();
bookCount++;
}
public String getTitle() {
return title;
}
public String getId() {
return id;
}
public LocalDate getDate() {
return date;
}
public int getCheckoutCount() {
return checkoutCount;
}
public void checkout() {
checkoutCount++;
}
public boolean shouldBeRemoved() {
return checkoutCount >= 3;
}
public static String generateUniqueId() {
return String.valueOf(System.currentTimeMillis());
}
public int getShelfLife() {
return LocalDate.now().compareTo(date);
}
}
class Novel extends Book {
private String author;
private int numCheckouts = 0;
private Map<Integer, Integer> checkoutHistory = new HashMap<>();
private int numCheckoutsToExpire = 5;
public Novel(String title, String author) {
super(title);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public void checkout() {
super.checkout();
numCheckouts++;
checkoutHistory.put(numCheckouts, getShelfLife());
}
public boolean shouldBeRemoved() {
int sum = 0;
int count = 0;
for (int i = 1; i <= numCheckouts; i++) {
if (checkoutHistory.containsKey(i)) {
sum += checkoutHistory.get(i);
count++;
}
}
int averageShelfLife = sum / count;
return numCheckouts >= numCheckoutsToExpire && averageShelfLife < 30;
}
}
class Textbook extends Book {
private String publisher;
private int shelfLife = 365;
public Textbook(String title, String publisher) {
super(title);
this.publisher = publisher;
}
public String getPublisher() {
return publisher;
}
public void SetPublisher(String publisher) {
this.publisher = publisher;
}
public int getShelfLife() {
return shelfLife;
}
}
public class BookTester {
public static void main(String[] args) {
Novel novel = new Novel("The Great Gatsby", "F. Scott Fitzgerald");
Textbook textbook = new Textbook("Introduction to Java", "Pearson");
System.out.println("Novel title: " + novel.getTitle());
System.out.println("Novel author: " + novel.getAuthor());
System.out.println("Textbook title: " + textbook.getTitle());
System.out.println("Textbook publisher: " + textbook.getPublisher());
novel.checkout();
novel.checkout();
novel.checkout();
textbook.checkout();
textbook.checkout();
System.out.println("Novel shelf life: " + novel.getShelfLife());
System.out.println("Textbook shelf life: " + textbook.getShelfLife());
System.out.println("Novel should be removed: " + novel.shouldBeRemoved());
System.out.println("Textbook should be removed: " + textbook.shouldBeRemoved());
}
}
BookTester.main(null);