|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.concurrent.atomic.AtomicInteger; |
| 4 | + |
| 5 | +// TODO: complete this object/class |
| 6 | + |
| 7 | +public class PaginationHelper<I> { |
| 8 | + |
| 9 | + public ArrayList<ArrayList<I>> paginatedItems; |
| 10 | + public int itemsPerPage; |
| 11 | + public int totalPages; |
| 12 | + public int totalItems; |
| 13 | + |
| 14 | + /** |
| 15 | + * The constructor takes in an array of items and a integer indicating how many |
| 16 | + * items fit within a single page |
| 17 | + */ |
| 18 | + public PaginationHelper(List<I> collection, int itemsPerPage) { |
| 19 | + collection.forEach(e -> System.out.println(e)); |
| 20 | + System.out.printf("itemsperpage = %d", itemsPerPage); |
| 21 | + this.paginatedItems = new ArrayList<ArrayList<I>>(); |
| 22 | + this.itemsPerPage = itemsPerPage; |
| 23 | + this.totalPages = (collection.size() / this.itemsPerPage) + 1; |
| 24 | + this.totalItems = collection.size(); |
| 25 | + int counter = 0; |
| 26 | + ArrayList<I> accumulatedPage = new ArrayList<I>(); |
| 27 | + for (final I eachItem : collection) { |
| 28 | + if (counter == itemsPerPage) { |
| 29 | + this.paginatedItems.add(accumulatedPage); |
| 30 | + accumulatedPage.clear(); |
| 31 | + accumulatedPage.add(eachItem); |
| 32 | + counter = 1; |
| 33 | + } else { |
| 34 | + counter++; |
| 35 | + accumulatedPage.add(eachItem); |
| 36 | + } |
| 37 | + } |
| 38 | + if (accumulatedPage.size() > 0) { |
| 39 | + this.paginatedItems.add(accumulatedPage); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * returns the number of items within the entire collection |
| 45 | + */ |
| 46 | + public int itemCount() { |
| 47 | + return this.totalItems; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * returns the number of pages |
| 52 | + */ |
| 53 | + public int pageCount() { |
| 54 | + return this.totalPages; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * returns the number of items on the current page. page_index is zero based. |
| 59 | + * this method should return -1 for pageIndex values that are out of range |
| 60 | + */ |
| 61 | + public int pageItemCount(int pageIndex) { |
| 62 | + System.out.println("itemcount = ", pageIndex); |
| 63 | + return this.paginatedItems.get(pageIndex).size(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * determines what page an item is on. Zero based indexes |
| 68 | + * this method should return -1 for itemIndex values that are out of range |
| 69 | + */ |
| 70 | + public int pageIndex(int itemIndex) { |
| 71 | + if (this.totalItems == 0) { |
| 72 | + return -1; |
| 73 | + } |
| 74 | + if (this.totalItems < (itemIndex + 1)) { |
| 75 | + return -1; |
| 76 | + } |
| 77 | + return (itemIndex + 1) / this.itemsPerPage; |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(string[] args) { |
| 81 | + List<Integer> items = new ArrayList<>(); |
| 82 | + for (int i = 1; i <= 24; i++) { |
| 83 | + items.add(i); |
| 84 | + } |
| 85 | + PaginationHelper<Integer> pagHelper = new PaginationHelper(items, 10); |
| 86 | + } |
| 87 | +} |
0 commit comments