Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. Note:
  2. Please find the below LoginApplication class and coresponding output for three sample runs
  3. Please revert back in case of any doubts or anything needs to be changed
  4. Program:
  5.  
  6. package com.chegg;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13. import java.util.Scanner;
  14. public class LoginApplication {
  15. // Global String variable to store input file name
  16. public static final String INPUT_FILE_NAME = "userinfo.txt";
  17. // Global HashMap to store user name and password
  18. public static HashMap<String, String> userNamePasswordMap = new HashMap<String, String>();
  19. // Global HashMap to store user name and full name
  20. public static HashMap<String, String> userNameFullnameMap = new HashMap<String, String>();
  21. public static void main(String[] args) {
  22. //This method will read input text file and create user name & password map
  23. //Also create user name & full name map
  24. readInputFile ();
  25. //Scanner input to read user input data
  26. Scanner input = new Scanner(System.in);
  27. //Variable to store attempt count
  28. int attemptCount = 1;
  29. //Iterating three times for user name and password
  30. while (attemptCount<=3) {
  31. System.out.print("Login: ");
  32. String userName = input.nextLine();//Getting user name
  33. System.out.print("Password: ");
  34. String password = input.nextLine();//Getting password
  35. //Checking weather user name exits and coresponding password is matching
  36. if (userNamePasswordMap!= null & userNamePasswordMap.get(userName) != null
  37. && userNamePasswordMap.get(userName).equals(password)) {
  38. //if matching then getting full name based on user name
  39. String fullName = userNameFullnameMap.get(userName);
  40. System.out.println("Welcome "+fullName);//Displaying welcome message
  41. break;//breaking the loop
  42. } else if (attemptCount <3){
  43. //Displaying message for wrong user name or password
  44. System.out.println("Either the username or password is incorrect. You have "+(3-attemptCount)+" more attempt.");
  45. }
  46. attemptCount ++;//increasing attempt count for wrong user name or password
  47. }
  48. //If 3 attempts over, displaying message
  49. if (attemptCount >3) {
  50. System.out.println("Sorry. Incorrect login. Please contact the system administrator.");
  51. }
  52. input.close();//Clossing input scanner
  53. }
  54. /**
  55. * This method will read line by line of input text file to get each phone
  56. * number. Then checking each phone number weather valid or in-valid. After
  57. * that translated valid phone number to its numeric equivalent
  58. */
  59. public static void readInputFile() {
  60. BufferedReader inputReader = null;
  61. try {
  62. // create file object by file name
  63. File file = new File(INPUT_FILE_NAME);
  64. // creating input reader to read text file
  65. inputReader = new BufferedReader(new FileReader(file));
  66. String line = "";
  67. // Iterating each line of input text file to get each user info
  68. while ((line = inputReader.readLine()) != null) {
  69. String[] lineArray = line.split(" ");//creating string array using space seperator
  70. String userName = lineArray[lineArray.length-2];//getting user name from array
  71. String password = lineArray[lineArray.length-1];//getting password from array
  72. String fullName = "";
  73. userNamePasswordMap.put(userName, password);//adding user name & password in map
  74. //getting full name
  75. for (int i=0;i<lineArray.length-2;i++) {
  76. fullName = fullName + lineArray[i]+" ";
  77. }
  78. userNameFullnameMap.put(userName, fullName.trim());//adding user name & fullname in map
  79. }
  80. } catch (FileNotFoundException foe) {
  81. foe.printStackTrace();
  82. } catch (IOException ioe) {
  83. ioe.printStackTrace();
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. } finally {
  87. try {
  88. if (inputReader != null) {
  89. inputReader.close();
  90. }
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. }
  97.  
  98. Output:
  99. Sample Run 1
  100.  
  101. Sample Run 2
  102.  
  103. Sample Run 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement