Advertisement
Guest User

Untitled

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