Created
August 13, 2023 15:59
-
-
Save rorrorome/f65b260270f398207338efa167e5e7fd to your computer and use it in GitHub Desktop.
페이징 처리
This file contains hidden or 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
| /* | |
| * 제로베이스 백엔드 스쿨 15기 | |
| * 한새롬 | |
| * Mission 1 깜짝과제 3. 페이징 처리 | |
| * | |
| * 페이지네비게이션의 html 코드를 출력하는 페이지네비게이션을 구현해보세요. | |
| * - 초기에 결정되는 값들은 | |
| * - 전체 게시글 수 | |
| * - 한 페이지당 보여지는 글의 수 | |
| * - 페이지네비게이션에서 보여주는 블럭수 | |
| * - 현재 페이지번호 | |
| * 현재 페이지번호에 해당하는 번호는 다른 페이지번호와 구분되어야 합니다. | |
| */ | |
| import java.io.BufferedWriter; | |
| import java.io.File; | |
| import java.io.FileWriter; | |
| import java.io.IOException; | |
| import java.util.Scanner; | |
| public class Mini0103 { | |
| public static class Pager { | |
| //전체게시글 수 | |
| long totalCount; | |
| //한 페이지 당 보여지는 글의 수 | |
| long post=10; | |
| //페이지 네비게이션에 보여지는 블럭 수 | |
| long pages=10; | |
| //현재 페이지 번호 | |
| long pageIndex; | |
| public Pager(long totalCount) { | |
| this.totalCount = totalCount; | |
| } | |
| public String html(long pageIndex) { | |
| this.pageIndex = pageIndex; | |
| //전체 페이지 수 | |
| long totalPage = (long) (totalCount/post) +1; | |
| //현재 네비게이션 바에 표시되는 페이지 번호들 | |
| long startPage = ((long) (pageIndex/pages))*pages +1; | |
| long lastPage = ((long) (pageIndex/pages)+1)*pages <= totalPage ? ((long) (pageIndex/pages)+1)*pages : totalPage; | |
| StringBuffer sb = new StringBuffer(); | |
| sb.append("<head></head>" | |
| + "<body>\r\n" | |
| + "<a href = '#'>[처음]</a>" | |
| + "<a href = '#'>[이전]</a>"); | |
| for(long i = startPage; i<=lastPage; i++) { | |
| if(i==pageIndex) { | |
| sb.append("<a href = '#' class='on'> "+i+"</a>"); | |
| }else { | |
| sb.append("<a href = '#'> "+i+"</a>"); | |
| } | |
| } | |
| sb.append("<a href = '#'> [다음]</a>" | |
| + "<a href = '#'>[마지막]</a>" | |
| + "</body>"); | |
| return sb.toString(); | |
| } | |
| } | |
| public static void main(String[] args) throws IOException{ | |
| long totalCount = 127; | |
| long pageIndex = 11; | |
| Pager pager = new Pager(totalCount); | |
| try { | |
| File file = new File("./pager.html"); | |
| BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
| writer.write(pager.html(pageIndex)); | |
| writer.close(); | |
| } catch(IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment