brainfrz

Console Library

Jan 24th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Console Library
  3.  *  Author:           Terry Weiss
  4.  *  Date Written:     January 23, 2016
  5.  *  Program Description:
  6.  *  This library allows for text padding from the left or right, and text centering. Currently
  7.  *      all console output is designed for an 80x24 output window. Currently whitespace escape
  8.  *      characters are not supported and will interfere with right padding and centering.
  9.  **************************************************************************************************/
  10.  
  11. public class Console
  12. {
  13.     //class constants
  14.     public static final int WIDTH = 80; //Number of columns in console output
  15.     public static final int ROWS  = 24; //Number of rows in console output
  16.  
  17.     //class variables
  18.  
  19.  
  20.    /***********************************************************************************************
  21.     * Method Name    : Console - padLeft
  22.     * Author         : Terry Weiss
  23.     * Date           : January 24, 2016
  24.     * Course/Section : CSC264-001
  25.     * Program Description:
  26.     * This method will generate a new string that is left-aligned and has been padded from the left
  27.     *     edge of the screen.
  28.     *
  29.     * BEGIN Pad Left
  30.     *    IF (pad is too big) THEN
  31.     *        Align on right side of screen
  32.     *    END IF
  33.     *    Generate format string
  34.     *    Return formatted string
  35.     * END Pad Left
  36.     ***********************************************************************************************/
  37.     public static String padLeft(int pad, String str)
  38.     {
  39.         //local constants
  40.  
  41.         //local variables
  42.         String format;  //Format string to be used by String.format()
  43.  
  44.         /********************  Start padLeft method  ********************/
  45.  
  46.         /*
  47.          * IF (pad is too big) THEN
  48.          *     Align on right side of screen
  49.          * END IF
  50.          */
  51.         if (pad + str.length() > WIDTH)
  52.         {
  53.             pad = WIDTH - str.length();
  54.         }//end if
  55.  
  56.         //Generate format string
  57.         format = "%" + pad + "s%s";
  58.  
  59.         //Return formatted string
  60.         return String.format(format, "", str);
  61.     }//end method padLeft
  62.  
  63.    /***********************************************************************************************
  64.     * Method Name    : Console - padRight
  65.     * Author         : Terry Weiss
  66.     * Date           : January 24, 2016
  67.     * Course/Section : CSC264-001
  68.     * Program Description:
  69.     * This method will generate a new string that is right-aligned and has been padded from the
  70.     *     right edge of the screen.
  71.     *
  72.     * BEGIN Pad Right
  73.     *    IF (pad is too big) THEN
  74.     *        Align on left side of the screen
  75.     *    END IF
  76.     *    Generate format string
  77.     *    Return formatted string
  78.     * END Pad Right
  79.     ***********************************************************************************************/
  80.     public static String padRight(int pad, String str)
  81.     {
  82.         //local constants
  83.  
  84.         //local variables
  85.         String format;  //Format string to be used by String.format()
  86.  
  87.         /********************  Start padRight method  ********************/
  88.  
  89.         /*
  90.          * IF (pad is too big) THEN
  91.          *     Align on left side of screen
  92.          * END IF
  93.          */
  94.         if (pad + str.length() > WIDTH)
  95.         {
  96.             pad = 0;
  97.         }//end if
  98.  
  99.         //Generate format string
  100.         format = "%" + (WIDTH - pad) + "s";
  101.  
  102.         //Return formatted string
  103.         return String.format(format, str);
  104.     }//end method padRight
  105.  
  106.    /***********************************************************************************************
  107.     * Method Name    : Console - center
  108.     * Author         : Terry Weiss
  109.     * Date           : January 24, 2016
  110.     * Course/Section : CSC264-001
  111.     * Program Description:
  112.     * This method will generate a new string that is center-aligned.
  113.     *
  114.     * BEGIN Center
  115.     *    Calc pad length as (width - string length) / 2
  116.     *    Generate format string
  117.     *    Return formatted string
  118.     * END Center
  119.     ***********************************************************************************************/
  120.     public static String center(String str)
  121.     {
  122.         //local constants
  123.  
  124.         //local variables
  125.         int pad;        //Number of padding spaces before the string
  126.         String format;  //Format string to be used by String.format()
  127.  
  128.         /********************  Start center method  ********************/
  129.  
  130.         //Calc pad length
  131.         pad = (WIDTH - str.length()) / 2;
  132.  
  133.         //Generate format string
  134.         format = "%" + pad + "s%s";
  135.  
  136.         //Return formatted string
  137.         return String.format(format, "", str);
  138.     }//end method center
  139.    
  140.    
  141.    /***********************************************************************************************
  142.     * Method Name    : Console - clearScreen
  143.     * Author         : Terry Weiss
  144.     * Date           : January 24, 2016
  145.     * Course/Section : CSC264-001
  146.     * Program Description:
  147.     * This method will push all text off the screen. The cursor will start at the bottom, and the
  148.     *     output will need to be pushed up with fillScreen.
  149.     *
  150.     * BEGIN Clear Screen
  151.     *    FOR (number of rows) DO
  152.     *        Display a blank line
  153.     *    END FOR
  154.     * END Clear Screen
  155.     ***********************************************************************************************/
  156.     public static void clearScreen()
  157.     {
  158.         //local constants
  159.  
  160.         //local variables
  161.  
  162.         /********************  Start clearScreen method  ********************/
  163.  
  164.         /*
  165.          * FOR (number of rows) DO
  166.          *   Display a blank line
  167.          * END FOR
  168.          */
  169.         for (int i = 0; i < ROWS; i++)
  170.         {
  171.             System.out.println();
  172.         }//end for
  173.     }//end method clearScreen
  174.  
  175.    /***********************************************************************************************
  176.     * Method Name    : Console - fillScreen
  177.     * Author         : Terry Weiss
  178.     * Date           : January 24, 2016
  179.     * Course/Section : CSC264-001
  180.     * Program Description:
  181.     * This method will push the text on screen up to the top of the screen. Enter the number of
  182.     *     rows currently on screen as input.
  183.     *
  184.     * BEGIN Fill Screen
  185.     *    Calc rows
  186.     *    FOR (number of rows to fill) DO
  187.     *        Display a blank line
  188.     *    END FOR
  189.     * END Fill Screen
  190.     ***********************************************************************************************/
  191.     public static void fillScreen(int rows)
  192.     {
  193.         //local constants
  194.  
  195.         //local variables
  196.  
  197.         /********************  Start fillScreen method  ********************/
  198.  
  199.         //Calc rows
  200.         rows = ROWS - rows;
  201.  
  202.         /*
  203.          * FOR (number of rows to fill) DO
  204.          *   Display a blank line
  205.          * END FOR
  206.          */
  207.         for (int i = 0; i < rows; i++)
  208.         {
  209.             System.out.println();
  210.         }//end for
  211.     }//end method fillScreen
  212.    
  213.  
  214.  
  215.    /***********************************************************************************************
  216.     * Method Name    : Console - main
  217.     * Author         : Terry Weiss
  218.     * Date           : January 24, 2016
  219.     * Course/Section : CSC264-001
  220.     * Program Description:
  221.     * The main method is used for debugging and testing purposes.
  222.     *
  223.     * BEGIN Main
  224.     *    Clear screen
  225.     *    Draw debug ruler
  226.     *    Test pad left
  227.     *    Test pad right
  228.     *    Test center
  229.     *    Fill screen
  230.     * END Main
  231.     ***********************************************************************************************/
  232.     public static void main(String[] args)
  233.     {
  234.         //local constants
  235.  
  236.         //local variables
  237.  
  238.         /********************  Start main method  ********************/
  239.  
  240.         //Clear screen
  241.         Console.clearScreen();
  242.        
  243.         //Draw debug ruler
  244.         System.out.println("123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
  245.                 + "123456789*");
  246.                
  247.         //Test pad left
  248.         System.out.println(Console.padLeft(20, "pizza"));
  249.        
  250.         //Test pad right
  251.         System.out.println(Console.padRight(20, "cake"));
  252.        
  253.         //Test center
  254.         System.out.println(Console.center("tacos"));
  255.        
  256.         //Fill screen
  257.         Console.fillScreen(4);
  258.     }//end method main
  259. }//end class Console
Advertisement
Add Comment
Please, Sign In to add comment