Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projekt1;
- import java.util.*;
- public class AccessControlCode {
- public static Scanner scan = new Scanner(System.in);
- public static void main(String[] args) {
- String username = createUsername();
- String password = passwordChange();
- // Logon
- while (true) {
- logOn(username, password);
- while(true) {
- System.out.println("You now have the following choices:\r\n" +
- " 1 - Change Password\r\n" +
- " 2 - Log off\r\n" +
- " 3 - Shut down");
- System.out.println("Please select:");
- int choice = scan.nextInt();
- scan.nextLine();
- if (choice == 1) {
- password = passwordChange();
- System.out.println("*** Password changed for user " + username);
- }
- if (choice == 2) {
- System.out.println("*** User " + username + " is logged off");
- while(true) {
- System.out.println("To logon enter username:");
- String musername = scan.nextLine();
- if (username.equals(musername)) {
- break;
- }
- }
- int t = 3;
- while (true) {
- t--;
- System.out.println("To logon enter password:");
- String mpassword = scan.nextLine();
- if (password.equals(mpassword)) {
- System.out.println("*** User " + username + " is Logged on.");
- break;
- }
- if (t == 0 ) {
- System.exit(0);
- }
- }
- }
- if (choice == 3) {
- System.out.println("*** User " + username + " is logged off");
- System.out.println("*** System shutting down.");
- System.exit(0);
- }
- }
- }
- }
- public static String createUsername() {
- while(true) {
- System.out.println("Please enter a non-empty username:");
- String username = scan.nextLine();
- if (Character.isLetter(username.charAt(0)) && !(username.contains(" ")) ) {
- return username;
- }
- }
- }
- // Change/Create password
- public static String passwordChange() {
- while(true) {
- System.out.println("Please enter a password :");
- String password = scan.nextLine();
- if (checkPWD(password)) {
- System.out.println("Please repeat the password :");
- String onePassword = scan.nextLine();
- if (checkPWD(onePassword) == true && onePassword.equals(password)) {
- return password;
- }
- }
- }
- }
- public static boolean checkPWD(String password) {
- int PWlen = password.length();
- boolean hasNonAlpha = password.matches("^.*[^a-zA-Z0-9 ].*");
- boolean hasTwoNumbers = password.matches("(.*\\d+.*){2}");
- boolean atleastTwoAlpha = password.matches("(.*[a-zA-Z]+.*){2}");
- boolean hasOneCapital = password.matches("^.*[A-Z].*");
- boolean hasOneLower = password.matches("^.*[a-z].*");
- if (PWlen >= 8 && hasNonAlpha == false && hasTwoNumbers == true
- && hasOneCapital == true && atleastTwoAlpha == true && hasOneLower == true) {
- return true;
- }
- else {
- return false;
- }
- }
- public static void logOn(String username, String password) {
- System.out.println("*** User " + username + " is registered.");
- while(true) {
- System.out.println("To logon enter username: ");
- String musername = scan.nextLine();
- if (username.equals(musername)) {
- break;
- }
- }
- while (true) {
- System.out.println("To logon enter password:");
- String mpassword = scan.nextLine();
- if (password.equals(mpassword)) {
- System.out.println("*** User " + username + " is Logged on.");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement