brainfrz icon

Console Library

brainfrz | PRO | 01/24/16 09:19:38 PM UTC | 0 ⭐ | 770 👁️ | Never ⏰ | []
Java |

8.5 KB

|

None

|

0 👍

/

0 👎

/**************************************************************************************************
 *  Program Name:     Console Library
 *  Author:           Terry Weiss
 *  Date Written:     January 23, 2016
 *  Program Description:
 *  This library allows for text padding from the left or right, and text centering. Currently
 *      all console output is designed for an 80x24 output window. Currently whitespace escape
 *      characters are not supported and will interfere with right padding and centering.
 **************************************************************************************************/
 
public class Console
{
    //class constants
    public static final int WIDTH = 80; //Number of columns in console output
    public static final int ROWS  = 24; //Number of rows in console output
 
    //class variables
 
 
   /***********************************************************************************************
    * Method Name    : Console - padLeft
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * This method will generate a new string that is left-aligned and has been padded from the left
    *     edge of the screen.
    *
    * BEGIN Pad Left
    *    IF (pad is too big) THEN
    *        Align on right side of screen
    *    END IF
    *    Generate format string
    *    Return formatted string
    * END Pad Left
    ***********************************************************************************************/
    public static String padLeft(int pad, String str)
    {
        //local constants
 
        //local variables
        String format;  //Format string to be used by String.format()
 
        /********************  Start padLeft method  ********************/
 
        /*
         * IF (pad is too big) THEN
         *     Align on right side of screen
         * END IF
         */
        if (pad + str.length() > WIDTH)
        {
            pad = WIDTH - str.length();
        }//end if
 
        //Generate format string
        format = "%" + pad + "s%s";
 
        //Return formatted string
        return String.format(format, "", str);
    }//end method padLeft
 
   /***********************************************************************************************
    * Method Name    : Console - padRight
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * This method will generate a new string that is right-aligned and has been padded from the
    *     right edge of the screen.
    *
    * BEGIN Pad Right
    *    IF (pad is too big) THEN
    *        Align on left side of the screen
    *    END IF
    *    Generate format string
    *    Return formatted string
    * END Pad Right
    ***********************************************************************************************/
    public static String padRight(int pad, String str)
    {
        //local constants
 
        //local variables
        String format;  //Format string to be used by String.format()
 
        /********************  Start padRight method  ********************/
 
        /*
         * IF (pad is too big) THEN
         *     Align on left side of screen
         * END IF
         */
        if (pad + str.length() > WIDTH)
        {
            pad = 0;
        }//end if
 
        //Generate format string
        format = "%" + (WIDTH - pad) + "s";
 
        //Return formatted string
        return String.format(format, str);
    }//end method padRight
 
   /***********************************************************************************************
    * Method Name    : Console - center
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * This method will generate a new string that is center-aligned.
    *
    * BEGIN Center
    *    Calc pad length as (width - string length) / 2
    *    Generate format string
    *    Return formatted string
    * END Center
    ***********************************************************************************************/
    public static String center(String str)
    {
        //local constants
 
        //local variables
        int pad;        //Number of padding spaces before the string
        String format;  //Format string to be used by String.format()
 
        /********************  Start center method  ********************/
 
        //Calc pad length
        pad = (WIDTH - str.length()) / 2;
 
        //Generate format string
        format = "%" + pad + "s%s";
 
        //Return formatted string
        return String.format(format, "", str);
    }//end method center
    
    
   /***********************************************************************************************
    * Method Name    : Console - clearScreen
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * This method will push all text off the screen. The cursor will start at the bottom, and the
    *     output will need to be pushed up with fillScreen.
    *
    * BEGIN Clear Screen
    *    FOR (number of rows) DO
    *        Display a blank line
    *    END FOR
    * END Clear Screen
    ***********************************************************************************************/
    public static void clearScreen()
    {
        //local constants
 
        //local variables
 
        /********************  Start clearScreen method  ********************/
 
        /*
         * FOR (number of rows) DO
         *   Display a blank line
         * END FOR
         */
        for (int i = 0; i < ROWS; i++)
        {
            System.out.println();
        }//end for
    }//end method clearScreen
 
   /***********************************************************************************************
    * Method Name    : Console - fillScreen
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * This method will push the text on screen up to the top of the screen. Enter the number of
    *     rows currently on screen as input.
    *
    * BEGIN Fill Screen
    *    Calc rows
    *    FOR (number of rows to fill) DO
    *        Display a blank line
    *    END FOR
    * END Fill Screen
    ***********************************************************************************************/
    public static void fillScreen(int rows)
    {
        //local constants
 
        //local variables
 
        /********************  Start fillScreen method  ********************/
 
        //Calc rows
        rows = ROWS - rows;
 
        /*
         * FOR (number of rows to fill) DO
         *   Display a blank line
         * END FOR
         */
        for (int i = 0; i < rows; i++)
        {
            System.out.println();
        }//end for
    }//end method fillScreen
    
 
 
   /***********************************************************************************************
    * Method Name    : Console - main
    * Author         : Terry Weiss
    * Date           : January 24, 2016
    * Course/Section : CSC264-001
    * Program Description:
    * The main method is used for debugging and testing purposes.
    *
    * BEGIN Main
    *    Clear screen
    *    Draw debug ruler
    *    Test pad left
    *    Test pad right
    *    Test center
    *    Fill screen
    * END Main
    ***********************************************************************************************/
    public static void main(String[] args)
    {
        //local constants
 
        //local variables
 
        /********************  Start main method  ********************/
 
        //Clear screen
        Console.clearScreen();
        
        //Draw debug ruler
        System.out.println("123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
                + "123456789*");
                
        //Test pad left
        System.out.println(Console.padLeft(20, "pizza"));
        
        //Test pad right
        System.out.println(Console.padRight(20, "cake"));
        
        //Test center
        System.out.println(Console.center("tacos"));
        
        //Fill screen
        Console.fillScreen(4);
    }//end method main
}//end class Console

Comments