Class BookstoreArrayList

java.lang.Object
com.university.bookstore.impl.BookstoreArrayList
All Implemented Interfaces:
BookstoreAPI

public class BookstoreArrayList extends Object implements 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 Details

    • BookstoreArrayList

      public BookstoreArrayList()
      Creates a new empty bookstore.
    • BookstoreArrayList

      public BookstoreArrayList(Collection<Book> initialBooks)
      Creates a bookstore with initial books.
      Parameters:
      initialBooks - books to add initially (may be null or empty)
  • Method Details

    • add

      public boolean add(Book book)
      Description copied from interface: BookstoreAPI
      Adds a book to the inventory. Duplicate ISBNs are not allowed.
      Specified by:
      add in interface BookstoreAPI
      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

      public boolean removeByIsbn(String isbn)
      Description copied from interface: BookstoreAPI
      Removes a book from the inventory by ISBN.
      Specified by:
      removeByIsbn in interface BookstoreAPI
      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

      public Book findByIsbn(String isbn)
      Description copied from interface: BookstoreAPI
      Finds a book by its ISBN.
      Specified by:
      findByIsbn in interface BookstoreAPI
      Parameters:
      isbn - the ISBN to search for
      Returns:
      the book if found, null otherwise
    • findByTitle

      public List<Book> findByTitle(String titleQuery)
      Description copied from interface: BookstoreAPI
      Searches for books by title (case-insensitive, partial match).
      Specified by:
      findByTitle in interface BookstoreAPI
      Parameters:
      titleQuery - the title or partial title to search for
      Returns:
      list of matching books (never null, may be empty)
    • findByAuthor

      public List<Book> findByAuthor(String authorQuery)
      Description copied from interface: BookstoreAPI
      Searches for books by author (case-insensitive, partial match).
      Specified by:
      findByAuthor in interface BookstoreAPI
      Parameters:
      authorQuery - the author name or partial name to search for
      Returns:
      list of matching books (never null, may be empty)
    • findByPriceRange

      public List<Book> findByPriceRange(double minPrice, double maxPrice)
      Description copied from interface: BookstoreAPI
      Finds all books within a price range (inclusive).
      Specified by:
      findByPriceRange in interface BookstoreAPI
      Parameters:
      minPrice - minimum price (inclusive)
      maxPrice - maximum price (inclusive)
      Returns:
      list of books within the price range
    • findByYear

      public List<Book> findByYear(int year)
      Description copied from interface: BookstoreAPI
      Finds all books published in a specific year.
      Specified by:
      findByYear in interface BookstoreAPI
      Parameters:
      year - the publication year
      Returns:
      list of books published in that year
    • size

      public int size()
      Description copied from interface: BookstoreAPI
      Gets the number of books in the inventory.
      Specified by:
      size in interface BookstoreAPI
      Returns:
      the total number of books
    • inventoryValue

      public double inventoryValue()
      Description copied from interface: BookstoreAPI
      Calculates the total value of all books in inventory.
      Specified by:
      inventoryValue in interface BookstoreAPI
      Returns:
      sum of all book prices
    • getMostExpensive

      public Book getMostExpensive()
      Description copied from interface: BookstoreAPI
      Finds the most expensive book in the inventory.
      Specified by:
      getMostExpensive in interface BookstoreAPI
      Returns:
      the book with highest price, null if inventory is empty
    • getMostRecent

      public Book getMostRecent()
      Description copied from interface: BookstoreAPI
      Finds the most recently published book.
      Specified by:
      getMostRecent in interface BookstoreAPI
      Returns:
      the book with the latest publication year, null if inventory is empty
    • snapshotArray

      public Book[] snapshotArray()
      Description copied from interface: BookstoreAPI
      Creates a defensive copy of the inventory as an array. Changes to the returned array will not affect the inventory.
      Specified by:
      snapshotArray in interface BookstoreAPI
      Returns:
      array containing all books in the inventory
    • getAllBooks

      public List<Book> getAllBooks()
      Description copied from interface: BookstoreAPI
      Gets all books in the inventory. The returned list is a defensive copy.
      Specified by:
      getAllBooks in interface BookstoreAPI
      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

      public Map<String,Object> getStatistics()
      Gets statistics about the inventory.
      Returns:
      map with statistics (size, total_value, avg_price, etc.)
    • toString

      public String toString()
      Overrides:
      toString in class Object