📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
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
// 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
// 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
// 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
// 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
package net.javaguides.login;
public class Main {
public static void main(String[] args) {
UserManager manager = new UserManager();
manager.start();
}
}
Comments
Post a Comment
Leave Comment