Answered step by step
Verified Expert Solution
Question
1 Approved Answer
LAB EXERCISE Complete the implementations of various classes in a bookStore. Read the API for bookStore carefully, the API of this lab is available under
LAB EXERCISE 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 (doc-index.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, SortBooksby Year in your Eclipse project. New lest Document - Notepad File Edit Format View Help package bookStore; * A class representing a book store that has an * owner. A book store owns a collection (or possibly collections) of books, but does not own the books themselves. *Only the owner of the book store is able to sell books from the book store. * The book store does NOT own its owner import java.util. public class BookStore { * YOU NEED A FIELD HERE TO HOLD THE HOOK OF THIS BookStore private TreeMap books; private RookstoreOwner 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 public BookstoreBookstore other) { // COMPLETE THIS *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 Windows (CRLF) Ln 44. Cel 23 New lest Document - Notepad File Edit Format View Help 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 public BookStoreOwner getOwner() [ // ALREADY IMPLEMENTED; DO NOT MODIFY return this.owner; * 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(BookStorcOwner current Owner, BookStore Owner newOwner) { // COMPLETE THIS * Adds the specified books to this book store. * @param hooks a list of books to add to this book store public void add(List books) // COMPLETE THIS * Returns true if this book store contains the specified book, and false * otherwise * @param book a book * @return true if this book store contains the specified book, and false * otherwise public boolean contains(Book book) Windows (CRLF) in l. Cel New lest Document - Notepad File Edit Format View Help public boolean contains Book book) // COMPLETE THIS * 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 nullis 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 hook store * contains a book equal to the specified book public Book sellingsingleBook BookStoreOwner user, Book book) // COMPLETE THIS * 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 descendingKeySetl) method. *Returns the empty list if the specified user is not equal to the owner of * this book store. * @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 public List selling BooksBookStoreOwner user, int pricevalue) { // COMPLETE THIS Screenshot saved The screenshot was added to your OneDrive. CneDrive Windows (CRLF) in l. Cel New lest Document File Edit Format View - Notepad Help And this is the rester 3: 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.Method Sorters; import org.junit.Fix MethodOrder; import org.junil.Rule; FixMethodOrder(MethodSorters.NAME_ASCENDING) public class Testerv3 { Rule public Timeout globalTimeout-Timeout.seconds(1); al Test public void test01_staticFields() { try i List fields - Arrays.asListBookstore.class.getDeclared Fields()); int Static - 0; for (Field f: fields) { int mod = f.getModifiers(); if (Modifier.isStaticmod)) { Static++; assertTruci" there should be no static fields", nStatic -- 0); } catch (Exception x) { faill"exception occurred trying to get the fields of this class"); @Test public void testo2a_ctor) Windows (CRLF) in 16. Col33 New lest Document - Notepad File Edit Format View Help fail("exception occurred trying to get the fields of this class"); (un. Test public void testo2a ctor() { BookStoreOwner w-new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w): assertEquals("ctor set the wrong BookStore Owner", w, b.getOwner()); al Test public void test03a_copyCtor() { BookStoreOwner w-new BookStoreOwner("OwnerC"); BookStore b = new BookStore(w); BookStore c= new BookStore(b); assertEquals("cupy clor set the wrong owner", W, C.getOwner()); //change Owner @Test(expected - IllegalArgumentException.class) public void test04a_change Owner() { BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b new BookStore(w); BookStoreOwner x-new BookStoreOwner/OwnerB"); b.change Owner(x,x); @Test public void test04b_changeOwner() [ BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b-new BookStorelw); BookStoreOwner x-new BookStoreOwner"OwnerB"); b.changeOwnerw, x); assertEquals("changeOwner failed to set the correct owner", X, b.getOwner()); // contains C.Test public void test06a contains() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore): Book b1-new Book("ABC",2018, 36); assertFalsel"contains returned true for an empty Bookstore!", b.contains(61)); Windows (CRLF) in 206. Col33 New lest Document - Notepad File Edit Format View Help assertFalsel"contains returned true lor an emply Bookstorel", b.contains(61)); (n. Test public void test06b_contains() { List books - new ArrayList books-new ArrayList0); Book bi-new Book("Java 15,2010, 160); books.add(bi): Book b2= new Book("Signals and Systems",2011,260); books.add/b1); Book b3- new Book/"Optical Communication" 2011, 30); books.add(bi); Book b4 new Book("56,2010, 10); books.add[bi]; BookStoreOwner w-new BookStoreOwner("OwnerA"); BookStore b-new BookStorelwl; b.add(books); Windows (CRLF) in 348. Cl22 New lest Document - Notepad File Edit Format View Help BookStore b-new BookStore(w); b.add(books) assert Truel"contains returned true for a Bookstore with only b1", b.contains(51)); assertfalse"contains returned false for a BookStore with only b1", b.contains(62)); assertFalsc"contains returned false for a Book Store with only b1", b.contains(b)); assert alsel"contains returned false for BookStore with only b1", b.contains(54)); //sellingsinglebook Test public void testo7a_sellingsingleHook() { List books - new ArrayList(); Book bi= 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(63); Book b4-new Book["5G,2010, 10); books.add(64) BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b-new BookStore(w); b.add books); Book got = b.sellingsingleBook[w, 1); assertEquals("selling returned the wrong Book", b1, Bol); got = b.sellingsingleBook[w, b2); assertEquals("selling returned the wrong Book", b2, got); cot-b.sellingsingleBook[w, b3); assertEquals("selling returned the wrong Book", b3, got); Rot-b.sellingsingleBook[w, b4); assertEquals("selling returned the wrong Book", b4, cot); 00 Test Windows (CRLF) in 348. Cl22 New lest Document - Notepad File Edit Format View Help books.add(62); Book b3-new Book["Optical Communication", 2011, 30); books.add(63); Book b4= new Book("56,2010, 10); books.add(64) BookStoreOwner w-new BookStore Owner("OwnerA"); BookStore b = new BookStore(w); b.add/books); Book got-b.sellingsingleBook[w, b1); assertEquals("selling returned the wrong Hook", bi, got); Fot-b.sellingsingleBook[w, b2); assertEquals("selling returned the wrong Book", b2, got); BOL-b.sellingsingleBook[w, b3); assertEquals("selling returned the wrong Book", b3, cot): got = b.sellingsingleBook[w, 4); assertEquals("selling returned the wrong Book", b4, Bot); @Test public void test07b_sellingsingleBook) List books = new ArrayList(); Book b1-new Book("Java 1", 2010, 160); books.add[bi]; Book b2= new Bookl"Signals and Systems", 2011, 260); books.add(b); Book b3-new Buok/"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 Rot-b.sellingsingleBook new BookStoreOwner/OwnerB"),1); assertNull("remove allowed a non-owner to remove a book", got); Screenshot saved The screenshot was added to your OneDrive. CneDrive Windows (CRLF) Ln 230. Col10 LAB EXERCISE 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 (doc-index.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, SortBooksby Year in your Eclipse project. New lest Document - Notepad File Edit Format View Help package bookStore; * A class representing a book store that has an * owner. A book store owns a collection (or possibly collections) of books, but does not own the books themselves. *Only the owner of the book store is able to sell books from the book store. * The book store does NOT own its owner import java.util. public class BookStore { * YOU NEED A FIELD HERE TO HOLD THE HOOK OF THIS BookStore private TreeMap books; private RookstoreOwner 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 public BookstoreBookstore other) { // COMPLETE THIS *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 Windows (CRLF) Ln 44. Cel 23 New lest Document - Notepad File Edit Format View Help 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 public BookStoreOwner getOwner() [ // ALREADY IMPLEMENTED; DO NOT MODIFY return this.owner; * 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(BookStorcOwner current Owner, BookStore Owner newOwner) { // COMPLETE THIS * Adds the specified books to this book store. * @param hooks a list of books to add to this book store public void add(List books) // COMPLETE THIS * Returns true if this book store contains the specified book, and false * otherwise * @param book a book * @return true if this book store contains the specified book, and false * otherwise public boolean contains(Book book) Windows (CRLF) in l. Cel New lest Document - Notepad File Edit Format View Help public boolean contains Book book) // COMPLETE THIS * 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 nullis 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 hook store * contains a book equal to the specified book public Book sellingsingleBook BookStoreOwner user, Book book) // COMPLETE THIS * 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 descendingKeySetl) method. *Returns the empty list if the specified user is not equal to the owner of * this book store. * @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 public List selling BooksBookStoreOwner user, int pricevalue) { // COMPLETE THIS Screenshot saved The screenshot was added to your OneDrive. CneDrive Windows (CRLF) in l. Cel New lest Document File Edit Format View - Notepad Help And this is the rester 3: 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.Method Sorters; import org.junit.Fix MethodOrder; import org.junil.Rule; FixMethodOrder(MethodSorters.NAME_ASCENDING) public class Testerv3 { Rule public Timeout globalTimeout-Timeout.seconds(1); al Test public void test01_staticFields() { try i List fields - Arrays.asListBookstore.class.getDeclared Fields()); int Static - 0; for (Field f: fields) { int mod = f.getModifiers(); if (Modifier.isStaticmod)) { Static++; assertTruci" there should be no static fields", nStatic -- 0); } catch (Exception x) { faill"exception occurred trying to get the fields of this class"); @Test public void testo2a_ctor) Windows (CRLF) in 16. Col33 New lest Document - Notepad File Edit Format View Help fail("exception occurred trying to get the fields of this class"); (un. Test public void testo2a ctor() { BookStoreOwner w-new BookStoreOwner("OwnerA"); BookStore b = new BookStore(w): assertEquals("ctor set the wrong BookStore Owner", w, b.getOwner()); al Test public void test03a_copyCtor() { BookStoreOwner w-new BookStoreOwner("OwnerC"); BookStore b = new BookStore(w); BookStore c= new BookStore(b); assertEquals("cupy clor set the wrong owner", W, C.getOwner()); //change Owner @Test(expected - IllegalArgumentException.class) public void test04a_change Owner() { BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b new BookStore(w); BookStoreOwner x-new BookStoreOwner/OwnerB"); b.change Owner(x,x); @Test public void test04b_changeOwner() [ BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b-new BookStorelw); BookStoreOwner x-new BookStoreOwner"OwnerB"); b.changeOwnerw, x); assertEquals("changeOwner failed to set the correct owner", X, b.getOwner()); // contains C.Test public void test06a contains() { BookStoreOwner w = new BookStoreOwner("OwnerA"); BookStore b = new BookStore): Book b1-new Book("ABC",2018, 36); assertFalsel"contains returned true for an empty Bookstore!", b.contains(61)); Windows (CRLF) in 206. Col33 New lest Document - Notepad File Edit Format View Help assertFalsel"contains returned true lor an emply Bookstorel", b.contains(61)); (n. Test public void test06b_contains() { List books - new ArrayList books-new ArrayList0); Book bi-new Book("Java 15,2010, 160); books.add(bi): Book b2= new Book("Signals and Systems",2011,260); books.add/b1); Book b3- new Book/"Optical Communication" 2011, 30); books.add(bi); Book b4 new Book("56,2010, 10); books.add[bi]; BookStoreOwner w-new BookStoreOwner("OwnerA"); BookStore b-new BookStorelwl; b.add(books); Windows (CRLF) in 348. Cl22 New lest Document - Notepad File Edit Format View Help BookStore b-new BookStore(w); b.add(books) assert Truel"contains returned true for a Bookstore with only b1", b.contains(51)); assertfalse"contains returned false for a BookStore with only b1", b.contains(62)); assertFalsc"contains returned false for a Book Store with only b1", b.contains(b)); assert alsel"contains returned false for BookStore with only b1", b.contains(54)); //sellingsinglebook Test public void testo7a_sellingsingleHook() { List books - new ArrayList(); Book bi= 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(63); Book b4-new Book["5G,2010, 10); books.add(64) BookStoreOwner w = new BookstoreOwner("OwnerA"); BookStore b-new BookStore(w); b.add books); Book got = b.sellingsingleBook[w, 1); assertEquals("selling returned the wrong Book", b1, Bol); got = b.sellingsingleBook[w, b2); assertEquals("selling returned the wrong Book", b2, got); cot-b.sellingsingleBook[w, b3); assertEquals("selling returned the wrong Book", b3, got); Rot-b.sellingsingleBook[w, b4); assertEquals("selling returned the wrong Book", b4, cot); 00 Test Windows (CRLF) in 348. Cl22 New lest Document - Notepad File Edit Format View Help books.add(62); Book b3-new Book["Optical Communication", 2011, 30); books.add(63); Book b4= new Book("56,2010, 10); books.add(64) BookStoreOwner w-new BookStore Owner("OwnerA"); BookStore b = new BookStore(w); b.add/books); Book got-b.sellingsingleBook[w, b1); assertEquals("selling returned the wrong Hook", bi, got); Fot-b.sellingsingleBook[w, b2); assertEquals("selling returned the wrong Book", b2, got); BOL-b.sellingsingleBook[w, b3); assertEquals("selling returned the wrong Book", b3, cot): got = b.sellingsingleBook[w, 4); assertEquals("selling returned the wrong Book", b4, Bot); @Test public void test07b_sellingsingleBook) List books = new ArrayList(); Book b1-new Book("Java 1", 2010, 160); books.add[bi]; Book b2= new Bookl"Signals and Systems", 2011, 260); books.add(b); Book b3-new Buok/"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 Rot-b.sellingsingleBook new BookStoreOwner/OwnerB"),1); assertNull("remove allowed a non-owner to remove a book", got); Screenshot saved The screenshot was added to your OneDrive. CneDrive Windows (CRLF) Ln 230. Col10
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started