コード例 #1
0
 public void viewDetail(ArticleDTO article) {
   int rangeLimit = 10;
   int word = 0;
   char[] content = (article.getContent()).toCharArray();
   StringBuilder contents = new StringBuilder();
   System.out.println("ID: " + article.getId());
   System.out.println("Author: " + article.getAuthor());
   System.out.println("Title: " + article.getTitle());
   // System.out.println("Content: " + article.getContent());
   for (int i = 0; i < content.length; i++) {
     contents.append(content[i]);
     if (content[i] == ' ') {
       word++;
       if (word == rangeLimit) {
         contents.append(" ");
         word = 0;
       }
     }
   }
   System.out.println("content: \n" + contents.toString().trim());
   System.out.println("Publish Date: " + article.getPublishDate());
   System.out.println("\n\n");
 }
コード例 #2
0
  /*
   * Method drawTable() Use for draw table, header, and menu bar articles
   * parameter is the articles we want to put into the table
   */
  public void drawTable(List<ArticleDTO> articles) {
    int[] maxColumns = new int[] {14, 24, 34, 25}; // maxColumnsLength(articles);
    // /* Find maximum
    // length for every
    // columns*/
    int totalLenght = sumColumnsLength(maxColumns); /*
														 * calculate total
														 * length
														 */
    String table = "";
    /* Header Block */
    header(maxColumns, totalLenght); /* Output header */
    // First line of table;
    table += addHorizontalLine(table, maxColumns, topLeft, middleTop, topRight);
    // Add Header Title;
    int index = 0;
    for (String header : headers) {
      table += Character.toString(verticalLine); // "?";
      // Add spaces;
      table = addLetter(table, ' ', (maxColumns[index] - header.length()) / 2);
      // Add header name;
      table += header;
      // Add spaces;
      int totalLength =
          maxColumns[index] - (maxColumns[index] - header.length()) / 2 - header.length();
      table = addLetter(table, ' ', totalLength);
      // (maxColumns[index] - header.length()) / 2);

      index++;
    }
    table += Character.toString(verticalLine) + "\n"; /* Add vertical line */

    /* End of Header Block */

    // Body
    final int PADDING = 2;
    for (ArticleDTO article : articles) {
      // Horizontal Line of every records;
      table += addHorizontalLine(table, maxColumns, middleLeft, middleCenter, middleRight);

      // Add ID
      table += Character.toString(verticalLine);
      table = addLetter(table, ' ', PADDING); // Add padding 1 space;
      table += article.getId(); // Add ID
      table =
          addLetter(
              table, ' ', maxColumns[0] - Integer.toString(article.getId()).length() - PADDING);
      // End Add ID;

      // Add Author
      table += Character.toString(verticalLine);
      table = addLetter(table, ' ', PADDING); // Add padding space;
      if (article.getAuthor().length() > 22) {
        table += article.getAuthor().substring(0, 17) + "..."; // Add
        // Author
        table = addLetter(table, ' ', PADDING);
      } else {
        table += article.getAuthor(); // Add Author
      }
      table = addLetter(table, ' ', maxColumns[1] - article.getAuthor().length() - PADDING);
      // End Add Author;

      // Add Title
      table += Character.toString(verticalLine);
      table = addLetter(table, ' ', PADDING);
      if (article.getTitle().length() > 32) {
        table += article.getTitle().substring(0, 27) + "...";
        table = addLetter(table, ' ', PADDING);
      } else table += article.getTitle();
      table = addLetter(table, ' ', maxColumns[2] - article.getTitle().length() - PADDING);
      // End Add Title;

      // Add Publish Date;
      table += Character.toString(verticalLine);
      table = addLetter(table, ' ', PADDING);
      table += article.getPublishDate();
      table = addLetter(table, ' ', maxColumns[3] - article.getPublishDate().length() - PADDING);

      table += Character.toString(verticalLine);
      table += "\n";
    }
    // End of Body

    // Footer;
    table += addHorizontalLine(table, maxColumns, buttomLeft, middleButtom, buttomRight);
    // End of Footer;
    System.out.println(table);
    System.out.println("Total Records:" + totalRecord); /*
															 * Output total
															 * records
															 */
    System.out.println("Pages: " + (this.currentPage + 1) + "/" + totalPage); /*
																				 * Output
																				 * current
																				 * page
																				 * and
																				 * total
																				 * pages
																				 */
    System.out.println(messageTable);
    messageTable = "Message: ";
    System.out.println();
    // Menu bar;
    menu(maxColumns, totalLenght); /* Output Menu Bar */
  }