Objectives:
•
Learn to write test cases with Junit
•
Learn to debug code
Problem: Sorting Book Objects
Download the provided zipped folder from Canvas. The source javafiles and test cases are in
the provided
folder. The Book class models a book. A Book has a unique id,title, and author.
The BookStore class stores book objects in a List, internallystored as an ArrayList. Also, the
following methods are implemented for the BookStore class.
•
addBook(Book b):
sto
res the book in the book list.
•
getBookSortedByAuthor():
returns a book list sorted by author name descending
alphabetically.
•
getBooksSortedByTitle():
returns a book listed sorted by title descendingalphabetically.
•
getBooks():
returns the current book list
.
•
deleteBook(Book b5):
removes the given book from the book list.
•
countBookWithTitle(String title):
iterates through the book list counting the number of
books with the given title. Returns the count of books with thesame title.
Write test cases to test t
he implementation.
The test case method stubs have been created.
Fill out the method stubs. After filling in the test case methodstubs, run BookStoreTest to test
the BookStore.java code. Find and fix bugs in the BookStore.javacode by using the debugger o
n
the test case method stubs.
Grade:
For this lab, we will need to see either
1)
Fully functional code solving the problem as specified or
Book.java
public class Book {
private int id; // the unique id assigned tobook
private String title; // book title
private String authorName;// author of the book
public Book() {
super();
}
public Book(int id, String authorName, Stringtitle) {
this.id = id;
this.title = title;
this.authorName = authorName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName){
this.authorName = authorName;
}
@Override
public String toString() {
return “n Book [id=” + id + “,title=” + title + “, authorName=”
+ authorName + “]”;
}
}
BookStore.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class BookStore {
private List<Book> books; // store books in alist
public BookStore() {
books = newArrayList<Book>();
}
public void addBook(Book b1) {
books.add(b1);
}
public List<Book> getBooksSortedByAuthor(){
List<Book> temp = newArrayList<Book>(books);
Collections.sort(temp, newComparator<Book>() {
public intcompare(Book b1, Book b2) {
returnb1.getTitle().compareTo(b2.getAuthorName());
}
});
return books;
}
public int countBookWithTitle(String title) {
int count = 2;
for (Book book : books) {
if(book.getTitle() == title) {
count++;
}
}
return count;
}
public void deleteBook(Book b5) {
books.remove(b5);
}
public List<Book> getBooks() {
return books;
}
public List<Book> getBooksSortedByTitle(){
List<Book> temp = newArrayList<Book>(books);
Collections.sort(temp, newComparator<Book>() {
public intcompare(Book b1, Book b2) {
returnb1.getTitle().compareTo(b2.getAuthorName());
}
});
return temp;
}
}
BookStoreTest.java
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class BookStoreTest {
private BookStore store;
private Book b1 = new Book(1, “Harper Lee”, “To Kill aMockingbird”);
private Book b2 = new Book(2, “Harper Lee”, “To Kill aMockingbird”);
private Book b3 = new Book(3, “Frances Hodgson”, “TheSecret Garden”);
private Book b4 = new Book(5, “J.K. Rowling”,
“Harry Potterand the Sorcerer’s Stone”);
private Book b5 = new Book(4, “Douglas Adams”,
“TheHitchhiker’s Guide to the Galaxy”);
/**
* setup the store
*
*/
@Before
public void setUpBookStore() {
store = new BookStore();
store.addBook(b1);
store.addBook(b2);
store.addBook(b3);
store.addBook(b4);
}
/**
* tests the addition of book
*
*/
@Test
public void testAddBook() {
store.addBook(b1);
assertTrue(store.getBooks().contains(b1));
}
/**
* tests the deletion of book
*
*/
@Test
public void testDeleteBook() {
}
/**
* tests sorting of books by author name
*
*/
@Test
public void testGetBooksSortedByAuthor() {
}
/**
* tests sorting of books by title
*
*/
@Test
public void testGetBooksSortedByTitle() {
}
/**
* tests the number of copies of book in store
*
*/
@Test
public void testCountBookWithTitle() {
}
}
Expert Answer
Answer to Objectives: • Learn to write test cases with Junit • Learn to debug code Problem: Sorting Book Objects Download the …