hockeygoalie5
In Runtime
- Messages
- 299
- Location
- United States
I'm taking a Java tutorial and hit a stone wall when a problem showed up. I checked the code with what the tutorial says, it matches but I get a different result than the tutorial!
MyLibrary.java:
MyLibraryTest.java:
Test checkOut fails giving the message, "Second book should not have checked out." Passes on tutorial.
MyLibrary.java:
Code:
package org.totalbeginner.tutorial;
import java.util.ArrayList;
public class MyLibrary {
String name;
ArrayList<Book> books;
ArrayList<Person> people;
public MyLibrary(String name) {
this.name = name;
books = new ArrayList<Book>();
people = new ArrayList<Person>();
}
public String getName() {
return name;
}
public ArrayList<Book> getBooks() {
return books;
}
public ArrayList<Person> getPeople() {
return people;
}
public void addBook(Book b1) {
this.books.add(b1);
}
public void removeBook(Book b1) {
this.books.remove(b1);
}
public void addPerson(Person p1) {
this.people.add(p1);
}
public void removePerson(Person p1) {
this.people.remove(p1);
}
public boolean checkOut(Book b1, Person p1) {
int booksOut = this.getBooksForPerson(p1).size();
if ((b1.getPerson() == null) &&
(booksOut < p1.getMaximumBooks())) {
b1.setPerson(p1);
return true;
}
else {
return false;
}
}
public boolean checkIn(Book b1) {
if (b1.getPerson() != null) {
b1.setPerson(null);
return true;
}
else {
return false;
}
}
public ArrayList<Book> getBooksForPerson(Person p1) {
ArrayList<Book> result = new ArrayList<Book>();
for (Book aBook : this.getBooks()) {
if ((aBook.getPerson() != null) && (aBook.getPerson().getName().equals(p1.getName()))) {
result.add(aBook);
}
}
return result;
}
}
Code:
package org.totalbeginner.tutorial;
import java.util.ArrayList;
import org.omg.CORBA.PUBLIC_MEMBER;
import junit.framework.TestCase;
public class MyLibraryTest extends TestCase {
private Book b1;
private Book b2;
private Person p1;
private Person p2;
private MyLibrary ml;
// test constructor
public void testMyLibrary() {
MyLibrary ml = new MyLibrary("Test");
assertEquals("Test", ml.name);
assertTrue(ml.books instanceof ArrayList);
assertTrue(ml.people instanceof ArrayList);
}
public void setup() {
b1 = new Book("Book1");
b2 = new Book("Book2");
p1 = new Person();
p2 = new Person();
p1.setName("Fred");
p2.setName("Sue");
ml = new MyLibrary("Test");
}
public void testAddBook() {
//create test objects
setup();
//test initial size is 0
assertEquals(0, ml.getBooks().size());
ml.addBook(b1);
ml.addBook(b2);
assertEquals(2, ml.getBooks().size());
assertEquals(0, ml.getBooks().indexOf(b1));
assertEquals(1, ml.getBooks().indexOf(b2));
ml.removeBook(b1);
assertEquals(1, ml.getBooks().size());
assertEquals(0, ml.getBooks().indexOf(b2));
ml.removeBook(b2);
assertEquals(0, ml.getBooks().size());
}
public void testCheckOut() {
// set up objects
setup();
addItems();
assertTrue("Book did not check out correctly", ml.checkOut(b1,p1));
assertEquals("Fred", b1.getPerson().getName());
assertFalse("Book was already checked out", ml.checkOut(b1,p2));
assertTrue("Book check in failed", ml.checkIn(b1));
assertFalse("Book was already checked in", ml.checkIn(b1));
assertFalse("Book was never checked out", ml.checkIn(b1));
// additional test for maximumBooks
setup();
addItems();
assertTrue("First book did not check out", ml.checkOut(b2, p1));
assertFalse("Second book should not have checked out", ml.checkOut(b1, p1));
}
private void addItems() {
ml.addBook(b1);
ml.addBook(b2);
ml.addPerson(p1);
ml.addPerson(p2);
}
public void testGetBooksForPerson() {
setup();
addItems();
assertEquals(0, ml.getBooksForPerson(p1).size());
ml.checkOut(b1, p1);
ArrayList<Book> testBooks = ml.getBooksForPerson(p1);
assertEquals(1, testBooks.size());
assertEquals(0, testBooks.indexOf(b1));
ml.checkOut(b2, p1);
testBooks = ml.getBooksForPerson(p1);
assertEquals(2, testBooks.size());
assertEquals(1, testBooks.indexOf(b2));
}
}