brainfrz icon

LoginInfo

brainfrz | PRO | 02/02/16 03:00:09 AM UTC | 0 ⭐ | 798 👁️ | Never ⏰ | []
Java |

21.25 KB

|

None

|

0 👍

/

0 👎

/**************************************************************************************************
 * Program Name   : Week 2 - Login Info
 * Author         : Terry Weiss
 * Date           : January 29, 2016
 * Course/Section : CSC264 - 001
 * Program Description: This program will create usernames and passwords for
 *     students at OCC using their first and last name. The username will be
 *     the student's first two letters of their first name, followed by the
 *     first three letters of their last name, followed by a random digit
 *     1 and 3. If either name is too short their full first/last name is used.
 *     All usernames are in all lowercase letters. The password will default to
 *     the user's first name (in all lower case letters). The usernames and
 *     passwords each in their own array. The user may create up to 10
 *     username/password combinations. Duplicate usernames are prevented by
 *     adding another digit (1, 2, or 3) onto the end of the duplicate username,
 *     thus creating a unique username. When the user stops creating usernames,
 *     all of the usernames and passwords are displayed on the screen.
 *
 * Methods:
 * -------
 * Main           - Reads the names, creates the login info and displays them
 * Prompt Name    - Prompts for a first and last name or quit value
 * Random Number  - Generates a random number within range (1 through 3 inclusive)
 * Generate ID    - Generates the ID using first and last name
 * Generate Pwd   - Generates the passwords using their first name in lower case
 * Unique ID      - Goes through ID list confirming each ID is unique or replacing
 * Display Logins - Displays a list of all the IDs and passwords
 **************************************************************************************************/
 
import java.util.Random;
 
