Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class AccesscControl1 {
- public static Scanner console;
- public static String username = "";
- public static String password = "";
- public static void main(String[] args) {
- console = new Scanner(System.in);
- userCreationUsername();
- userCreationPassword();
- System.out.println("*** User " + username + " is registered.");
- login();
- mainMenu();
- }
- //Method for creating and verifying username
- public static void userCreationUsername(){
- while ( true ) {
- System.out.println("Please enter a non-empty username:");
- username = console.nextLine();
- if ( checkUserName (username)) {
- break;
- }
- }
- }
- //Method for creating and verifying password
- public static void userCreationPassword() {
- while ( true ) {
- System.out.println("Please enter a password :");
- password = console.nextLine();
- if ( checkPWD(password) ) {
- System.out.println("Please repeat the password : ");
- String password2 = console.nextLine();
- if ( password.equals(password2) ) {
- break;
- }
- }
- }
- }
- //Method for login section
- public static void login() {
- System.out.println("To logon enter username:");
- System.out.println("To logon enter password:");
- int tries = 0;
- while ( true ) {
- String usernameTemp = console.nextLine();
- if ( usernameTemp.equals(username) ) {
- break;
- } else {
- System.out.println("To logon enter username:");
- }
- }
- while ( true ) {
- String passwordTemp = console.nextLine();
- if (passwordTemp.equals(password) ) {
- System.out.println("*** User " + username + " is Logged on.");
- break;
- }
- tries++;
- if ( tries == 3) {
- System.exit(0);
- } else {
- System.out.println("To logon enter password:");
- }
- }
- }
- //Main menu - 1 for log off, 2 for change password and 3 for shut down
- public static void mainMenu() {
- while ( true ) {
- System.out.println("You now have the following choices:");
- System.out.println(" 1 - Change Password");
- System.out.println(" 2 - Log off");
- System.out.println(" 3 - Shut down");
- System.out.println("Please select:");
- //Takes valid user input
- int select = 0;
- while ( true ) {
- if ( console.hasNextInt() ) {
- select = console.nextInt();
- break;
- } else {
- console.next();
- }
- }
- if ( select == 1 ) {
- userCreationPassword();
- System.out.println("*** Password changed for user " + username);
- } else if ( select == 2 ) {
- System.out.println("*** User " + username + " is logged off");
- login();
- } else if ( select == 3 ) {
- System.out.println("*** User " + username + " is logged off");
- System.out.println("*** System shutting down.");
- System.exit(0);
- }
- }
- }
- public static boolean checkPWD(String password) {
- int digitCount = 0;
- int letterCount = 0;
- int alphaCount = 0;
- for ( int i = 0; i < password.length(); i++) {
- if( Character.isDigit(password.charAt(i)) ) {
- digitCount++;
- alphaCount++;
- }
- if ( Character.isLetter(password.charAt(i)) ) {
- letterCount++;
- alphaCount++;
- }
- }
- if ( digitCount >= 2 && letterCount >= 2 && alphaCount == password.length() ) {
- if( password.length() < 8 ) {
- return false;
- } else if( password.equals(password.toLowerCase()) ) {
- return false;
- } else if( password.equals(password.toUpperCase()) ) {
- return false;
- }
- return true;
- } else {
- return false;
- }
- }
- //Username checker
- public static boolean checkUserName(String username) {
- if ( Character.isLetter(username.charAt(0)) && username.contains(" ") == false ) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement