6 kyu

Implement Pagination

Description:

Pagination is about dividing up results into pages with a fixed number of items on each page, your task is to write a generic pagination class that manages pagination for any class implementing IEnumerable<T>

The class should take in the IEnumerable<T> to manage in the constructor and must have the following properties:

IEnumerable<T> Items // gets the items for the current page index
int CurrentPage      // gets/sets the current page index, starting at 1
int ItemsPerPage     // gets/sets the number of items to return on each page, default 10 items
int Total            // gets the total number of items in the source
int TotalPages       // gets total number of pages
  • If CurrentPage is set to <= 0 it should default back to page 1.
  • If CurrentPage is set to > TotalPages Items should be empty (not null).
  • If ItemsPerPage is set to <= 0 it should default back to 10 items.
  • Properties listed as only gettable should not be settable.
  • Settable properties can be set in any order.

For example:

var pagination = new Pagination(new List<int>() {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23});

var first = pagination.Items.ToList(); // first contains the list 1,2,3,4,5,6,7,8,9,10

pagination.CurrentPage = 2;
var second = pagination.Items.ToList(); // second contains the list 11,12,13,14,15,16,17,18,19,20

pagination.CurrentPage = 3;
var last = pagination.Items.ToList();  // last contains the list 21,22,23

int total = pagination.Total; // total is set to 23
int pages = pagination.TotalPages; // pages is set to 3
Fundamentals

Similar Kata:

Stats:

CreatedJan 9, 2017
PublishedJan 9, 2017
Warriors Trained288
Total Skips16
Total Code Submissions807
Total Times Completed104
C# Completions104
Total Stars13
% of votes with a positive feedback rating99% of 44
Total "Very Satisfied" Votes43
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments8
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • user4850992 Avatar
  • smile67 Avatar
  • kazk Avatar
  • hobovsky Avatar
Ad