Created
September 5, 2023 01:37
-
-
Save kingseungil/21cd100aa842f1652bf7b08b2cef666e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package surprise_assignment; | |
public class Pager { | |
private final long totalCount; | |
public Pager(long totalCount) { | |
this.totalCount = totalCount; | |
} | |
public String html(long pageIndex) { | |
StringBuilder output = new StringBuilder(); | |
int itemsPerPage = 10; | |
long totalPages = (totalCount + itemsPerPage - 1) / itemsPerPage; | |
long startPage = ((pageIndex - 1) / itemsPerPage) * itemsPerPage + 1; // 1, 11, 21, 31, ... | |
long endPage = Math.min(startPage + itemsPerPage - 1, totalPages); | |
output.append("<a href='#'>[처음]</a>").append("\n"); | |
output.append("<a href='#'>[이전]</a>").append("\n").append("\n"); | |
for (long i = startPage; i <= endPage; i++) { | |
if (i == pageIndex) { | |
output.append("<a href='#' class='on'>").append(i).append("</a>").append("\n"); | |
} else { | |
output.append("<a href='#'>").append(i).append("</a>").append("\n"); | |
} | |
} | |
if (pageIndex < totalPages) { | |
output.append("\n").append("<a href='#'>[다음]</a>").append("\n"); | |
} | |
output.append("<a href='#'>[마지막]</a>"); | |
return output.toString(); | |
} | |
public static void main(String[] args) { | |
long totalCount = 201; | |
long pageIndex = 21; | |
Pager pager = new Pager(totalCount); | |
System.out.println(pager.html(pageIndex)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment