Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AccessControl {
  4.  
  5. //Defining public variables used throughout the program
  6. public static Scanner console;
  7. public static String username = "";
  8. public static String password = "";
  9.  
  10. public static void main(String[] args) {
  11.  
  12. console = new Scanner(System.in);
  13. //Running all the methods in the program
  14. userCreationUsername();
  15. userCreationPassword();
  16. System.out.println("*** User " + username + " is registered.");
  17. login();
  18. mainMenu();
  19. }
  20.  
  21. //Method for creating and verifying username
  22. public static void userCreationUsername(){
  23. while ( true ) {
  24. System.out.println("Please enter a non-empty username:");
  25. username = console.nextLine();
  26. if ( checkUserName (username)) {
  27. break;
  28. }
  29. }
  30. }
  31.  
  32. //Method for creating and verifying password
  33. public static void userCreationPassword() {
  34.  
  35. while ( true ) {
  36. System.out.println("Please enter a password :");
  37. password = console.nextLine();
  38. if ( checkPWD(password) ) {
  39. System.out.println("Please repeat the password : ");
  40. String password2 = console.nextLine();
  41.  
  42. if ( password.equals(password2) ) {
  43. break;
  44. }
  45. }
  46. }
  47. }
  48.  
  49. /*Method for login section. If the username input is the same as the previously saved username, and the password is the same as the previously saved
  50. password, access is granted to the main menu. If an invalid password is entered three times, the program shuts down.*/
  51. public static void login() {
  52. System.out.println("To logon enter username:");
  53. System.out.println("To logon enter password:");
  54. int tries = 0;
  55.  
  56. while ( true ) {
  57. String usernameTemp = console.nextLine();
  58. if ( usernameTemp.equals(username) ) {
  59. break;
  60. } else {
  61. System.out.println("To logon enter username:");
  62. }
  63. }
  64.  
  65. while ( true ) {
  66. String passwordTemp = console.nextLine();
  67.  
  68. if (passwordTemp.equals(password) ) {
  69. System.out.println("*** User " + username + " is Logged on.");
  70. break;
  71. }
  72. tries++;
  73.  
  74. if ( tries == 3) {
  75. System.exit(0);
  76. } else {
  77. System.out.println("To logon enter password:");
  78. }
  79. }
  80. }
  81.  
  82. /*Main menu - 1 for log off, 2 for change password and 3 for shut down. If option 1 is chosen, the client is returned to the log-in method. If
  83. option 2 is chosen, the client is returned to password creation section. If option 3 is chosen, the program terminates.*/
  84. public static void mainMenu() {
  85. while ( true ) {
  86. System.out.println("You now have the following choices:");
  87. System.out.println(" 1 - Change Password");
  88. System.out.println(" 2 - Log off");
  89. System.out.println(" 3 - Shut down");
  90. System.out.println("Please select:");
  91.  
  92. //This makes sure that only integers are accepted as user input
  93. int select = 0;
  94. while ( true ) {
  95. if ( console.hasNextInt() ) {
  96. select = console.nextInt();
  97. /* We encountered a problem in which the console.nextInt() does not consume a line. A work-around for this is
  98. to make an empty user input to consume the last new line. */
  99. console.nextLine();
  100. break;
  101. } else {
  102. console.next();
  103. }
  104. }
  105. //The integer inputs 1, 2 and 3 yield the outputs:
  106. if ( select == 1 ) {
  107. userCreationPassword();
  108. System.out.println("*** Password changed for user " + username);
  109. } else if ( select == 2 ) {
  110. System.out.println("*** User " + username + " is logged off");
  111. login();
  112. } else if ( select == 3 ) {
  113. System.out.println("*** User " + username + " is logged off");
  114. System.out.println("*** System shutting down.");
  115. System.exit(0);
  116. }
  117. }
  118. }
  119.  
  120. public static boolean checkPWD(String password) {
  121. int digitCount = 0;
  122. int letterCount = 0;
  123. int alphaCount = 0;
  124.  
  125. for ( int i = 0; i < password.length(); i++) {
  126. if( Character.isDigit(password.charAt(i)) ) {
  127. digitCount++;
  128. alphaCount++;
  129. }
  130. if ( Character.isLetter(password.charAt(i)) ) {
  131. letterCount++;
  132. alphaCount++;
  133. }
  134. }
  135.  
  136. if ( digitCount >= 2 && letterCount >= 2 && alphaCount == password.length() ) {
  137. if( password.length() < 8 ) {
  138. return false;
  139. } else if( password.equals(password.toLowerCase()) ) {
  140. return false;
  141. } else if( password.equals(password.toUpperCase()) ) {
  142. return false;
  143. }
  144. return true;
  145.  
  146. } else {
  147. return false;
  148. }
  149. }
  150.  
  151.  
  152. //Username checker
  153. public static boolean checkUserName(String username) {
  154. if ( Character.isLetter(username.charAt(0)) && username.contains(" ") == false ) {
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement