Building a Login and Registration System with Java

In this step-by-step tutorial, you'll learn how to build a basic login and registration system using Core Java. This system will allow users to register with a username, password, email, and a secret question for password recovery. Users will be able to log in using their credentials and reset their password if forgotten.

Overview

The system consists of three main components:
  • User.java: Defines the structure of a user with username, password, email, secret question, and answer.
  • UserManager.java: Handles user registration, login, and password reset operations.
  • Main.java: Contains the entry point of the application, initializing and starting the user management system.

Step 1: Creating the User Class

The User class represents a user in our system. It stores the user's credentials and provides a method to update the password.
package net.javaguides.login;

public class User {
    private String username;
    private String password;
    private String email;
    private String secretQuestion;
    private String secretAnswer;

    // Constructor
    public User(String username,
                String password,
                String email,
                String secretQuestion,
                String secretAnswer) {
        this.username = username;
        this.password = password;
        this.email = email;
        this.secretQuestion = secretQuestion;
        this.secretAnswer = secretAnswer;
    }

    // Getters and Setters
    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public String getSecretQuestion() {
        return secretQuestion;
    }

    public String getSecretAnswer() {
        return secretAnswer;
    }

    // Method to update password
    public void setPassword(String password) {
        this.password = password;
    }
}

Step 2: Implementing the UserManager Class 

The UserManager class manages the registration, login, and password reset functionalities. It stores registered users in a list and uses a Scanner for input operations.
package net.javaguides.login;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserManager {
    private List<User> users = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    // Register a new user
    public void register() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        System.out.print("Enter email: ");
        String email = scanner.nextLine();
        System.out.print("Enter secret question: ");
        String secretQuestion = scanner.nextLine();
        System.out.print("Enter secret answer: ");
        String secretAnswer = scanner.nextLine();

        users.add(new User(username, password, email, secretQuestion, secretAnswer));
        System.out.println("Registration successful.");
    }

    // User login
    public void login() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("Login successful.");
                return;
            }
        }
        System.out.println("Login failed. Username or password incorrect.");
    }

    // Forget password operation
    public void forgetPassword() {
        System.out.print("Enter your username: ");
        String username = scanner.nextLine();

        for (User user : users) {
            if (user.getUsername().equals(username)) {
                System.out.println("Answer the secret question: " + user.getSecretQuestion());
                String answer = scanner.nextLine();
                if (user.getSecretAnswer().equals(answer)) {
                    System.out.print("Enter new password: ");
                    String newPassword = scanner.nextLine();
                    user.setPassword(newPassword);
                    System.out.println("Password reset successful.");
                    return;
                } else {
                    System.out.println("Incorrect answer.");
                    return;
                }
            }
        }
        System.out.println("User not found.");
    }

    // Start the user management operations
    public void start() {
        while (true) {
            System.out.println("\nUser Management System");
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.println("3. Forget Password");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline left-over

            switch (choice) {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    forgetPassword();
                    break;
                case 4:
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid choice. Please select 1, 2, 3, or 4.");
                    break;
            }
        }
    }
}

Step 3: Registration 

Users can register by providing a username, password, email, and a secret question and answer for password recovery.
    // Register a new user
    public void register() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        System.out.print("Enter email: ");
        String email = scanner.nextLine();
        System.out.print("Enter secret question: ");
        String secretQuestion = scanner.nextLine();
        System.out.print("Enter secret answer: ");
        String secretAnswer = scanner.nextLine();

        users.add(new User(username, password, email, secretQuestion, secretAnswer));
        System.out.println("Registration successful.");
    }

Step 4: Login 

The login method checks if the username and password entered match those of any registered user.

    // User login
    public void login() {
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("Login successful.");
                return;
            }
        }
        System.out.println("Login failed. Username or password incorrect.");
    }

Step 5: Password Reset 

If users forget their password, they can reset it by answering their secret questions correctly.
    // Forget password operation
    public void forgetPassword() {
        System.out.print("Enter your username: ");
        String username = scanner.nextLine();

        for (User user : users) {
            if (user.getUsername().equals(username)) {
                System.out.println("Answer the secret question: " + user.getSecretQuestion());
                String answer = scanner.nextLine();
                if (user.getSecretAnswer().equals(answer)) {
                    System.out.print("Enter new password: ");
                    String newPassword = scanner.nextLine();
                    user.setPassword(newPassword);
                    System.out.println("Password reset successful.");
                    return;
                } else {
                    System.out.println("Incorrect answer.");
                    return;
                }
            }
        }
        System.out.println("User not found.");
    }

Step 6: Starting the System 

The start method provides a simple text-based menu for accessing the system's functionalities.
    // Start the user management operations
    public void start() {
        while (true) {
            System.out.println("\nUser Management System");
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.println("3. Forget Password");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline left-over

            switch (choice) {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    forgetPassword();
                    break;
                case 4:
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid choice. Please select 1, 2, 3, or 4.");
                    break;
            }
        }
    }

Step 7: Running the Application 

Finally, the Main class initializes a UserManager instance and starts the system.
package net.javaguides.login;

public class Main {
    public static void main(String[] args) {
        UserManager manager = new UserManager();
        manager.start();
    }
}

Conclusion 

Congratulations! You've built a simple login and registration system using Core Java. This application covers essential programming concepts such as class design, list management, and handling user input. You can extend this project by adding features like email verification, password encryption, or storing users in a database. 

Happy coding!

Comments