Skip to content

Instantly share code, notes, and snippets.

@dkwktm45
Created July 12, 2023 07:38
Show Gist options
  • Select an option

  • Save dkwktm45/70822c3c25496dbc60ed00e3ba252581 to your computer and use it in GitHub Desktop.

Select an option

Save dkwktm45/70822c3c25496dbc60ed00e3ba252581 to your computer and use it in GitHub Desktop.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class mission3 {
public static void main(String[] args) {
long totalCount = 127;
long pageIndex = 3;
Paper paper = new Paper(totalCount, 10);
paper.setTotalPage(totalCount);
try {
File file = new File("C:\\Users\\user\\Desktop\\paper.html");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("<head>");
writer.write("\r\n\t <meta charset=\\\"UTF-8\\\"/>");
writer.write("\r\n\t <style>\r\n\t\t");
writer.write(".on {color:red;}");
writer.write("\r\n\t </style>\r\n</head>");
for (int i = 1; i <= paper.totalPage; i++) {
writer.write("<div>");
writer.write(paper.html(i));
writer.write("</div>");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static class Paper {
public Paper(long totalCount, long setPage) {
this.totalCount = totalCount;
this.setPage = setPage;
}
long totalCount;
long setPage;
long totalPage;
public String html(int index) {
String out = "";
out += " <a href='#'>[처음]</a> ";
out += " <a href='#'>[이전]</a> ";
long[] startAndEnd = getStartEnd(index);
for (long i = startAndEnd[0]; i <= startAndEnd[1]; i++) {
if (i == index) out += String.format(" <a href='#' class='on'>%s</a> ", i);
else out += String.format(" <a href='#'>%s</a> ", i);
}
out += "<a href='#'> [다음] </a>";
out += "<a href='#'> [마지막] </a>";
return out;
}
private long[] getStartEnd(int index) {
long[] arr = new long[2];
// start 값 설정
if (index % this.setPage == 0) {
arr[0] = this.setPage * (index / this.setPage - 1) + 1;
} else {
arr[0] = this.setPage * (index / this.setPage) + 1;
}
// end 값 설정
arr[1] = arr[0] - 1 + (this.setPage);
if (arr[1] > this.totalPage) arr[1] = this.totalPage;
return arr;
}
public void setTotalPage(long totalCount) {
this.totalPage = totalCount / 10 + (totalCount % 10 > 0 ? 1 : 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment