🎓 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 (178K+ 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
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!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment