Skip to content

Instantly share code, notes, and snippets.

@zhyunk
Last active April 17, 2023 05:42
Show Gist options
  • Save zhyunk/fe053fd8ae77dce5b077d21eb500d52c to your computer and use it in GitHub Desktop.
Save zhyunk/fe053fd8ae77dce5b077d21eb500d52c to your computer and use it in GitHub Desktop.
깜짝 과제 3
// 23.04.12
// 김지현
public class Main3 {
public static void main(String[] args) {
int totalCount = 127; // 전체 게시글 수
int pageIndex = 3; // 현재 페이지 번호
int pageSize = 10; // 페이지 네비게이션에서 보여주는 블럭 수
int recordCount = 3; // 한 페이지당 보여지는 글의 수
new Pager(totalCount)
.setPageSize(pageSize)
.setRecordCount(recordCount)
.html(pageIndex);
}
static class Pager {
int totalPageCount; // 전체 페이지 수
int startPage; // 시작 페이지 번호
int endPage; // 마지막 페이지 번호
int totalCount; // 전체 게시글 수
int recordCount; // 한 페이지당 보여지는 글
int pageSize; // 페이지 네비게이션에서 보여주는 블럭 수
public Pager(int totalCount) {
// 전체 게시글 수 입력받음
this.totalCount = totalCount;
}
// 전체 페이지 수 계산
private void makeTotalPageCount() {
totalPageCount = (totalCount - 1) / recordCount + 1;
}
// 시작 페이지 계산
private void makeStartPage(int pageIndex) {
startPage = ((pageIndex - 1) / pageSize) * pageSize + 1;
}
// 마지막 페이지 계산
private void makeEndPage() {
endPage = startPage + pageSize - 1;
if (endPage > totalPageCount) {
endPage = totalPageCount;
}
}
// html 작성 & 출력
public void html(int pageIndex) {
makeTotalPageCount();
makeStartPage(pageIndex);
makeEndPage();
StringBuilder sb = new StringBuilder();
sb.append("<a href='#'>[처음]</a>\n");
sb.append("<a href='#'>[이전]</a>\n");
sb.append("\n");
for(int i = startPage; i <= endPage; i++) {
if (i == pageIndex) {
sb.append( "<a href='#' class='on'>");
} else {
sb.append("<a href='#'>");
}
sb.append(i).append("</a>\n");
}
sb.append("\n");
sb.append("<a href='#'>[다음]</a>\n");
sb.append("<a href='#'>[마지막]</a>\n");
System.out.println(sb);
}
public Pager setRecordCount(int recordCount) {
this.recordCount = recordCount;
return this;
}
public Pager setPageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment