brainfrz

LoginInfo

Feb 1st, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.25 KB | None | 0 0
  1. /**************************************************************************************************
  2.  * Program Name   : Week 2 - Login Info
  3.  * Author         : Terry Weiss
  4.  * Date           : January 29, 2016
  5.  * Course/Section : CSC264 - 001
  6.  * Program Description: This program will create usernames and passwords for
  7.  *     students at OCC using their first and last name. The username will be
  8.  *     the student's first two letters of their first name, followed by the
  9.  *     first three letters of their last name, followed by a random digit
  10.  *     1 and 3. If either name is too short their full first/last name is used.
  11.  *     All usernames are in all lowercase letters. The password will default to
  12.  *     the user's first name (in all lower case letters). The usernames and
  13.  *     passwords each in their own array. The user may create up to 10
  14.  *     username/password combinations. Duplicate usernames are prevented by
  15.  *     adding another digit (1, 2, or 3) onto the end of the duplicate username,
  16.  *     thus creating a unique username. When the user stops creating usernames,
  17.  *     all of the usernames and passwords are displayed on the screen.
  18.  *
  19.  * Methods:
  20.  * -------
  21.  * Main           - Reads the names, creates the login info and displays them
  22.  * Prompt Name    - Prompts for a first and last name or quit value
  23.  * Random Number  - Generates a random number within range (1 through 3 inclusive)
  24.  * Generate ID    - Generates the ID using first and last name
  25.  * Generate Pwd   - Generates the passwords using their first name in lower case
  26.  * Unique ID      - Goes through ID list confirming each ID is unique or replacing
  27.  * Display Logins - Displays a list of all the IDs and passwords
  28.  **************************************************************************************************/
  29.  
  30. import java.util.Random;
  31.  
  32. class LoginInfoTTW
  33. {
  34.  
  35.     /**********************************************************************************************
  36.      * Method Name    : LoginInfo - Main
  37.      * Author         : Terry Weiss
  38.      * Date           : January 29, 2016
  39.      * Course/Section : CSC264 - 001
  40.      * Program Description: This method will read a name entered by the user. It
  41.      *     then creates a new String with the name centered for console output,
  42.      *     and displays it.
  43.      *
  44.      * BEGIN main
  45.      *    Init location to 0
  46.      *    Clear screen
  47.      *    Display input title
  48.      *    Prompt name {prompt name method, returning name array}
  49.      *    WHILE (name is array of two elements)
  50.      *        Generate and assign ID to element in ID list {generate ID method}
  51.      *        Generate and assign password to element in Pwd list {generate pwd method}
  52.      *        Increment location
  53.      *        IF (array is full)
  54.      *            Set name to array of one element
  55.      *        ELSE
  56.      *            Display blank line
  57.      *            Prompt name {prompt name method}
  58.      *            IF (first name is quit string) THEN
  59.      *                Set ID at current location to quit string
  60.      *            END IF
  61.      *        END IF
  62.      *    END WHILE
  63.      *    Check all IDs are unique {uniqueID method}
  64.      *    Clear screen
  65.      *    Display output title
  66.      *    Display all login info {display logins method}
  67.      * END Read Name
  68.      **********************************************************************************************/
  69.     public static void main(String[] args)
  70.     {
  71.         //Local constants
  72.         final int MAX_LOGINS   = 10;                //Maximum number of logins
  73.         final String QUIT_STR  = "done";            //Sentinel value for ending input
  74.         final int FIRST_NAME   = 0;                 //Location of first name in names array
  75.         final int LAST_NAME    = 1;                 //Location of last name in names array
  76.         final int MIN_ID_NUM   = 1;                 //Minimum legal number in ID
  77.         final int MAX_ID_NUM   = 3;                 //Maximum legal number in ID
  78.  
  79.         //Local variables
  80.         int location;                               //Location of ID/Password in their arrays
  81.         String[] idList  = new String[MAX_LOGINS];  //List of IDs
  82.         String[] pwdList = new String[MAX_LOGINS];  //List of passwords
  83.         String[] names;                             //First and last name of student
  84.  
  85.         //Utility class objects
  86.         final Library64 myLib  = new Library64();   //Console control library
  87.         final Random generator = new Random();      //Random number generator
  88.  
  89.         /*************************************START MAIN METHOD************************************/
  90.  
  91.         //Init location to 0
  92.         location = 0;
  93.  
  94.         //Clear screen
  95.         myLib.clrscr();
  96.  
  97.         //Display input title
  98.         System.out.println("\n");
  99.         System.out.println(Console.center("Name Entry"));
  100.         System.out.println();
  101.  
  102.         //Prompt name {prompt name method, returning name array}
  103.         names = promptName(QUIT_STR);
  104.  
  105.         //WHILE (name is array of two elements)
  106.         while (names.length == 2)
  107.         {
  108.  
  109.             //Generate and assign ID to element in ID list {generate ID method}
  110.             idList[location]  = generateID(names[FIRST_NAME], names[LAST_NAME], MIN_ID_NUM,
  111.                                              MAX_ID_NUM, generator);
  112.  
  113.             //Generate and assign password to element in Pwd list {generate pwd method}
  114.             pwdList[location] = generatePwd(names[FIRST_NAME]);
  115.  
  116.             //Increment location
  117.             location++;
  118.  
  119.             //IF (array is full)
  120.             if (location == MAX_LOGINS)
  121.             {
  122.  
  123.                 //Set name to array of one element
  124.                 names    = new String[1];
  125.                 names[0] = QUIT_STR;
  126.             }
  127.             else
  128.             {
  129.  
  130.                 //Display blank line
  131.                 System.out.println();
  132.  
  133.                 //Prompt name {prompt name method}
  134.                 names = promptName(QUIT_STR);
  135.  
  136.                 //IF (first name is quit string) THEN
  137.                 if(names[0].equals(QUIT_STR))
  138.                 {
  139.  
  140.                     //Set ID at current location to quit string
  141.                     idList[location] = QUIT_STR;
  142.                 }
  143.  
  144.             }//END IF
  145.  
  146.         }//END WHILE
  147.  
  148.         //Check all IDs are unique {uniqueID method}
  149.         uniqueID(idList, MIN_ID_NUM, MAX_ID_NUM, QUIT_STR, generator);
  150.  
  151.         //Clear screen
  152.         myLib.clrscr();
  153.  
  154.         //Display output title
  155.         System.out.println("\n");
  156.         System.out.println(Console.center("Login Info"));
  157.         System.out.println();
  158.  
  159.         //Display all login info {display logins method}
  160.         displayLogins(idList, pwdList, QUIT_STR);
  161.  
  162.     }//end method main
  163.  
  164.  
  165.     //Input methods
  166.  
  167.     /**********************************************************************************************
  168.      * Method Name    : LoginInfo - Prompt Name
  169.      * Author         : Terry Weiss
  170.      * Date           : January 29, 2016
  171.      * Course/Section : CSC264 - 001
  172.      * Program Description: This method will prompt for the student's first
  173.      *     name. If it's not the quit string, it'll prompt for the last name and
  174.      *     return an array of the full name. Otherwise, it'll return a
  175.      *     one-element array with quit string.
  176.      *
  177.      * BEGIN Prompt Name
  178.      *    Prompt first name
  179.      *    IF (first name isn't quit strig) THEN
  180.      *        Instantiate two-element array
  181.      *        Assign first name to element 0
  182.      *        Prompt last name to element 1
  183.      *        Return array of first and last names
  184.      *    ELSE
  185.      *        Return one-element array with quit string
  186.      *    END IF
  187.      * END Prompt Names
  188.      **********************************************************************************************/
  189.     private static String[] promptName(String quitString)
  190.     {
  191.         //Local constants
  192.         final int FIRST_NAME   = 0;     //Location of first name in names array
  193.         final int LAST_NAME    = 1;     //Location of last name in names array
  194.  
  195.         //Local variables
  196.         String[] names;                 //Student's first and last name, or quit string
  197.         String input;                   //User's initial input to check against quit string
  198.  
  199.         /*******************************  Start Method Prompt Name  *******************************/
  200.  
  201.         //Prompt first name
  202.         System.out.print("\t\tEnter first name (\"" + quitString + "\" to quit):  ");
  203.         input = Keyboard.readString();
  204.  
  205.         //IF (first name isn't quit strig) THEN
  206.         if (!input.equalsIgnoreCase(quitString))
  207.         {
  208.  
  209.             //Instantiate two-element array
  210.             names = new String[2];
  211.  
  212.             //Assign first name to element 0
  213.             names[FIRST_NAME] = input;
  214.  
  215.             //Prompt last name to element 1
  216.             System.out.print("\t\tEnter last name:                    ");
  217.             names[LAST_NAME]  = Keyboard.readString();
  218.         }
  219.         else
  220.         {
  221.  
  222.             //Return one-element array with quit string
  223.             names    = new String[1];
  224.             names[0] = quitString;
  225.  
  226.         }//END IF
  227.  
  228.  
  229.         return names;
  230.  
  231.     }//end method Prompt Name
  232.  
  233.  
  234.     //Calculation methods
  235.  
  236.     /**********************************************************************************************
  237.      * Method Name    : LoginInfo - Random Number
  238.      * Author         : Terry Weiss
  239.      * Date           : January 29, 2016
  240.      * Course/Section : CSC264 - 001
  241.      * Program Description: This method will generate a random number between given min and max.
  242.      *     If the maximum is less than the minimum, the minimum will be returned.
  243.      *
  244.      * BEGIN Random Number
  245.      *    IF (min >= max) THEN
  246.      *        Set number to min
  247.      *    ELSE
  248.      *        Generate a random number between min and max and set it to number
  249.      *    END IF
  250.      *    Return number
  251.      * END Random number
  252.      **********************************************************************************************/
  253.     private static int randomNumber(int min, int max, Random generator)
  254.     {
  255.         //Local constants
  256.  
  257.         //Local variables
  258.         int number;         //Random number to be returned
  259.  
  260.         /******************************  Start Method Random Number  ******************************/
  261.  
  262.         //IF (min >= max) THEN
  263.         if (min >= max)
  264.         {
  265.  
  266.             //Set number to min
  267.             number = min;
  268.         }
  269.         else
  270.         {
  271.  
  272.             //Generate a random number between min and max and set it to number
  273.             number = generator.nextInt(max - min + 1) + min;
  274.         }
  275.  
  276.         return number;
  277.  
  278.     }//end method Random Number
  279.  
  280.  
  281.     /**********************************************************************************************
  282.      * Method Name    : LoginInfo - Generate ID
  283.      * Author         : Terry Weiss
  284.      * Date           : January 29, 2016
  285.      * Course/Section : CSC264 - 001
  286.      * Program Description: This method will generate an ID using the student's first name,
  287.      *     last name and a generated number.
  288.      *
  289.      * BEGIN Generate ID
  290.      *    Init ID to an empty string
  291.      *    IF (first name is shorter than max chars) THEN
  292.      *        Append to ID full first name
  293.      *    ELSE
  294.      *        Append to ID first two letters of first name
  295.      *    END IF
  296.      *    IF (last name is shorter than max chars) THEN
  297.      *        Append to ID full last name
  298.      *    ELSE
  299.      *        Append to ID first three letters of last name
  300.      *    END IF
  301.      *    Generate and append a random digit between min and max
  302.      *    Return ID
  303.      * END Generate ID
  304.      **********************************************************************************************/
  305.     private static String generateID(String firstName, String lastName,
  306.                                         int min, int max, Random generator)
  307.     {
  308.         //Local constants
  309.         int FIRST_NAME_CHARS = 2;   //Number of letters used from first name
  310.         int LAST_NAME_CHARS  = 3;   //Number of letters used from last name
  311.  
  312.         //Local variables
  313.         String id;          //Student's ID
  314.  
  315.         /*******************************  Start Method Generate ID  *******************************/
  316.  
  317.         //Init ID to an empty string
  318.         id = "";
  319.  
  320.         //IF (first name is shorter than max chars) THEN
  321.         if (firstName.length() <= FIRST_NAME_CHARS)
  322.         {
  323.  
  324.             //Append to ID full first name
  325.             id += firstName.toLowerCase();
  326.         }
  327.         else
  328.         {
  329.  
  330.             //Append to ID first two letters of first name
  331.             id += firstName.toLowerCase().substring(0, FIRST_NAME_CHARS);
  332.         }
  333.  
  334.         //IF (last name is shorter than max chars) THEN
  335.         if (lastName.length() <= LAST_NAME_CHARS)
  336.         {
  337.  
  338.             //Append to ID full last name
  339.             id += lastName.toLowerCase();
  340.         }
  341.         else
  342.         {
  343.  
  344.             //Append to ID first three letters of last name
  345.             id += lastName.toLowerCase().substring(0, LAST_NAME_CHARS);
  346.         }
  347.  
  348.         //Generate and append a random digit between min and max
  349.         id += randomNumber(min, max, generator);
  350.  
  351.  
  352.         return id;
  353.  
  354.     }//end method Generate ID
  355.  
  356.     /**********************************************************************************************
  357.      * Method Name    : LoginInfo - Generate Password
  358.      * Author         : Terry Weiss
  359.      * Date           : January 29, 2016
  360.      * Course/Section : CSC264 - 001
  361.      * Program Description: This method generates a password by converting the
  362.      *     student's first name to lower case.
  363.      *
  364.      * BEGIN Generate Pwd
  365.      *    Return first name in lower case
  366.      * END Generate Pwd
  367.      **********************************************************************************************/
  368.     private static String generatePwd(String firstName)
  369.     {
  370.         //Local constants
  371.  
  372.         //Local variables
  373.  
  374.         /****************************  Start Method Generate Password  ****************************/
  375.  
  376.         //Return first name in lower case
  377.         return firstName.toLowerCase();
  378.  
  379.     }//end method Generate Pwd
  380.  
  381.  
  382.     //Data Management methods
  383.  
  384.     /**********************************************************************************************
  385.      * Method Name    : LoginInfo - Unique ID
  386.      * Author         : Terry Weiss
  387.      * Date           : January 29, 2016
  388.      * Course/Section : CSC264 - 001
  389.      * Program Description: This method checks through each element of the ID
  390.      *     list, comparing the current ID with the remaining IDs. If there is
  391.      *     a duplicate, the compared ID gets changed by appending another
  392.      *     random digit.
  393.      *
  394.      * BEGIN Unique ID
  395.      *    IF (ID list is empty) THEN
  396.      *        Display message that there are no logins entered
  397.      *    ELSE
  398.      *        FOR (each element left in ID list)
  399.      *            FOR (each element left in ID list)
  400.      *                IF (ID matches element) THEN
  401.      *                    Append a random number in range to ID at current element
  402.      *                END IF
  403.      *            END FOR
  404.      *        END FOR
  405.      *    END IF
  406.      * END Unique ID
  407.      **********************************************************************************************/
  408.     private static void uniqueID(String[] idList, int min, int max, String quitString,
  409.                                     Random generator)
  410.     {
  411.         //Local constants
  412.  
  413.         //Local variables
  414.  
  415.         /********************************  Start Method Unique ID  ********************************/
  416.  
  417.         //IF (ID list is empty) THEN
  418.         if (idList[0] == null)
  419.         {
  420.  
  421.             //Display message that there are no logins entered
  422.             System.out.println(Console.center("There are no logins entered."));
  423.         }
  424.         else
  425.         {
  426.  
  427.             //FOR (each element left in ID list)
  428.             for (int id = 0; id < idList.length - 1 && !idList[id].equals(quitString); id++)
  429.             {
  430.  
  431.                 //FOR (each element left in ID list)
  432.                 for (int comparedID = id + 1; comparedID < idList.length; comparedID++)
  433.                 {
  434.  
  435.                     //IF (ID matches element) THEN
  436.                     if (idList[id].equals(idList[comparedID]))
  437.                     {
  438.                         //Append a random number in range to ID at current element
  439.                         idList[comparedID] = idList[comparedID] + randomNumber(min, max, generator);
  440.  
  441.                     }//END IF
  442.                 }//END FOR EACH REMAINING ID
  443.             }//END FOR EACH ID
  444.         }//END IF (ID list is empty)
  445.  
  446.     }//end method Unique ID
  447.  
  448.  
  449.     //Output methods
  450.  
  451.     /**********************************************************************************************
  452.      * Method Name    : LoginInfo - Display Logins
  453.      * Author         : Terry Weiss
  454.      * Date           : January 29, 2016
  455.      * Course/Section : CSC264 - 001
  456.      * Program Description: This method displays each user ID and password
  457.      *
  458.      * BEGIN Display Logins
  459.      *    IF (ID list is empty) THEN
  460.      *        Display message that there aren't any logins
  461.      *    ELSE
  462.      *        Init login to 0
  463.      *        Init pad to 0
  464.      *        FOR (each ID)
  465.      *            Set length to ID's length + prompt length
  466.      *            IF (length > pad) THEN
  467.      *                Set pad to length
  468.      *            END IF
  469.      *        END FOR
  470.      *        FOR (each password)
  471.      *            Set length to password's length + prompt length
  472.      *            IF (length > pad) THEN
  473.      *                Set pad to length
  474.      *            END IF
  475.      *        END FOR
  476.      *        Calculate correct pad
  477.      *        WHILE (login's ID in list isn't quit value and ID is less than ID list's length)
  478.      *            Display ID at login
  479.      *            Display password at login
  480.      *            Increment login
  481.      *        END WHILE
  482.      *    END IF
  483.      * END Display Logins
  484.      **********************************************************************************************/
  485.     private static void displayLogins(String[] idList, String[] pwdList, String quitString)
  486.     {
  487.         //Local constants
  488.         final int PROMPT_LENGTH   = 21;     //Number of characters for prompt
  489.         final int OUTPUT_COLUMNS  = 80;     //Number of columns in output window
  490.  
  491.         //Local variables
  492.         int login;                          //Student's login's location
  493.         int pad;                            //Left padding to center output
  494.         int length;                         //Character length of longest output
  495.         int ids;                            //Number of IDs that have been entered
  496.  
  497.         /*****************************  Start Method Display Logins  ******************************/
  498.  
  499.         //IF (ID list is empty) THEN
  500.         if (idList[0] == null)
  501.         {
  502.             System.out.println(Console.center("There are no logins entered."));
  503.         }
  504.         else
  505.         {
  506.  
  507.             //Init login to 0
  508.             login = 0;
  509.  
  510.             //Init pad to 0
  511.             pad   = 0;
  512.  
  513.  
  514.             /*
  515.              * The first for loop finds the longest ID length and also counts the number of IDs
  516.              * that were entered so that the password loop stops at the same spot. This way
  517.              * it's not necessary for the password list to have an entered quit string, and there is
  518.              * no chance of a null pointer exception.
  519.              */
  520.  
  521.             //FOR (each ID)
  522.             for (ids = 0; ids < idList.length - 1 && !idList[ids].equals(quitString); ids++)
  523.             {
  524.  
  525.                 //Set length to ID's length + prompt length
  526.                 length = idList[ids].length() + PROMPT_LENGTH;
  527.  
  528.                 //IF (length > pad) THEN
  529.                 if (length > pad)
  530.                 {
  531.  
  532.                     //Set pad to length
  533.                     pad = length;
  534.                 }
  535.             }
  536.  
  537.             //FOR (each password)
  538.             for (int pwd = 0; pwd < pwdList.length - 1 && pwd < ids; pwd++)
  539.             {
  540.  
  541.                 //Set length to password's length + prompt length
  542.                 length = pwdList[pwd].length() + PROMPT_LENGTH;
  543.  
  544.                 //IF (length > pad) THEN
  545.                 if (length > pad)
  546.                 {
  547.  
  548.                     //Set pad to length
  549.                     pad = length;
  550.                 }
  551.             }
  552.            
  553.             //Calculate correct pad
  554.             pad = (OUTPUT_COLUMNS - pad) / 2;
  555.  
  556.  
  557.             //WHILE (login's ID in list isn't quit value and ID is less than ID list's length)
  558.             while (login < idList.length && !idList[login].equals(quitString))
  559.             {
  560.  
  561.                 //Display ID
  562.                 System.out.println(Console.padLeft(pad, "Student's ID:        " + idList[login]));
  563.  
  564.                 //Display password
  565.                 System.out.println(Console.padLeft(pad, "Student's password:  " + pwdList[login]));
  566.  
  567.                 //Display blank line
  568.                 System.out.println();
  569.  
  570.                 //Increment login
  571.                 login++;
  572.             }
  573.  
  574.         }//END IF (empty login list)
  575.     }//end method Display Logins
  576.  
  577.  
  578. }//end class LoginInfoTTW
Advertisement
Add Comment
Please, Sign In to add comment