Class BookstoreArrayList
java.lang.Object
com.university.bookstore.impl.BookstoreArrayList
- All Implemented Interfaces:
BookstoreAPI
ArrayList-based implementation of the BookstoreAPI.
This implementation uses an ArrayList for storage and enforces ISBN uniqueness. All collection returns are defensive copies to maintain encapsulation.
Performance characteristics:
- add: O(n) - due to uniqueness check
- removeByIsbn: O(n) - linear search required
- findByIsbn: O(n) - linear search required
- size: O(1) - ArrayList maintains size
Note: This implementation is NOT thread-safe. For concurrent access, consider using synchronization or ConcurrentHashMap.
- Since:
- 2024-09-15
- Version:
- 1.0
- Author:
- Navid Mohaghegh
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty bookstore.BookstoreArrayList(Collection<Book> initialBooks) Creates a bookstore with initial books. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds a book to the inventory.voidclear()Clears all books from the inventory.findByAuthor(String authorQuery) Searches for books by author (case-insensitive, partial match).findByIsbn(String isbn) Finds a book by its ISBN.findByPriceRange(double minPrice, double maxPrice) Finds all books within a price range (inclusive).findByTitle(String titleQuery) Searches for books by title (case-insensitive, partial match).findByYear(int year) Finds all books published in a specific year.Gets all books in the inventory.Finds the most expensive book in the inventory.Finds the most recently published book.Gets statistics about the inventory.doubleCalculates the total value of all books in inventory.booleanremoveByIsbn(String isbn) Removes a book from the inventory by ISBN.intsize()Gets the number of books in the inventory.Book[]Creates a defensive copy of the inventory as an array.voidSorts the inventory by price (ascending).voidSorts the inventory by title (alphabetical order).voidSorts the inventory by year (ascending).toString()
-
Constructor Details
-
BookstoreArrayList
public BookstoreArrayList()Creates a new empty bookstore. -
BookstoreArrayList
Creates a bookstore with initial books.- Parameters:
initialBooks- books to add initially (may be null or empty)
-
-
Method Details
-
add
Description copied from interface:BookstoreAPIAdds a book to the inventory. Duplicate ISBNs are not allowed.- Specified by:
addin interfaceBookstoreAPI- Parameters:
book- the book to add (non-null)- Returns:
- true if the book was added, false if ISBN already exists or book is null
-
removeByIsbn
Description copied from interface:BookstoreAPIRemoves a book from the inventory by ISBN.- Specified by:
removeByIsbnin interfaceBookstoreAPI- Parameters:
isbn- the ISBN of the book to remove- Returns:
- true if a book was removed, false if no book with that ISBN exists
-
findByIsbn
Description copied from interface:BookstoreAPIFinds a book by its ISBN.- Specified by:
findByIsbnin interfaceBookstoreAPI- Parameters:
isbn- the ISBN to search for- Returns:
- the book if found, null otherwise
-
findByTitle
Description copied from interface:BookstoreAPISearches for books by title (case-insensitive, partial match).- Specified by:
findByTitlein interfaceBookstoreAPI- Parameters:
titleQuery- the title or partial title to search for- Returns:
- list of matching books (never null, may be empty)
-
findByAuthor
Description copied from interface:BookstoreAPISearches for books by author (case-insensitive, partial match).- Specified by:
findByAuthorin interfaceBookstoreAPI- Parameters:
authorQuery- the author name or partial name to search for- Returns:
- list of matching books (never null, may be empty)
-
findByPriceRange
Description copied from interface:BookstoreAPIFinds all books within a price range (inclusive).- Specified by:
findByPriceRangein interfaceBookstoreAPI- Parameters:
minPrice- minimum price (inclusive)maxPrice- maximum price (inclusive)- Returns:
- list of books within the price range
-
findByYear
Description copied from interface:BookstoreAPIFinds all books published in a specific year.- Specified by:
findByYearin interfaceBookstoreAPI- Parameters:
year- the publication year- Returns:
- list of books published in that year
-
size
public int size()Description copied from interface:BookstoreAPIGets the number of books in the inventory.- Specified by:
sizein interfaceBookstoreAPI- Returns:
- the total number of books
-
inventoryValue
public double inventoryValue()Description copied from interface:BookstoreAPICalculates the total value of all books in inventory.- Specified by:
inventoryValuein interfaceBookstoreAPI- Returns:
- sum of all book prices
-
getMostExpensive
Description copied from interface:BookstoreAPIFinds the most expensive book in the inventory.- Specified by:
getMostExpensivein interfaceBookstoreAPI- Returns:
- the book with highest price, null if inventory is empty
-
getMostRecent
Description copied from interface:BookstoreAPIFinds the most recently published book.- Specified by:
getMostRecentin interfaceBookstoreAPI- Returns:
- the book with the latest publication year, null if inventory is empty
-
snapshotArray
Description copied from interface:BookstoreAPICreates a defensive copy of the inventory as an array. Changes to the returned array will not affect the inventory.- Specified by:
snapshotArrayin interfaceBookstoreAPI- Returns:
- array containing all books in the inventory
-
getAllBooks
Description copied from interface:BookstoreAPIGets all books in the inventory. The returned list is a defensive copy.- Specified by:
getAllBooksin interfaceBookstoreAPI- Returns:
- list of all books (never null, may be empty)
-
clear
public void clear()Clears all books from the inventory. Useful for testing and bulk operations. -
sortByTitle
public void sortByTitle()Sorts the inventory by title (alphabetical order). -
sortByPrice
public void sortByPrice()Sorts the inventory by price (ascending). -
sortByYear
public void sortByYear()Sorts the inventory by year (ascending). -
getStatistics
-
toString
-