Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Вывод:
- -> 1 <- | 2 | 3 | 4 | 5
- 1 | -> 2 <- | 3 | 4 | 5
- 1 | 2 | -> 3 <- | 4 | 5
- 2 | 3 | -> 4 <- | 5 | 6
- 3 | 4 | -> 5 <- | 6 | 7
- 4 | 5 | -> 6 <- | 7 | 8
- 5 | 6 | -> 7 <- | 8 | 9
- 6 | 7 | -> 8 <- | 9 | 10
- 7 | 8 | -> 9 <- | 10 | 11
- 8 | 9 | -> 10 <- | 11 | 12
- 9 | 10 | -> 11 <- | 12 | 13
- 10 | 11 | -> 12 <- | 13 | 14
- 11 | 12 | -> 13 <- | 14 | 15
- 12 | 13 | -> 14 <- | 15 | 16
- 13 | 14 | -> 15 <- | 16 | 17
- 14 | 15 | -> 16 <- | 17 | 18
- 15 | 16 | -> 17 <- | 18 | 19
- 16 | 17 | -> 18 <- | 19 | 20
- 16 | 17 | 18 | -> 19 <- | 20
- 16 | 17 | 18 | 19 | -> 20 <-*/
- public final class ListPaginatorPages {
- public static void main(String[] args) {
- final int minPage = 1;
- final int maxPage = 20;
- final Page page = new Page(minPage, maxPage);
- for (int currentPage = minPage; currentPage <= maxPage; currentPage++) {
- page.apply(currentPage);
- System.out.println(page);
- }
- }
- private static final class Page {
- private final int minPage, maxPage;
- private final List<String> pages;
- Page(int minPage, int maxPage) {
- this.minPage = minPage;
- this.maxPage = maxPage;
- this.pages = new ArrayList<>();
- apply(minPage);
- }
- void apply(int currentPage) {
- if (!(currentPage >= this.minPage && currentPage <= this.maxPage)) {
- throw new IllegalArgumentException();
- }
- this.pages.clear();
- int minBorder = this.minPage, maxBorder = this.maxPage;
- while ((maxBorder - minBorder) != 4) {
- if ((minBorder < (currentPage - 2))) {
- minBorder++;
- }
- if ((maxBorder > (currentPage + 2))) {
- maxBorder--;
- }
- }
- for (int i = minBorder; i <= maxBorder; i++) {
- this.pages.add(i == currentPage ? String.format("-> %d <-", i) : Integer.toString(i));
- }
- }
- @Override
- public String toString() {
- return String.join(" | ", this.pages);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement