0

I am trying to create a simple logging in interface, where you can log in to change your information like email, username, password. The final piece of this puzzle for me is how can I send them back to the login screen after a lot of loops and steps:

package Exercises;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;


import java.util.HashMap;

public class Final {
    public static void main(String[] args) {
        ArrayList<String> email = new ArrayList<String>();
        HashMap<String, String> accounts = new HashMap<String, String>();
        try (Scanner scan = new Scanner(System.in)) {
            boolean n = true;
            while(n){
                System.out.println("1 - Login");
                System.out.println("2 - Register");
                String number1 = scan.nextLine();
                switch(number1){
                    case "1":
                        boolean loginCheck = true;
                        while(loginCheck){
                            System.out.print("Enter username: ");
                            String userIn = scan.nextLine();
                            if(accounts.containsKey(userIn)){
                                System.out.print("Enter password: ");
                                String passIn = scan.nextLine();
                                if (passIn == accounts.get(userIn)) {
                                    System.out.println("Welcome" + userIn + ", you can perform these actions: ");
                                    System.out.println("1 - Change username");
                                    System.out.println("2 - Change email");
                                    System.out.println("3 - Change password");
                                    System.out.println("4 - Log out");
                                    System.out.println("0 - Exit");
                                    String number2 = scan.nextLine();
                                    switch (number2) {
                                        case "0":
                                            System.exit(0);
                                            break;
                                        case "1":
                                            System.out.print("Type the new username: ");
                                            String newUser = scan.nextLine();
                                            accounts.remove(userIn);
                                            accounts.put(newUser, passIn);
                                            System.out.println("The username has been changed!");
                                            break;
                                        case "3":
                                            System.out.print("Type the old email: ");
                                            String oldMail = scan.nextLine();
                                            System.out.print("Type the new email: ");
                                            String newMail = scan.nextLine();
                                            email.set(email.indexOf(oldMail), newMail);
                                            System.out.println("Email has been changed!");
                                            break;
                                        case "4":
                                            
                                        default:
                                            break;
                                    }
                                }
                                else{
                                    System.out.println("Wrong password!");
                                    System.out.println("1 - Try again");
                                    System.out.println("2 - Forgot password");
                                    String fpassCheck = scan.nextLine();
                                    switch(fpassCheck){
                                        case "1":
                                            break;
                                        case "2":
                                            System.out.print("Type the email used to register: ");
                                            String mailCheck = scan.nextLine();
                                            if (email.contains(mailCheck)) {
                                                System.out.print("Type the new passwor: ");
                                                String newPass = scan.nextLine();
                                                accounts.remove(userIn);
                                                accounts.put(userIn, newPass);
                                                System.out.println("Password has been changed.");
                                                break;
                                            }
                                            else{
                                                System.out.println("Account do not exist yet!");
                                                break;
                                            }
                                        default:
                                            break;
                                    }
                                }
                            }
                            else{
                                System.out.println("Wrong username");
                            }
                        }                    
                    case "2":
                        System.out.print("Enter email: ");
                        String mailReg = scan.nextLine();
                        if(isValid(mailReg)){
                            if(email.contains(mailReg)){
                                System.out.println("There is already an existing account using this email!");
                                break;
                            }
                            else{
                                email.add(mailReg);
                                System.out.print("Create username: ");
                                String userReg = scan.nextLine();
                                boolean passCheck = true;
                                while (passCheck) {
                                    String passRegex = "^(?=[A-Z])(?=.*[. , - _ ;]).{7,15}$";
                                    Pattern pat = Pattern.compile(passRegex);
                                    System.out.print("Tạo pasword: ");
                                    String passReg = scan.nextLine();
                                    if (pat.matcher(passReg).matches()) {
                                        accounts.put(userReg, passReg);
                                        passCheck = false;
                                    }
                                    else{
                                        System.out.println("Password length must be between 7 and 15 characters, contains at least 1 uppercase, and 1 special character (. , - _ ;)");
                                    }
                                }
                            }
                        }
                        else{
                            System.out.println("Incorrect Email, please try again.");
                    }
                }
            }
        }
    }

    public static boolean isValid(String email)
    {
        String emailRegex = "^(.+)@(.+)$";
                              
        Pattern pat = Pattern.compile(emailRegex);
        if (email == null)
            return false;
        return pat.matcher(email).matches();
    }
}

After doing several actions, how can I make it so that it goes back to the login screen loop ? I assume that using multiple breaks is just really bad and might not even work properly.

Phong2902
  • 33
  • 2
  • 6
  • 1
    Your `main` method is *very* long. Please do yourself and us a favour and cut this wall of code into smaller pieces, each within its own method. – MC Emperor Dec 21 '21 at 13:39
  • Also, [How do I compare strings in Java?](https://stackoverflow.com/q/513832/507738) – MC Emperor Dec 21 '21 at 13:47

0 Answers0