Class BookArrayUtils
java.lang.Object
com.university.bookstore.utils.BookArrayUtils
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 Summary
Modifier and TypeMethodDescriptionstatic doubleaveragePrice(Book[] books) Calculates the average price of books in the array.static intcountBeforeYear(Book[] books, int yearCutoff) Counts books published before a given year.static intcountByAuthor(Book[] books, String author) Counts books by a specific author (case-insensitive, exact match).countByDecade(Book[] books) Groups books by decade and returns a summary.static Book[]filterByDecade(Book[] books, int decade) Filters books published in a specific decade.static Book[]filterByYearRange(Book[] books, int startYear, int endYear) Finds books within a year range (inclusive).static Book[]filterPriceAtMost(Book[] books, double maxPrice) Filters books with price at most the specified maximum.static BookfindLongestTitle(Book[] books) Finds the book with the longest title.static BookfindOldest(Book[] books) Finds the oldest book (earliest publication year).static Book[]Merges two book arrays into one, preserving all elements.static Book[]removeDuplicates(Book[] books) Removes duplicate books based on ISBN.static voidsortByPrice(Book[] books) Sorts books by price in ascending order (in-place).static voidsortByYear(Book[] books) Sorts books by year in ascending order (in-place).
-
Method Details
-
countBeforeYear
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
-
filterPriceAtMost
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
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
Sorts books by price in ascending order (in-place). Nulls are moved to the end.- Parameters:
books- array to sort (modified in-place)
-
sortByYear
Sorts books by year in ascending order (in-place). Nulls are moved to the end.- Parameters:
books- array to sort (modified in-place)
-
averagePrice
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
-
merge
-
removeDuplicates
-
filterByYearRange
-
countByDecade
-
findLongestTitle
-