Close Book - Class Book (Part 1)

  • define 1 argument constructor for title
  • define toString method for title, and a tester method
  • generate unique id for class
  • create a public getter that has Book Count
  • define tester method that initializes at least 2 books, outputs title, and provides a count of books in library

Open Book - Extended Classes (Part 2)

Try to use alternate forms of loops and techniques for construction

  • Ensure Novel and Textbook run the Constructor from Book.
  • Create instance variables unique to Novel has Author, Textbook has publishing company. New items are not required by Constructor.
  • Make Getters and Setters for all new items. You can add your own.
  • Add a time when book entered the library. This should be same for Parent and Subclasses.
  • Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.
  • Define tester method to test all items.
import java.time.LocalDateTime;

// Book class 
public class Book {
    private static int bookCount = 0;


    protected final LocalDateTime entryTime;
    private final String id;
    private String title;

    public Book(String title) {
        this.title = title;
        this.id = generateUniqueId();
        this.entryTime = LocalDateTime.now();
        bookCount++;
    }

    // initializing a unique id using LocalDateTime.now
    // defines a generateUniqueId() method to generate a unique ID string for each Book 
    private String generateUniqueId() {
        return LocalDateTime.now().toString().replaceAll("[^\\d]", "");
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    // keeps track of the number of Books created using a static bookCount field and uses getBookCount() method to return count
    public static int getBookCount() {
        return bookCount;
    }

    // toString() method which returns a string of a Book's id and title.
    @Override
    public String toString() {
        return "Book{" +
                "entryTime=" + entryTime +
                ", id='" + id + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}

// Novel class that extends Book and adds an author instance variable with getters and setters
class Novel extends Book {
    private String author;

    public Novel(String title) {
        super(title);
    }

    // get author 
    public String getAuthor() {
        return author;
    }

    // set author
    public void setAuthor(String author) {
        this.author = author;
    }

    // toString method which return string of Novel's id, title, and author
    @Override
    public String toString() {
        return "Novel{" +
                "entryTime=" + super.entryTime +
                ", id='" + super.getId() + '\'' +
                ", title='" + super.getTitle() + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

// Textbook class that extends Book and adds a publisher instance variable with getters and setters.
class Textbook extends Book {
    private String publisher;

    public Textbook(String title) {
        super(title);
    }

    // get publisher
    public String getPublisher() {
        return publisher;
    }

    // set publisher
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    // toString method which returns string of Textbook's id, title, and publisher
    @Override
    public String toString() {
        return "Textbook{" +
                "entryTime=" + super.entryTime +
                ", id='" + super.getId() + '\'' +
                ", title='" + super.getTitle() + '\'' +
                ", publisher='" + publisher + '\'' +
                '}';
    }
}

// Tester Method
public class BookTester {
    public static void main(String[] args) {
        Book book = new Book("The Great Gatsby");
        Novel novel = new Novel("To Kill a Mockingbird");
        novel.setAuthor("Harper Lee");
        Textbook textbook = new Textbook("Calculus");
        textbook.setPublisher("Pearson");

        System.out.println(book);
        System.out.println(novel);
        System.out.println(textbook);

        System.out.println("Book count: " + Book.getBookCount());
    }
}

BookTester.main(null);
Book{entryTime=2023-04-21T09:28:36.320806, id='20230421092836319858', title='The Great Gatsby'}
Novel{entryTime=2023-04-21T09:28:36.321081, id='20230421092836320825', title='To Kill a Mockingbird', author='Harper Lee'}
Textbook{entryTime=2023-04-21T09:28:36.321111, id='20230421092836321094', title='Calculus', publisher='Pearson'}
Book count: 3