Class BookArrayUtils

java.lang.Object
com.university.bookstore.utils.BookArrayUtils

public final class BookArrayUtils extends Object
Utility class for array-based operations on Book objects.

This class provides static methods for manipulating and analyzing arrays of books, demonstrating array operations without using ArrayList.

All methods handle null arrays and null elements gracefully.

Since:
2024-09-15
Version:
1.0
Author:
Navid Mohaghegh
  • Method Details

    • countBeforeYear

      public static int countBeforeYear(Book[] books, int yearCutoff)
      Counts books published before a given year.
      Parameters:
      books - array of books (may be null or contain nulls)
      yearCutoff - the cutoff year (exclusive)
      Returns:
      count of books published before the cutoff year
    • countByAuthor

      public static int countByAuthor(Book[] books, String author)
      Counts books by a specific author (case-insensitive, exact match).
      Parameters:
      books - array of books (may be null or contain nulls)
      author - the author name to search for
      Returns:
      count of books by the specified author
    • filterPriceAtMost

      public static Book[] filterPriceAtMost(Book[] books, double maxPrice)
      Filters books with price at most the specified maximum. Returns a compact array (no nulls, exact size).
      Parameters:
      books - array of books (may be null or contain nulls)
      maxPrice - maximum price (inclusive)
      Returns:
      compact array of books with price less than or equal to maxPrice
      Throws:
      IllegalArgumentException - if maxPrice is negative
    • filterByDecade

      public static Book[] filterByDecade(Book[] books, int decade)
      Filters books published in a specific decade. For example, decade 1990 includes years 1990-1999.
      Parameters:
      books - array of books (may be null or contain nulls)
      decade - the decade start year (e.g., 1990, 2000)
      Returns:
      compact array of books from that decade
    • sortByPrice

      public static void sortByPrice(Book[] books)
      Sorts books by price in ascending order (in-place). Nulls are moved to the end.
      Parameters:
      books - array to sort (modified in-place)
    • sortByYear

      public static void sortByYear(Book[] books)
      Sorts books by year in ascending order (in-place). Nulls are moved to the end.
      Parameters:
      books - array to sort (modified in-place)
    • averagePrice

      public static double averagePrice(Book[] books)
      Calculates the average price of books in the array.
      Parameters:
      books - array of books (may be null or contain nulls)
      Returns:
      average price, or 0.0 if array is null or empty
    • findOldest

      public static Book findOldest(Book[] books)
      Finds the oldest book (earliest publication year).
      Parameters:
      books - array of books (may be null or contain nulls)
      Returns:
      the oldest book, or null if array is null/empty
    • merge

      public static Book[] merge(Book[] arr1, Book[] arr2)
      Merges two book arrays into one, preserving all elements.
      Parameters:
      arr1 - first array (may be null)
      arr2 - second array (may be null)
      Returns:
      merged array containing all books from both arrays
    • removeDuplicates

      public static Book[] removeDuplicates(Book[] books)
      Removes duplicate books based on ISBN. Returns a compact array with unique books only.
      Parameters:
      books - array of books (may be null or contain nulls)
      Returns:
      compact array with duplicates removed
    • filterByYearRange

      public static Book[] filterByYearRange(Book[] books, int startYear, int endYear)
      Finds books within a year range (inclusive).
      Parameters:
      books - array of books
      startYear - start year (inclusive)
      endYear - end year (inclusive)
      Returns:
      compact array of books within the year range
    • countByDecade

      public static Map<Integer,Integer> countByDecade(Book[] books)
      Groups books by decade and returns a summary.
      Parameters:
      books - array of books
      Returns:
      map with decade as key and count as value
    • findLongestTitle

      public static Book findLongestTitle(Book[] books)
      Finds the book with the longest title.
      Parameters:
      books - array of books
      Returns:
      book with longest title, null if array is null/empty