PaginationHelper

kata programming

مساله:

برای این تمرین تسلط خود را برای page-fu بیشتر خواهید کرد. شما باید کلاس PaginationHelper را کامل کنید، که ابزاری برای کمک کردن به اطلاعات صفحه بندی کوئری می باشد که مربوط به آرایه ها است.

این کلاس این گونه طراحی شده است که آرایه ای از مقادیر را بگیرد و مشخص کند چه تعداد از آیتم ها در هر صفحه قرار می گیرند. نوع مقادیر در آرایه می باشد که به ما مربوط نیست.

در ادامه مثال هایی را می بینید که نشان می دهد این کلاس چگونه کار می کند:

PaginationHelper<Character> helper = new PaginationHelper(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f'), 4);
helper.pageCount(); //should == 2
helper.itemCount(); //should == 6
helper.pageItemCount(0); //should == 4
helper.pageItemCount(1); // last page - should == 2
helper.pageItemCount(2); // should == -1 since the page is invalid

// pageIndex takes an item index and returns the page that it belongs on
helper.pageIndex(5); //should == 1 (zero based index)
helper.pageIndex(2); //should == 0
helper.pageIndex(20); //should == -1
helper.pageIndex(-10); //should == -1

Description:

For this exercise you will be strengthening your page-fu mastery. You will complete the PaginationHelper class, which is a utility class helpful for querying paging information related to an array.

The class is designed to take in an array of values and an integer indicating how many items will be allowed per each page. The types of values contained within the collection/array are not relevant.

The following are some examples of how this class is used:

PaginationHelper<Character> helper = new PaginationHelper(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f'), 4);
helper.pageCount(); //should == 2
helper.itemCount(); //should == 6
helper.pageItemCount(0); //should == 4
helper.pageItemCount(1); // last page - should == 2
helper.pageItemCount(2); // should == -1 since the page is invalid

// pageIndex takes an item index and returns the page that it belongs on
helper.pageIndex(5); //should == 1 (zero based index)
helper.pageIndex(2); //should == 0
helper.pageIndex(20); //should == -1
helper.pageIndex(-10); //should == -1

import java.util.List;
public class PaginationHelper<I> {
    private List<I> collection;
    private int itemsPerPage;
    public PaginationHelper(List<I> collection, int itemsPerPage){
        this.collection = collection;
        this.itemsPerPage = itemsPerPage;
    }    
    public int itemCount(){
        return collection.size();
    }    
    public int pageCount(){
       int count = itemCount() % itemsPerPage == 0 ? itemCount() / itemsPerPage : itemCount() / itemsPerPage + 1;
        return count;
    }    
    public int pageItemCount(int pageIndex){
        if(pageIndex >= pageCount()){
            return -1;
        }            
        int count = pageIndex < pageCount() - 1 ? itemsPerPage : itemCount() - itemsPerPage * (pageCount() - 1);
        return count;
    }        
    public int pageIndex(int itemIndex){
        int pageIndex = -1;
        if(itemIndex >= itemCount() || itemIndex < 0){
            return pageIndex;
        }    
        pageIndex = itemIndex / itemsPerPage;
        return pageIndex;
    }    
}
import java.util.List;
import java.lang.Math;

// TODO: complete this object/class

public class PaginationHelper<I> {

  private final int itemsPerPage;
  private final List<I> collection;
  
  /**
   * The constructor takes in an array of items and a integer indicating how many
   * items fit within a single page
   */
  public PaginationHelper(List<I> collection, int itemsPerPage) {
    this.itemsPerPage = itemsPerPage;
    this.collection = collection;
  }
  
  /**
   * returns the number of items within the entire collection
   */
  public int itemCount() {
    return collection.size();
  }
  
  /**
   * returns the number of pages
   */
  public int pageCount() {
    return (int)Math.ceil(itemCount() / (float)itemsPerPage);
  }
  
  /**
   * returns the number of items on the current page. page_index is zero based.
   * this method should return -1 for pageIndex values that are out of range
   */
  public int pageItemCount(int pageIndex) {
    if (pageIndex >= pageCount() || pageIndex < 0) 
      return -1;
    else if (pageIndex < pageCount()-1)
      return itemsPerPage;
    else
      return itemCount() % itemsPerPage;
  }
  
  /**
   * determines what page an item is on. Zero based indexes
   * this method should return -1 for itemIndex values that are out of range
   */
  public int pageIndex(int itemIndex) {
    if (itemIndex >= itemCount() || itemIndex < 0)
      return -1;
    else
      return itemIndex / itemsPerPage;  
  }
}
import java.util.List;

public class PaginationHelper<I> {

  int item_count;
  int items_per_page;
  int page_count;

  /**
   * The constructor takes in an array of items and a integer indicating how many
   * items fit within a single page
   */
  public PaginationHelper(List<I> collection, int itemsPerPage) {
    item_count = collection.size();
    items_per_page = itemsPerPage;
    page_count = (item_count + items_per_page - 1) / items_per_page;    
  }
  
  /**
   * returns the number of items within the entire collection
   */
  public int itemCount() {
    return item_count; 
  }
  
  /**
   * returns the number of pages
   */
  public int pageCount() {
    return page_count; 
  }
  
  /**
   * returns the number of items on the current page. page_index is zero based.
   * this method should return -1 for pageIndex values that are out of range
   */
  public int pageItemCount(int pageIndex) {
    if (pageIndex < 0 || pageIndex >= page_count)
      return -1;
    else if (pageIndex == page_count - 1)
      return item_count % items_per_page;
    else
      return items_per_page;  
  }
  
  /**
   * determines what page an item is on. Zero based indexes
   * this method should return -1 for itemIndex values that are out of range
   */
  public int pageIndex(int itemIndex) {
    if (itemIndex < 0 || itemIndex >= item_count)
      return -1;
    else
      return itemIndex / items_per_page;  
  }
}
import java.util.List;

public class PaginationHelper<I> {
 
   public List<I> items;
   public int perPage;

  public PaginationHelper(List<I> collection, int itemsPerPage) {
    items = collection;
    perPage = itemsPerPage;
  }
  
  public int itemCount() {
     return items.size();
  }

  public int pageCount() {
    return this.itemCount() / perPage + 1;
  }
  
  public int pageItemCount(int pageIndex) {
    return (pageIndex >= this.pageCount())? -1 : ((pageIndex == this.pageCount()-1)? this.itemCount() % perPage : perPage);
  }

  public int pageIndex(int itemIndex) {
    return (itemIndex < 0 || itemIndex > this.itemCount()-1)? -1 : itemIndex / perPage;
  }
}

دیدگاهتان را بنویسید