class LoginInfoTTW
{
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Main
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method will read a name entered by the user. It
     *     then creates a new String with the name centered for console output,
     *     and displays it.
     *
     * BEGIN main
     *    Init location to 0
     *    Clear screen
     *    Display input title
     *    Prompt name {prompt name method, returning name array}
     *    WHILE (name is array of two elements)
     *        Generate and assign ID to element in ID list {generate ID method}
     *        Generate and assign password to element in Pwd list {generate pwd method}
     *        Increment location
     *        IF (array is full)
     *            Set name to array of one element
     *        ELSE
     *            Display blank line
     *            Prompt name {prompt name method}
     *            IF (first name is quit string) THEN
     *                Set ID at current location to quit string
     *            END IF
     *        END IF
     *    END WHILE
     *    Check all IDs are unique {uniqueID method}
     *    Clear screen
     *    Display output title
     *    Display all login info {display logins method}
     * END Read Name
     **********************************************************************************************/
    public static void main(String[] args)
    {
        //Local constants
        final int MAX_LOGINS   = 10;                //Maximum number of logins
        final String QUIT_STR  = "done";            //Sentinel value for ending input
        final int FIRST_NAME   = 0;                 //Location of first name in names array
        final int LAST_NAME    = 1;                 //Location of last name in names array
        final int MIN_ID_NUM   = 1;                 //Minimum legal number in ID
        final int MAX_ID_NUM   = 3;                 //Maximum legal number in ID
 
        //Local variables
        int location;                               //Location of ID/Password in their arrays
        String[] idList  = new String[MAX_LOGINS];  //List of IDs
        String[] pwdList = new String[MAX_LOGINS];  //List of passwords
        String[] names;                             //First and last name of student
 
        //Utility class objects
        final Library64 myLib  = new Library64();   //Console control library
        final Random generator = new Random();      //Random number generator
 
        /*************************************START MAIN METHOD************************************/
 
        //Init location to 0
        location = 0;
 
        //Clear screen
        myLib.clrscr();
 
        //Display input title
        System.out.println("\n");
        System.out.println(Console.center("Name Entry"));
        System.out.println();
 
        //Prompt name {prompt name method, returning name array}
        names = promptName(QUIT_STR);
 
        //WHILE (name is array of two elements)
        while (names.length == 2)
        {
 
            //Generate and assign ID to element in ID list {generate ID method}
            idList[location]  = generateID(names[FIRST_NAME], names[LAST_NAME], MIN_ID_NUM,
                                             MAX_ID_NUM, generator);
 
            //Generate and assign password to element in Pwd list {generate pwd method}
            pwdList[location] = generatePwd(names[FIRST_NAME]);
 
            //Increment location
            location++;
 
            //IF (array is full)
            if (location == MAX_LOGINS)
            {
 
                //Set name to array of one element
                names    = new String[1];
                names[0] = QUIT_STR;
            }
            else
            {
 
                //Display blank line
                System.out.println();
 
                //Prompt name {prompt name method}
                names = promptName(QUIT_STR);
 
                //IF (first name is quit string) THEN
                if(names[0].equals(QUIT_STR))
                {
 
                    //Set ID at current location to quit string
                    idList[location] = QUIT_STR;
                }
 
            }//END IF
 
        }//END WHILE
 
        //Check all IDs are unique {uniqueID method}
        uniqueID(idList, MIN_ID_NUM, MAX_ID_NUM, QUIT_STR, generator);
 
        //Clear screen
        myLib.clrscr();
 
        //Display output title
        System.out.println("\n");
        System.out.println(Console.center("Login Info"));
        System.out.println();
 
        //Display all login info {display logins method}
        displayLogins(idList, pwdList, QUIT_STR);
 
    }//end method main
 
 
    //Input methods
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Prompt Name
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method will prompt for the student's first
     *     name. If it's not the quit string, it'll prompt for the last name and
     *     return an array of the full name. Otherwise, it'll return a
     *     one-element array with quit string.
     *
     * BEGIN Prompt Name
     *    Prompt first name
     *    IF (first name isn't quit strig) THEN
     *        Instantiate two-element array
     *        Assign first name to element 0
     *        Prompt last name to element 1
     *        Return array of first and last names
     *    ELSE
     *        Return one-element array with quit string
     *    END IF
     * END Prompt Names
     **********************************************************************************************/
    private static String[] promptName(String quitString)
    {
        //Local constants
        final int FIRST_NAME   = 0;     //Location of first name in names array
        final int LAST_NAME    = 1;     //Location of last name in names array
 
        //Local variables
        String[] names;                 //Student's first and last name, or quit string
        String input;                   //User's initial input to check against quit string
 
        /*******************************  Start Method Prompt Name  *******************************/
 
        //Prompt first name
        System.out.print("\t\tEnter first name (\"" + quitString + "\" to quit):  ");
        input = Keyboard.readString();
 
        //IF (first name isn't quit strig) THEN
        if (!input.equalsIgnoreCase(quitString))
        {
 
            //Instantiate two-element array
            names = new String[2];
 
            //Assign first name to element 0
            names[FIRST_NAME] = input;
 
            //Prompt last name to element 1
            System.out.print("\t\tEnter last name:                    ");
            names[LAST_NAME]  = Keyboard.readString();
        }
        else
        {
 
            //Return one-element array with quit string
            names    = new String[1];
            names[0] = quitString;
 
        }//END IF
 
 
        return names;
 
    }//end method Prompt Name
 
 
    //Calculation methods
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Random Number
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method will generate a random number between given min and max.
     *     If the maximum is less than the minimum, the minimum will be returned.
     *
     * BEGIN Random Number
     *    IF (min >= max) THEN
     *        Set number to min
     *    ELSE
     *        Generate a random number between min and max and set it to number
     *    END IF
     *    Return number
     * END Random number
     **********************************************************************************************/
    private static int randomNumber(int min, int max, Random generator)
    {
        //Local constants
 
        //Local variables
        int number;         //Random number to be returned
 
        /******************************  Start Method Random Number  ******************************/
 
        //IF (min >= max) THEN
        if (min >= max)
        {
 
            //Set number to min
            number = min;
        }
        else
        {
 
            //Generate a random number between min and max and set it to number
            number = generator.nextInt(max - min + 1) + min;
        }
 
        return number;
 
    }//end method Random Number
 
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Generate ID
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method will generate an ID using the student's first name,
     *     last name and a generated number.
     *
     * BEGIN Generate ID
     *    Init ID to an empty string
     *    IF (first name is shorter than max chars) THEN
     *        Append to ID full first name
     *    ELSE
     *        Append to ID first two letters of first name
     *    END IF
     *    IF (last name is shorter than max chars) THEN
     *        Append to ID full last name
     *    ELSE
     *        Append to ID first three letters of last name
     *    END IF
     *    Generate and append a random digit between min and max
     *    Return ID
     * END Generate ID
     **********************************************************************************************/
    private static String generateID(String firstName, String lastName,
                                        int min, int max, Random generator)
    {
        //Local constants
        int FIRST_NAME_CHARS = 2;   //Number of letters used from first name
        int LAST_NAME_CHARS  = 3;   //Number of letters used from last name
 
        //Local variables
        String id;          //Student's ID
 
        /*******************************  Start Method Generate ID  *******************************/
 
        //Init ID to an empty string
        id = "";
 
        //IF (first name is shorter than max chars) THEN
        if (firstName.length() <= FIRST_NAME_CHARS)
        {
 
            //Append to ID full first name
            id += firstName.toLowerCase();
        }
        else
        {
 
            //Append to ID first two letters of first name
            id += firstName.toLowerCase().substring(0, FIRST_NAME_CHARS);
        }
 
        //IF (last name is shorter than max chars) THEN
        if (lastName.length() <= LAST_NAME_CHARS)
        {
 
            //Append to ID full last name
            id += lastName.toLowerCase();
        }
        else
        {
 
            //Append to ID first three letters of last name
            id += lastName.toLowerCase().substring(0, LAST_NAME_CHARS);
        }
 
        //Generate and append a random digit between min and max
        id += randomNumber(min, max, generator);
 
 
        return id;
 
    }//end method Generate ID
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Generate Password
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method generates a password by converting the
     *     student's first name to lower case.
     *
     * BEGIN Generate Pwd
     *    Return first name in lower case
     * END Generate Pwd
     **********************************************************************************************/
    private static String generatePwd(String firstName)
    {
        //Local constants
 
        //Local variables
 
        /****************************  Start Method Generate Password  ****************************/
 
        //Return first name in lower case
        return firstName.toLowerCase();
 
    }//end method Generate Pwd
 
 
    //Data Management methods
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Unique ID
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method checks through each element of the ID
     *     list, comparing the current ID with the remaining IDs. If there is
     *     a duplicate, the compared ID gets changed by appending another
     *     random digit.
     *
     * BEGIN Unique ID
     *    IF (ID list is empty) THEN
     *        Display message that there are no logins entered
     *    ELSE
     *        FOR (each element left in ID list)
     *            FOR (each element left in ID list)
     *                IF (ID matches element) THEN
     *                    Append a random number in range to ID at current element
     *                END IF
     *            END FOR
     *        END FOR
     *    END IF
     * END Unique ID
     **********************************************************************************************/
    private static void uniqueID(String[] idList, int min, int max, String quitString,
                                    Random generator)
    {
        //Local constants
 
        //Local variables
 
        /********************************  Start Method Unique ID  ********************************/
 
        //IF (ID list is empty) THEN
        if (idList[0] == null)
        {
 
            //Display message that there are no logins entered
            System.out.println(Console.center("There are no logins entered."));
        }
        else
        {
 
            //FOR (each element left in ID list)
            for (int id = 0; id < idList.length - 1 && !idList[id].equals(quitString); id++)
            {
 
                //FOR (each element left in ID list)
                for (int comparedID = id + 1; comparedID < idList.length; comparedID++)
                {
 
                    //IF (ID matches element) THEN
                    if (idList[id].equals(idList[comparedID]))
                    {
                        //Append a random number in range to ID at current element
                        idList[comparedID] = idList[comparedID] + randomNumber(min, max, generator);
 
                    }//END IF
                }//END FOR EACH REMAINING ID
            }//END FOR EACH ID
        }//END IF (ID list is empty)
 
    }//end method Unique ID
 
 
    //Output methods
 
    /**********************************************************************************************
     * Method Name    : LoginInfo - Display Logins
     * Author         : Terry Weiss
     * Date           : January 29, 2016
     * Course/Section : CSC264 - 001
     * Program Description: This method displays each user ID and password
     *
     * BEGIN Display Logins
     *    IF (ID list is empty) THEN
     *        Display message that there aren't any logins
     *    ELSE
     *        Init login to 0
     *        Init pad to 0
     *        FOR (each ID)
     *            Set length to ID's length + prompt length
     *            IF (length > pad) THEN
     *                Set pad to length
     *            END IF
     *        END FOR
     *        FOR (each password)
     *            Set length to password's length + prompt length
     *            IF (length > pad) THEN
     *                Set pad to length
     *            END IF
     *        END FOR
     *        Calculate correct pad
     *        WHILE (login's ID in list isn't quit value and ID is less than ID list's length)
     *            Display ID at login
     *            Display password at login
     *            Increment login
     *        END WHILE
     *    END IF
     * END Display Logins
     **********************************************************************************************/
    private static void displayLogins(String[] idList, String[] pwdList, String quitString)
    {
        //Local constants
        final int PROMPT_LENGTH   = 21;     //Number of characters for prompt
        final int OUTPUT_COLUMNS  = 80;     //Number of columns in output window
 
        //Local variables
        int login;                          //Student's login's location
        int pad;                            //Left padding to center output
        int length;                         //Character length of longest output
        int ids;                            //Number of IDs that have been entered
 
        /*****************************  Start Method Display Logins  ******************************/
 
        //IF (ID list is empty) THEN
        if (idList[0] == null)
        {
            System.out.println(Console.center("There are no logins entered."));
        }
        else
        {
 
            //Init login to 0
            login = 0;
 
            //Init pad to 0
            pad   = 0;
 
 
            /*
             * The first for loop finds the longest ID length and also counts the number of IDs
             * that were entered so that the password loop stops at the same spot. This way
             * it's not necessary for the password list to have an entered quit string, and there is
             * no chance of a null pointer exception.
             */
 
            //FOR (each ID)
            for (ids = 0; ids < idList.length - 1 && !idList[ids].equals(quitString); ids++)
            {
 
                //Set length to ID's length + prompt length
                length = idList[ids].length() + PROMPT_LENGTH;
 
                //IF (length > pad) THEN
                if (length > pad)
                {
 
                    //Set pad to length
                    pad = length;
                }
            }
 
            //FOR (each password)
            for (int pwd = 0; pwd < pwdList.length - 1 && pwd < ids; pwd++)
            {
 
                //Set length to password's length + prompt length
                length = pwdList[pwd].length() + PROMPT_LENGTH;
 
                //IF (length > pad) THEN
                if (length > pad)
                {
 
                    //Set pad to length
                    pad = length;
                }
            }
            
            //Calculate correct pad
            pad = (OUTPUT_COLUMNS - pad) / 2;
 
 
            //WHILE (login's ID in list isn't quit value and ID is less than ID list's length)
            while (login < idList.length && !idList[login].equals(quitString))
            {
 
                //Display ID
                System.out.println(Console.padLeft(pad, "Student's ID:        " + idList[login]));
 
                //Display password
                System.out.println(Console.padLeft(pad, "Student's password:  " + pwdList[login]));
 
                //Display blank line
                System.out.println();
 
                //Increment login
                login++;
            }
 
        }//END IF (empty login list)
    }//end method Display Logins
 
 
}//end class LoginInfoTTW

Comments