Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package projekt1;
  2. import java.util.*;
  3.  
  4. public class AccessControlCode {
  5. public static Scanner scan = new Scanner(System.in);
  6. public static void main(String[] args) {
  7. String username = createUsername();
  8. String password = passwordChange();
  9. // Logon
  10. while (true) {
  11. logOn(username, password);
  12. while(true) {
  13. System.out.println("You now have the following choices:\r\n" +
  14. " 1 - Change Password\r\n" +
  15. " 2 - Log off\r\n" +
  16. " 3 - Shut down");
  17. System.out.println("Please select:");
  18. int choice = scan.nextInt();
  19. scan.nextLine();
  20. if (choice == 1) {
  21. password = passwordChange();
  22. System.out.println("*** Password changed for user " + username);
  23. }
  24. if (choice == 2) {
  25. System.out.println("*** User " + username + " is logged off");
  26. while(true) {
  27. System.out.println("To logon enter username:");
  28. String musername = scan.nextLine();
  29. if (username.equals(musername)) {
  30. break;
  31. }
  32. }
  33. int t = 3;
  34. while (true) {
  35. t--;
  36. System.out.println("To logon enter password:");
  37. String mpassword = scan.nextLine();
  38. if (password.equals(mpassword)) {
  39. System.out.println("*** User " + username + " is Logged on.");
  40. break;
  41. }
  42. if (t == 0 ) {
  43. System.exit(0);
  44. }
  45. }
  46. }
  47.  
  48. if (choice == 3) {
  49. System.out.println("*** User " + username + " is logged off");
  50. System.out.println("*** System shutting down.");
  51. System.exit(0);
  52. }
  53. }
  54. }
  55. }
  56. public static String createUsername() {
  57. while(true) {
  58. System.out.println("Please enter a non-empty username:");
  59. String username = scan.nextLine();
  60. if (Character.isLetter(username.charAt(0)) && !(username.contains(" ")) ) {
  61. return username;
  62. }
  63. }
  64. }
  65. // Change/Create password
  66. public static String passwordChange() {
  67. while(true) {
  68. System.out.println("Please enter a password :");
  69. String password = scan.nextLine();
  70. if (checkPWD(password)) {
  71. System.out.println("Please repeat the password :");
  72. String onePassword = scan.nextLine();
  73. if (checkPWD(onePassword) == true && onePassword.equals(password)) {
  74. return password;
  75. }
  76. }
  77. }
  78. }
  79. public static boolean checkPWD(String password) {
  80. int PWlen = password.length();
  81. boolean hasNonAlpha = password.matches("^.*[^a-zA-Z0-9 ].*");
  82. boolean hasTwoNumbers = password.matches("(.*\\d+.*){2}");
  83. boolean atleastTwoAlpha = password.matches("(.*[a-zA-Z]+.*){2}");
  84. boolean hasOneCapital = password.matches("^.*[A-Z].*");
  85. boolean hasOneLower = password.matches("^.*[a-z].*");
  86. if (PWlen >= 8 && hasNonAlpha == false && hasTwoNumbers == true
  87. && hasOneCapital == true && atleastTwoAlpha == true && hasOneLower == true) {
  88. return true;
  89. }
  90. else {
  91. return false;
  92. }
  93. }
  94. public static void logOn(String username, String password) {
  95. System.out.println("*** User " + username + " is registered.");
  96. while(true) {
  97. System.out.println("To logon enter username: ");
  98. String musername = scan.nextLine();
  99. if (username.equals(musername)) {
  100. break;
  101. }
  102. }
  103. while (true) {
  104. System.out.println("To logon enter password:");
  105. String mpassword = scan.nextLine();
  106. if (password.equals(mpassword)) {
  107. System.out.println("*** User " + username + " is Logged on.");
  108. break;
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement