Question: Complete the implementations of various classes in a bookStore. Read the API for bookStore carefully, the API of this lab is available under doc folder

Complete the implementations of various classes in a bookStore. Read the API for bookStore carefully, the API of this lab is available under doc folder in the Java project folder (docindex.html). Some example tests are given to you in the Testerv3 class. We consider a database that stores information about books and owners. Each book has (e.g., String title, int price, int yearPublished), whereas BookStoreOwner has (String name and long id). You must implement all methods in the BookStore. For this question, you are required to submit Java files such as Book, BookStore, BookStoreOwner, SortBooksbyYear in your Eclipse project.

Testerv3.java

package bookStore;

import static org.junit.Assert.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.*;

import org.junit.Test; import org.junit.rules.Timeout; import org.junit.runners.MethodSorters;

import org.junit.FixMethodOrder; import org.junit.Rule;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class Testerv3 { @Rule public Timeout globalTimeout = Timeout.seconds(1);

@Test public void test01_staticFields() { try { List fields = Arrays.asList(BookStore.class.getDeclaredFields());

int nStatic = 0; for (Field f : fields) { int mod = f.getModifiers(); if (Modifier.isStatic(mod)) { nStatic++; } } assertTrue("there should be no static fields", nStatic == 0); } catch (Exception x) { fail("exception occurred trying to get the fields of this class"); } }

@Test public void test02a_ctor() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); assertEquals("ctor set the wrong BookStoreOwner", w, b.getOwner()); }

@Test public void test03a_copyCtor() { BookStoreOwner w = new BookStoreOwner("OwnerC"); BookStore b = new BookStore(w); BookStore c = new BookStore(b); assertEquals("copy ctor set the wrong owner", w, c.getOwner()); }

//changeOwner @Test(expected = IllegalArgumentException.class) public void test04a_changeOwner() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); BookStoreOwner x = new BookStoreOwner("OwnerB"); b.changeOwner(x, x); } @Test public void test04b_changeOwner() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); BookStoreOwner x = new BookStoreOwner("OwnerB"); b.changeOwner(w, x); assertEquals("changeOwner failed to set the correct owner", x, b.getOwner()); } //contains @Test public void test06a_contains() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); Book b1= new Book("ABC",2018, 36); assertFalse("contains returned true for an empty Bookstore!", b.contains(b1)); } @Test public void test06b_contains() { List books = new ArrayList(); Book b1= new Book("Java 1",2010, 160); books.add(b1); Book b2= new Book("Signals and Systems",2011, 260); books.add(b2); Book b3= new Book("Optical Communication",2011, 30); books.add(b3); Book b4= new Book("5G",2010, 10); books.add(b4); BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); b.add(books); assertTrue("contains returned false for a BookStore with every book", b.contains(b1)); assertTrue("contains returned false for a BookStore with every book", b.contains(b2)); assertTrue("contains returned false for a BookStore with every book", b.contains(b3)); assertTrue("contains returned false for a BookStore with every book", b.contains(b4)); } @Test public void test06c_contains() { List books = new ArrayList(); Book b1= new Book("Java 1",2010, 160); books.add(b1); Book b2= new Book("Signals and Systems",2011, 260); books.add(b1); Book b3= new Book("Optical Communication",2011, 30); books.add(b1); Book b4= new Book("5G",2010, 10); books.add(b1);

BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); b.add(books); assertTrue("contains returned true for a BookStore with only b1", b.contains(b1)); assertFalse("contains returned false for a BookStore with only b1", b.contains(b2)); assertFalse("contains returned false for a BookStore with only b1", b.contains(b3)); assertFalse("contains returned false for BookStore with only b1", b.contains(b4)); } //sellingsinglebook @Test public void test07a_sellingsingleBook() { List books = new ArrayList(); Book b1= new Book("Java 1",2010, 160); books.add(b1); Book b2= new Book("Signals and Systems",2011, 260); books.add(b2); Book b3= new Book("Optical Communication",2011, 30); books.add(b3); Book b4= new Book("5G",2010, 10); books.add(b4); BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); b.add(books); Book got = b.sellingsingleBook(w, b1); assertEquals("selling returned the wrong Book", b1, got); got = b.sellingsingleBook(w, b2); assertEquals("selling returned the wrong Book", b2, got); got = b.sellingsingleBook(w, b3); assertEquals("selling returned the wrong Book", b3, got); got = b.sellingsingleBook(w, b4); assertEquals("selling returned the wrong Book", b4, got); } @Test public void test07b_sellingsingleBook() { List books = new ArrayList(); Book b1= new Book("Java 1",2010, 160); books.add(b1); Book b2= new Book("Signals and Systems",2011, 260); books.add(b2); Book b3= new Book("Optical Communication",2011,30); books.add(b3); Book b4= new Book("5G",2010, 10); books.add(b4); BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w); b.add(books); Book got = b.sellingsingleBook(new BookStoreOwner("OwnerB"),b1); assertNull("remove allowed a non-owner to remove a book", got); } }

BookStore.java Complete the implementations of various classes in a bookStore. Read the APIfor bookStore carefully, the API of this lab is available under doc

