BookList class
import java.io.*;
import java.util.*;
public class BookList
{
/* **************************
PROGRAMMER: Chris Eason
PROGRAMMING LANGUAGE: jdk 1.2.2
PROJECT: Puddleville Library
COURSE: CS 1302
DATE: 8 February 2001
PURPOSE: This class models a collection of books.
METHODS:
The constructor is overloaded to create the collection using either
a file containing information about the books, or an array of Book objects.
BookList(String fileName)
Booklist(Book[] books)
The look up method finds a book in the collection and returns a reference
to it. If the requested book is not in the collection, null is returned.
Book lookUp(String ISBN)
INPUT:
The input for the BookList constructor that accepts a file name will have
the following format:
The file will list the books one per line. Each line will contain all the
information for one book with the caret ^ symbol separating each piece of data.
The format for a book that is checked out will be as follows:
ISBN^PatronNumber^month^day^year
The format for a Book that is not checked out will be as follows:
ISBN^ ^^^
OUTPUT: This class produces no direct output.
LIMITATIONS:
Assumes all input has been checked and is error free.
NO ERROR CHECKING is done.
************************** */
/* **************************
CONSTRUCTORS
************************** */
public BookList(String fileName) throws Exception
{
/* **************************
PURPOSE: Creates the collection of books
PARAMETERS: String filename -- the name of the file containing the book data
INPUT: A text file containing information about books in the format specified
in the opening documentation of this class.
OUTPUT: Produces no direct output.
LIMITATIONS: Does no error checking on the input.
All Exceptions are thrown
************************** */
BufferedReader bookFile = new BufferedReader( new FileReader(fileName) );
// Count the number of books in the file NOTE: one book per line
int count = 0;
while (bookFile.readLine() != null)
count++;
// Create the instance variable theBooks with the appropriate size
theBooks = new Book[count];
// Read the data from the file and fill the array
int pos = 0; // the position in the array to be filled
bookFile = new BufferedReader( new FileReader(fileName) );
String bookDataLine = bookFile.readLine();
while (bookDataLine != null)
{
// parse the input and create the book
StringTokenizer stok = new StringTokenizer(bookDataLine, "^");
// the first token is the ISBN
Book thisBook = new Book(stok.nextToken());
// the next token is either blank (not checked out) or the patron (checked out)
String tok = stok.nextToken();
if ( ! tok.equals(" ") ) // the token is not blank, so the book is checked out
{
thisBook.setPatron(tok);
// take care of the due date
int month = Integer.parseInt(stok.nextToken());
int day = Integer.parseInt(stok.nextToken());
int year = Integer.parseInt(stok.nextToken());
thisBook.setDueDate(new Date(year-1900, month-1, day));
}
// add the book to the array
theBooks[pos++] = thisBook;
// get the next input line from the file
bookDataLine = bookFile.readLine();
} // while (bookDataLine != null)
} // BookList(String)
public BookList(Book[] books)
{
/* **************************
PURPOSE: Creates the collection of books
PARAMETERS: Book[] books -- an array of books that are to be in the collection
INPUT: No direct input.
OUTPUT: Produces no direct output.
LIMITATIONS: Assumes the books passed in are error free.
************************** */
theBooks = books;
} // BookList(Book[])
/* **************************
PUBLIC METHODS
************************** */
public Book lookUp(String ISBN)
{
/* **************************
PURPOSE: Find a book in the collection.
PARAMETERS: String ISBN -- the ISBN of the book that is being searched for.
RETURNS: If the requested book is in the collection -- A reference to the Book
If the requested book is NOT in the collection -- null
INPUT: No direct input.
OUTPUT: Produces no direct output.
LIMITATIONS:
************************** */
// Search the array for the requested ISBN and return the book
for (int i = 0; i < theBooks.length; i++)
if ( theBooks[i].getISBN().equals(ISBN) )
return theBooks[i];
// If we get past the loop, the ISBN wasn't found
return null;
} // lookUp(String)
/* **************************
INSTANCE VARIABLES
************************** */
Book[] theBooks;
} // BookList class
[BACK]
[HOME]
Updated 2/17/2001, contact
Andres Roa