Having trouble using methods between classes & what im being asked to do here
I have 3 classes bookStuff, which contains the main method and uses bookCollection, which uses book to define a data type for a collection of books given in text files.
I only need to complete the bookCollection class by creating methods which use book to run. However im having difficulty understanding how to use book to complete these methods.
the method Addbook adds a book to a collection however im not sure how to use the function findbook to check if that book is already in the collection.
/*
AddBook
Adds the given book to the collection, provided there is room in the
collection and the book is not already there. If the collection has
already reached its limit, throw the CollectionFull exception.
If the book is already in the collection as determined by ISBN, throw
the DuplicateBook exception.
*/
// put book in next available spot
// throw collection full exception
public void addBook(Book otherBook) {
//for(int i = 0; i < this.size; i++) {
//if (bookArray[i].equals(otherBook)) {
//throw new DuplicateBook("book is already in collection");
} else if (currentSize >= LIMIT) {
throw new CollectionFull("collection is full");
} else {
this.bookArray[currentSize] = otherBook;
//increments current size
currentSize++;
}
}
}
/*
FindBook
More than one method involves searching for a book (using the
ISBN) in a collection. It will make your job easier if you write a
findBook method as a helper function. This method should be
private, since it will be used only by the methods within the
BookCollection class.
This method should return null if the book cannot be found in the
collection. This will allow findBook to be used by addBook and
merge, where not finding the book is expected (addBook) or OK
(merge).
*/
// if in collection return match
// if false do nothing
private Book findBook(String isbn) {
//return this.bookArray[0];
//.getIsbn().equals(isbn);
for (int i = 0; i <= size; i++) {
if (bookArray[i].getIsbn().equals(isbn)) {
return this.bookArray[i];
}
}
return null;
}
instructor directions thanks for your help.
Posted Feb 22, 2016, 4:05 AM UTC
83