* YOU NEED A FIELD HERE TO HOLD THE BOOK OF THIS Bookstore private TreeMap books;|| private BookstoreOwner owner; * Initializes this book store so that it has the specified owner and no * books. * @param owner the owner of this book store public Bookstore (BookstoreOwner owner) { // COMPLETE THIS * Initializes this book store by copying another book store. This book store will * have the same owner and the same number and type of books as the other book store. * @param other the book store to copy 43 public Bookstore (Bookstore other) { // COMPLETE THIS 48 500 * Returns the owner of this book store. *

* This method is present only for testing purposes. Returning the owner of this * book store allows any user to sell books from the book store (because any * user can become the owner of this book store)! * @return the owner of this book store 600 public BookstoreOwner getowner() { // ALREADY IMPLEMENTED; DO NOT MODIFY return this.owner; 63 650 * Allows the current owner of this book store to give this book store to a new * owner. * @param currentowner the current owner of this book store * @param newowner the new owner of this book store * @throws IllegalArgumentException if currentowner reference is not the reference of the * current owner of this book store public void changeowner(Bookstoreowner currentowner, Bookstoreowner newowner) { // COMPLETE THIS 870 public void add(List books) { // COMPLETE THIS 940 * Returns true if this book store contains the specified book, and false * otherwise. 97 * @param book a book * @return true if this book store contains the specified book, and false otherwise 98 99 100 101 1020 183 184 105 106 public boolean contains (Book book) { // COMPLETE THIS 107 188 1890 110 111 * Allows the owner of this book store to sell a single book equal to the * specified book from this book store. * If the specified user is not equal to the owner of this book store, then the * book is not sold from this book store, and null is returned. * @param user the person trying to sell the book * @param book a book * @return a book equal to the specified book from this book store, or null if user is not the owner of this book store @pre. the book store contains a book equal to the specified book 120 121 122 public Book sellingsingleBook (Bookstoreowner user, Book book) { // COMPLETE THIS 1236 124 125 126 127 128 1290 130 131 132 * Allows the owner of this book store to sell the smallest number of books * whose total price value in dollars is equal or less than to the specified * price value in dollars. Try from the most expensive book and you may want * to use descendingkeyset() method. 133 *

137 * Returns the empty list if the specified user is not equal to the owner of * this book store. *

141 142 143 * @param user the person trying to sell books from this book store * @param pricevalue a value in dollars * @return the smallest number of books whose total price value in dollars is equal to the specified value in dollars from this book store @pre. the book store contains a group of books whose total price value is equal to specified value 144 145 146 1470 public List sellingBooks (Bookstoreowner user, int pricevalue) { * YOU NEED A FIELD HERE TO HOLD THE BOOK OF THIS Bookstore private TreeMap books;|| private BookstoreOwner owner; * Initializes this book store so that it has the specified owner and no * books. * @param owner the owner of this book store public Bookstore (BookstoreOwner owner) { // COMPLETE THIS * Initializes this book store by copying another book store. This book store will * have the same owner and the same number and type of books as the other book store. * @param other the book store to copy 43 public Bookstore (Bookstore other) { // COMPLETE THIS 48 500 * Returns the owner of this book store. *

* This method is present only for testing purposes. Returning the owner of this * book store allows any user to sell books from the book store (because any * user can become the owner of this book store)! * @return the owner of this book store 600 public BookstoreOwner getowner() { // ALREADY IMPLEMENTED; DO NOT MODIFY return this.owner; 63 650 * Allows the current owner of this book store to give this book store to a new * owner. * @param currentowner the current owner of this book store * @param newowner the new owner of this book store * @throws IllegalArgumentException if currentowner reference is not the reference of the * current owner of this book store public void changeowner(Bookstoreowner currentowner, Bookstoreowner newowner) { // COMPLETE THIS 870 public void add(List books) { // COMPLETE THIS 940 * Returns true if this book store contains the specified book, and false * otherwise. 97 * @param book a book * @return true if this book store contains the specified book, and false otherwise 98 99 100 101 1020 183 184 105 106 public boolean contains (Book book) { // COMPLETE THIS 107 188 1890 110 111 * Allows the owner of this book store to sell a single book equal to the * specified book from this book store. * If the specified user is not equal to the owner of this book store, then the * book is not sold from this book store, and null is returned. * @param user the person trying to sell the book * @param book a book * @return a book equal to the specified book from this book store, or null if user is not the owner of this book store @pre. the book store contains a book equal to the specified book 120 121 122 public Book sellingsingleBook (Bookstoreowner user, Book book) { // COMPLETE THIS 1236 124 125 126 127 128 1290 130 131 132 * Allows the owner of this book store to sell the smallest number of books * whose total price value in dollars is equal or less than to the specified * price value in dollars. Try from the most expensive book and you may want * to use descendingkeyset() method. 133 *

137 * Returns the empty list if the specified user is not equal to the owner of * this book store. *

141 142 143 * @param user the person trying to sell books from this book store * @param pricevalue a value in dollars * @return the smallest number of books whose total price value in dollars is equal to the specified value in dollars from this book store @pre. the book store contains a group of books whose total price value is equal to specified value 144 145 146 1470 public List sellingBooks (Bookstoreowner user, int pricevalue) {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!