In most programs, the paging algorithm is required. Obviously, the paging algorithm must be independent. If the business logic is also mixed together, it is a pain to modify it. In fact, the design of the paging algorithm is like solving the equation. Input several variables to find several other variables. The following is a simple design: the total number of input records, the current page, the number of redirect pages, and the index of the output request. Of course, you can also modify the algorithm to output other information.
Package paging; public class page {/*** the number of records displayed on each page, that is, the number of your requests */public static final int pagesize = 20; /*** total number of records */private int records;/*** count the current page from 1 */private int now_page; /*** total page number */Public int pages;/*** request location, which indicates the number of records you have requested */Public int request_index; /*** whether the Page Object is successfully constructed */Public Boolean OK = true; Public page () {}/ *** is generally used in the first request, because you do not know the total number of records */Public page (INT now_page) {This (pagesize, now_page, 0 );} Public page (INT now_page, int go_count) {This (pagesize, now_page, go_count );} /***** @ Param records * Total Record Book * @ Param now_page * index of the current page * @ Param go_count * jumps to several pages (negative numbers are supported) */Public page (INT records, int now_page, int go_count) {If (now_page-1) * pagesize> = records | now_page <= 0) {system. out. println ("failed to construct page-1! "); OK = false;} If (now_page + go_count <= 0 | (now_page + go_count-1) * pagesize> = records) {system. out. println ("failed to construct page-2! "); OK = false;} This. now_page = now_page; this. records = records; this. request_index = (this. now_page + go_count-1) * pagesize; If (this. records % pagesize = 0) {This. pages = This. records/pagesize;} else {This. pages = This. records/pagesize + 1 ;}@ overridepublic string tostring () {return "pages:" + pages + "" + "request_index:" + request_index ;}}