Class Book (Part 1) Close Book
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Book {
private static int bookCount; // defining the static integer that has the count of the books
public String bookTitle; // defining the string that stores the title of the books
private static long id; // defining the static variable that has the unique id, static so that it stays in the memory and shared with all the objects
private LocalTime timeEntered;
private static ArrayList<Book> library = new ArrayList<Book>();
public Book(String bookTitle) { // 1 argument constructor that passes in the book title
this.bookTitle = bookTitle;
bookCount++; // add to the book counter each time a Book object is created
}
public static int getCount(){ // public getter for the book count
return bookCount;
}
public long nextId() { // unique ids using a counter for each book
return id++;
}
public String toString() { // toString method defined with Id and Title
return "Id: " + nextId() + " | Title: " + this.bookTitle + " Time Entered: " + getTime();
}
public LocalTime getTime() {
timeEntered = LocalTime.now();
return timeEntered;
}
public void shelfLife() {
}
public static void main(String[] args) { // tester method defined
Book book1 = new Book("Harry Potter");
Book book2 = new Book("The Great Gatsby");
Book book3 = new Book("To Kill a Mockingbird");
Book book4 = new Book("The Hunger Games");
Book books[] = {book1, book2, book3, book4};
System.out.println("Books in the Library: "); // array to print each book id and title
for( Book book : books) {
System.out.println(book);
}
System.out.println();
System.out.println("Total Number of Books: "); // print for counter
System.out.println(getCount());
}
}
Book.main(null);
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Novel extends Book {
private String author;
private int year;
private String genre;
private LocalTime timeEntered;
public Novel(String bookTitle, String author) {
super(bookTitle);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String newAuthor) {
this.author = newAuthor;
}
public int getYear() {
return year;
}
public void setYear(int newYear) {
this.year = newYear;
}
public String getGenre() {
return genre;
}
public void setGenre(String newGenre) {
this.genre = newGenre;
}
public LocalTime getTime() {
timeEntered = LocalTime.now();
return timeEntered;
}
public String toString() { // toString method defined with Id and Title
return "Id: " + nextId() + " | Title: " + this.bookTitle + " Author: " + this.author + " Genre: " + this.genre + " Year: " + this.year + " Time Entered: " + getTime();
}
public static void main(String[] args) { // tester method defined
Novel novel1 = new Novel("The Giver", "Lois Lowry");
novel1.setYear(1993);
Novel novel2 = new Novel("Of Mice and Men", "John Steinbeck");
novel2.setYear(1993);
Novel novel3 = new Novel("Romeo and Juliet", "William Shakespeare");
novel3.setYear(1597);
Novel novel4 = new Novel("The Catcher in the Rye", "J. D. Salinger");
novel4.setYear(1951);
Novel novels[] = {novel1, novel2, novel3, novel4};
System.out.println("Novels in the Library: "); // array to print each book id and title
for( Novel novel : novels) {
novel.setGenre("Fiction");
System.out.println(novel);
}
System.out.println();
System.out.println("Total Number of Books: "); // print for counter
System.out.println(getCount());
System.out.println();
System.out.println("Testing Getters: ");
System.out.println(novel2.getAuthor());
System.out.println(novel1.getYear());
System.out.println(novel2.getGenre());
System.out.println(novel2.getTime());
}
}
Novel.main(null);
Open book
public class Textbook extends Book {
private String publisher;
private int year;
private String level;
public Textbook(String bookTitle, String publisher, String level) {
super(bookTitle);
this.publisher = publisher;
this.level = level;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String newPublish) {
publisher = newPublish;
}
public int getYear() {
return year;
}
public void setYear(int newYear) {
this.year = newYear;
}
public String getLevel() {
return level;
}
public void setLevel(String newLevel) {
level = newLevel;
}
public String toString() { // toString method defined with Id and Title
return "Id: " + nextId() + " | Title: " + this.bookTitle + " Publisher: " + this.publisher + " Level: " + this.level + " Year: " + this.year + " Time Entered: " + getTime();
}
public static void main(String[] args) { // tester method defined
Textbook text1 = new Textbook("U.S. History", "Publisher 1", "High School");
text1.setYear(2001);
Textbook text2 = new Textbook("World History", "Publisher 2", "Middle School");
text2.setYear(2009);
text2.setPublisher("Penguin Publisher");
Textbook textbooks[] = {text1, text2};
System.out.println("Textbooks in the Library: "); // array to print each book id and title
for( Textbook textbook : textbooks) {
System.out.println(textbook);
}
System.out.println();
System.out.println("Total Number of Books: "); // print for counter
System.out.println(getCount());
System.out.println();
System.out.println("Testing Getters: ");
System.out.println(text1.getLevel());
System.out.println(text2.getPublisher());
}
}
Textbook.main(null);
Simulation
1:15:22
public void drawLine(int n) {
for (int i = 1; i <= n; i++) {
System.out.print("*");
drawLine(n - 1);
n--;
}
}
drawLine(10);