Skip to content

Instantly share code, notes, and snippets.

@Starlight258
Last active February 12, 2024 08:03
Show Gist options
  • Save Starlight258/3e1a99783d971b0761c72771963fee57 to your computer and use it in GitHub Desktop.
Save Starlight258/3e1a99783d971b0761c72771963fee57 to your computer and use it in GitHub Desktop.
3.java
// 김명지
public class Main {
public static class Pager{
private final long totalCount; // 전체 게시글 수
public Pager(long totalCount) {
this.totalCount = totalCount;
}
public String html(long currentPage) {
// 한 페이지당 보여지는 글의 수
long postsPerPage = 10;
long totalPageCount = (totalCount + postsPerPage - 1) / postsPerPage; // 올림
// 페이지 네비게이션 크기
long navigationSize = 10;
long currentBlock = (currentPage - 1) / navigationSize; // 페이지 인덱스는 0부터 시작
long startPage = currentBlock * navigationSize + 1; // 실제 페이지는 1부터 시작
long endPage = Math.min(startPage + navigationSize - 1, totalPageCount);
// html 생성 시작
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<a href=\"#\">[처음]</a>\n");
// 이전 블록
if (startPage > navigationSize) {
htmlBuilder.append(createPageItem("[이전]"));
htmlBuilder.append("\n");
}
// 페이지 번호
for (long page = startPage; page <= endPage; page++) {
if (page == currentPage) {
htmlBuilder.append("<a href=\"#\" class=\"on\">" + page + "</a>\n");
} else {
htmlBuilder.append(createPageItem(Long.toString(page)));
}
}
// 다음 블록
if (endPage <= totalPageCount) {
htmlBuilder.append("\n");
htmlBuilder.append(createPageItem("[다음]"));
htmlBuilder.append(createPageItem("[마지막]"));
}
return htmlBuilder.toString();
}
private String createPageItem(String text) {
return "<a href=\"#\">" + text + "</a>\n";
}
}
public static void main(String[] args) {
long totalCount = 127;
long pageIndex = 1;
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