Here you can find the source of printBox(List
Parameter | Description |
---|---|
data | a parameter |
public static void printBox(List<String> data)
//package com.java2s; /*/*from www . j a v a 2 s . c o m*/ * Copyright (c) 2009 The Regents of the University of California. * All rights reserved. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the above * copyright notice and the following two paragraphs appear in all copies * of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * ENHANCEMENTS, OR MODIFICATIONS. * */ import java.util.List; public class Main { /** * print a box * * @param data */ public static void printBox(List<String> data) { if (data == null || data.size() <= 0) { return; } int size = 0; for (String line : data) { if (line.length() > size) { size = line.length(); } } int adjustedSize = size + 4; //Add 4 to the size for "| " and " |" in the box. System.out.println(singleHorizontalLine(adjustedSize)); for (String line : data) { System.out.println(boxLine(line, adjustedSize)); } System.out.println(singleHorizontalLine(adjustedSize)); } /** * create a single horiz line * * @param size * @return */ public static String singleHorizontalLine(int size) { return horizontalLine(size, '-'); } /** * create a box line * * @param line * @param size * @return */ private static String boxLine(String line, int size) { return "| " + line + getSpaces(size - line.length()) + " |"; } /** * create a horiz line * * @param size * @param c * @return */ private static String horizontalLine(int size, char c) { String line = ""; for (int i = 0; i < size; i++) { line += c; } return line; } /** * return n spaces * * @param n * @return */ private static String getSpaces(int n) { String spaces = ""; for (int i = 0; i < n; i++) { spaces += " "; } return spaces; } }