[go: up one dir, main page]

0% found this document useful (0 votes)
5 views3 pages

Create User Account Log in Log Out

The document outlines a Java program for a user account system that includes functionalities for signing up, logging in, and logging out with OTP verification. It utilizes a graphical user interface for user input and displays messages using JOptionPane. The program manages user credentials and session states through a static UserAccount class.

Uploaded by

bernardmarcella1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Create User Account Log in Log Out

The document outlines a Java program for a user account system that includes functionalities for signing up, logging in, and logging out with OTP verification. It utilizes a graphical user interface for user input and displays messages using JOptionPane. The program manages user credentials and session states through a static UserAccount class.

Uploaded by

bernardmarcella1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import javax.swing.

JOptionPane;
import java.util.Random;

public class UserAccountSystem {

public static void main(String[] args) {


signUp();
}

static void signUp() {


String inputName = getUserInput("Enter Username:");
String inputPassword = getUserInput("Enter Password:");
setUserName(inputName);
setPassword(inputPassword);
generateOTP();
displayMessage("An OTP has been sent: " + getOtpCode());
verifyOTP();
}

static void verifyOTP() {


String inputOtp = getUserInput("Enter OTP to verify:");
if (inputOtp.equals(getOtpCode())) {
displayMessage("Account successfully created!");
login();
} else {
displayMessage("Invalid OTP. Try again.");
verifyOTP();
}
}

static void login() {


if (getLoggedIn()) {
displayMessage("Already logged in.");
logout();
} else {
String inputName = getUserInput("Enter Username to login:");
String inputPassword = getUserInput("Enter Password:");
if (inputName.equals(getUserName()) &&
inputPassword.equals(getPassword())) {
setLoggedIn(true);
displayMessage("Login successful!");
logout();
} else {
displayMessage("Username or Password is incorrect.");
login();
}
}
}

static void logout() {


String choice = getUserInput("Enter your password to logout or type 'OTP'
for OTP logout:");
if (choice.equals(getPassword())) {
setLoggedIn(false);
displayMessage("Logged out successfully!");
System.exit(0); // Exit the program
} else if (choice.equals("OTP")) {
String inputOtp = getUserInput("Enter OTP for logout:");
if (inputOtp.equals(getOtpCode())) {
setLoggedIn(false);
displayMessage("Logged out using OTP!");
System.exit(0); // Exit the program
} else {
displayMessage("Invalid OTP.");
logout();
}
} else {
displayMessage("Invalid password.");
logout();
}
}

static String getUserInput(String message) {


return JOptionPane.showInputDialog(message);
}

static void setUserName(String userName) {


UserAccount.setUserName(userName);
}

static String getUserName() {


return UserAccount.getUserName();
}

static void setPassword(String password) {


UserAccount.setPassword(password);
}

static String getPassword() {


return UserAccount.getPassword();
}

static void generateOTP() {


Random random = new Random();
int otp = random.nextInt(10000);
setOtpCode(String.format("%04d", otp));
}

static void setOtpCode(String otp) {


UserAccount.setOtpCode(otp);
}

static String getOtpCode() {


return UserAccount.getOtpCode();
}

static void setLoggedIn(boolean loggedIn) {


UserAccount.setLoggedIn(loggedIn);
}

static boolean getLoggedIn() {


return UserAccount.getLoggedIn();
}

static void displayMessage(String message) {


JOptionPane.showMessageDialog(null, message);
}
}
class UserAccount {
static String userName = "";
static String password = "";
static String otpCode = "";
static boolean loggedIn = false;

static void setUserName(String userName) {


UserAccount.userName = userName;
}

static String getUserName() {


return userName;
}

static void setPassword(String password) {


UserAccount.password = password;
}

static String getPassword() {


return password;
}

static void setOtpCode(String otpCode) {


UserAccount.otpCode = otpCode;
}

static String getOtpCode() {


return otpCode;
}

static void setLoggedIn(boolean loggedIn) {


UserAccount.loggedIn = loggedIn;
}

static boolean getLoggedIn() {


return loggedIn;
}
}

You might also like