4/20 - Test
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
Part 2
- Ensure Novel and Textbook run the Constructor from Book.
- Create instance variables unique to Novel has Author, Textbook has publishing company. These are not required in Constructor. Make sure there are getters and setters.
- Define a default method in Book that returns selfLife from date/time of constructor. Define shelfLife methods as needed in Textbook and Novel. A TextBook has a fixed shelf life based on the date/time of constructor. A Novel has a computed shelf life and is valided by averageing a certain number of Checkouts average over shelf life, use sensible data structure.
- Make a method to count time book is checkout
- Make a method to determine if book should come off of the shelf
- Define tester method to test items #1-5.
import java.util.Random;
import java.time.LocalTime;
import java.time.Duration;
/**
* Book.java
*
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Book.java
*
*/
public class Book
{
public String _title; // title of book
public static int _bookCount = 0; // book count
LocalTime _checkOutTime;
LocalTime _checkInTime;
/**
* Constructor for class
* PART 1
*
* @param title of book
*/
public Book(String title)
{
_title = title;
_bookCount++;
}
/**
* outputs title of book
* PART 2
*/
@Override
public String toString()
{
String desctription = _title;
return desctription;
}
/**
* getBook
*
* @return
*/
public int getBook()
{
return _bookCount;
}
/**
* getId
*
* PART 3
*
* @return random book key from the max number of the Integer class
* that is represented in 32 bits
*/
public int getId()
{
Random random = new Random();
return random.nextInt(Integer.MAX_VALUE);
}
/**
* checkOutBook
*
*/
public void checkOutBook()
{
_checkOutTime = LocalTime.now();
}
/**
* checkInBook
*
*/
public void checkInBook()
{
_checkInTime = LocalTime.now();
}
/**
* getCheckOutTime
*
* @return
*/
public long getCheckOutTime()
{
Duration d = Duration.between(_checkOutTime, _checkInTime);
return d.getNano();
}
/**
* isShelflifeOver
*
* @return
*/
public boolean isShelflifeOver()
{
return false;
// to be implemented in 2 subclasses
}
}
import java.time.Duration;
/**
* Novel.java
*
* Description
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Novel.java
*
*/
public class Novel extends Book
{
public String _author;
public long _checkOutAvgNano; // averge time in nanoseconds
public int _numCheckouts = 0;
public long _checkOutTimeSum = 0;
public long _shelfLifeNano;
public Novel(String title, long shelfLifeNano)
{
super(title);
_shelfLifeNano = shelfLifeNano;
}
/**
* getAuthor
*
* @param author
* @return
*/
public String getAuthor(String author)
{
return author;
}
/**
* setAuthor
*
* @param author
*/
public void setAuthor(String author)
{
_author = author;
}
/**
*
*/
public void checkInBook()
{
super.checkInBook();
_numCheckouts++;
long checkOutTime = getCheckOutTime();
_checkOutTimeSum += checkOutTime;
_checkOutAvgNano = _checkOutTimeSum / _numCheckouts;
}
/**
*
*/
public long getCheckOutTime()
{
Duration d = Duration.between(_checkOutTime, _checkInTime);
return d.getNano();
}
/**
*
*/
@Override
public boolean isShelflifeOver()
{
if (_checkOutAvgNano > _shelfLifeNano)
{
return true;
}
return false;
}
}
import java.time.Duration;
import java.time.LocalTime;
/**
* Textbook.java
*
* Description
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Textbook.java
*
*/
public class Textbook extends Book
{
public String _publishingCompany;
public long _shelfLifeNano;
LocalTime _timeAtConstruction;
/**
* Constructor for class
*
* @param title
* @param shelfLifeNano
*/
public Textbook(String title, long shelfLifeNano)
{
super(title);
_shelfLifeNano = shelfLifeNano;
_timeAtConstruction = LocalTime.now();
}
/**
* getPublishingCompany
*
* @param publishingCompany
* @return
*/
public String getPublishingCompany(String publishingCompany)
{
return publishingCompany;
}
/**
* setPublishingCompany
*
* @param publishingCompany
*/
public void setPublishingCompany(String publishingCompany)
{
_publishingCompany = publishingCompany;
}
/**
*
*/
@Override
public boolean isShelflifeOver()
{
LocalTime time = LocalTime.now();
Duration d = Duration.between(_timeAtConstruction, time);
long duration = d.getNano();
if (duration > _shelfLifeNano)
{
return true;
}
return false;
}
}
// MAIN TESTER METHOD
System.out.printf("NUM\tTITLE\t\t\t\tBOOK KEY\tTIME\tSHELF LIFE OVER?\tAUTHOR\t\tPUBLISHING COMPANY\n\n");
// create 2 books, output title, and provide count of books in library
// creates 1 book
Book book = new Book("Barron's Computer Science A");
System.out.printf("%d\t%s\t%d\n", book.getBook(), book.toString(), book.getId());
// creates 1 book
Book book2 = new Book("Angels and Demons");
System.out.printf("%d\t%s\t\t%d\n", book2.getBook(), book2.toString(), book2.getId());
{ // creates 1 novel
Novel novel = new Novel("Lion, Witch, and the Wardrobe", 70000000);
novel.checkOutBook(); // checks out a novel
novel.checkInBook(); // checks in a novel
novel.checkOutBook();
novel.checkInBook();
novel.checkOutBook();
novel.checkInBook();
Boolean shelfLife = novel.isShelflifeOver();
long checkOutTime = novel.getCheckOutTime();
System.out.printf("%d\t%s\t%d\t%dns\t", novel.getBook(), novel.toString(), novel.getId(), checkOutTime);
System.out.printf("%s", shelfLife.toString());
System.out.printf("\t\t\t%s\n", novel.getAuthor("C.S. Lewis"));
}
{ // creates 1 novel
Novel novel = new Novel("The Da Vincii Code", 70);
novel.checkOutBook();
novel.checkInBook();
novel.checkOutBook();
novel.checkInBook();
novel.checkOutBook();
novel.checkInBook();
Boolean shelfLife = novel.isShelflifeOver();
long checkOutTime = novel.getCheckOutTime();
System.out.printf("%d\t%s\t\t%d\t%dns\t", novel.getBook(), novel.toString(), novel.getId(), checkOutTime);
System.out.printf("%s", shelfLife.toString());
System.out.printf("\t\t\t%s\n", novel.getAuthor("Dan Brown"));
}
{ // creates 1 textbook
Textbook textbook = new Textbook("The Practice of Programming", 30000000);
textbook.checkOutBook();
textbook.checkInBook();
Boolean shelfLife = textbook.isShelflifeOver();
long checkOutTime = textbook.getCheckOutTime();
System.out.printf("%d\t%s\t%d\t%dns", textbook.getBook(), textbook.toString(), textbook.getId(),
checkOutTime);
System.out.printf("\t%s", shelfLife.toString());
System.out.printf("\t\t\t\t\t%s\n", textbook.getPublishingCompany("Pan Books (January 1, 2001)"));
}
{ // creates 1 textbook
Textbook textbook = new Textbook("e=MC^2 a Biography", 30);
textbook.checkOutBook();
textbook.checkInBook();
Boolean shelfLife = textbook.isShelflifeOver();
long checkOutTime = textbook.getCheckOutTime();
System.out.printf("%d\t%s\t\t%d\t%dns", textbook.getBook(), textbook.toString(), textbook.getId(),
checkOutTime);
System.out.printf("\t%s", shelfLife.toString());
System.out.printf("\t\t\t\t\t%s\n", textbook.getPublishingCompany("Addison-Wesley Professional Computing"));
}