import javax.swing.
*;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.regex.Pattern;
public class Main {
private static User user = null;
private static final DateTimeFormatter inputFormatter =
DateTimeFormatter.ofPattern("MM/dd/yyyy");
private static final DateTimeFormatter outputFormatter =
DateTimeFormatter.ofPattern("MMMM d, yyyy");
public static void main(String[] args) {
bernardAndIvanMethod();
}
private static void bernardAndIvanMethod() {
signUp();
login();
logout();
}
private static void signUp() {
String name = getInput("Enter your name:", "Sign Up");
LocalDate birthday = null;
while (birthday == null) {
String birthdayStr = getInput("Enter your birthday (MM/DD/YYYY):",
"Sign Up");
birthday = parseBirthday(birthdayStr);
}
String username = getInput("Enter your username (lowercase):", "Sign
Up").toLowerCase();
String password = getInput("Enter your password (at least 8 characters,
combination of uppercase, lowercase, numbers, and symbols):", "Sign Up");
while (!isValidPassword(password)) {
JOptionPane.showMessageDialog(null, "Invalid password format. Please
try again.", "Sign Up", JOptionPane.ERROR_MESSAGE);
password = getInput("Enter your password (at least 8 characters,
combination of uppercase, lowercase, numbers, and symbols):", "Sign Up");
}
user = new User(name, birthday, username, password);
JOptionPane.showMessageDialog(null, "Registration successful! You can now
log in.", "Sign Up", JOptionPane.INFORMATION_MESSAGE);
}
private static void login() {
for (int attempts = 0; attempts < 3; attempts++) {
String inputUsername = getInput("Enter your username:", "Log
In").toLowerCase();
String inputPassword = getInput("Enter your password:", "Log In");
if (user == null || !user.getUsername().equals(inputUsername)) {
JOptionPane.showMessageDialog(null, "User account does not exist.
Please sign up first.", "Log In", JOptionPane.ERROR_MESSAGE);
return;
}
if (user.getPassword().equals(inputPassword)) {
displayUserInfo(user);
return;
} else {
JOptionPane.showMessageDialog(null, "Incorrect username or
password. Attempts left: " + (2 - attempts), "Log In", JOptionPane.ERROR_MESSAGE);
}
}
JOptionPane.showMessageDialog(null, "Maximum attempts reached. Account
locked.", "Log In", JOptionPane.ERROR_MESSAGE);
}
private static void logout() {
JOptionPane.showMessageDialog(null, "You have logged out successfully.",
"Log Out", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
private static String getInput(String message, String title) {
String input;
do {
input = JOptionPane.showInputDialog(null, message, title,
JOptionPane.QUESTION_MESSAGE);
if (input == null) {
System.exit(0);
} else if (input.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a
value.", title, JOptionPane.ERROR_MESSAGE);
}
} while (input.trim().isEmpty());
return input;
}
private static LocalDate parseBirthday(String birthdayStr) {
try {
LocalDate birthday = LocalDate.parse(birthdayStr, inputFormatter);
if (birthday.isAfter(LocalDate.now())) {
JOptionPane.showMessageDialog(null, "Birthday cannot be in the
future. Please enter a valid date.", "Sign Up", JOptionPane.ERROR_MESSAGE);
return null;
}
return birthday;
} catch (DateTimeParseException e) {
JOptionPane.showMessageDialog(null, "Invalid date format. Please use
MM/DD/YYYY.", "Sign Up", JOptionPane.ERROR_MESSAGE);
return null;
}
}
private static int calculateAge(LocalDate birthday) {
LocalDate today = LocalDate.now();
return Period.between(birthday, today).getYears();
}
private static boolean isValidPassword(String password) {
return password.length() >= 8 &&
Pattern.compile("[A-Z]").matcher(password).find() &&
Pattern.compile("[a-z]").matcher(password).find() &&
Pattern.compile("[0-9]").matcher(password).find() &&
Pattern.compile("[^A-Za-z0-9]").matcher(password).find();
}
private static void displayUserInfo(User user) {
int age = calculateAge(user.getBirthday());
JOptionPane.showMessageDialog(null,
"Congratulations, " + user.getUsername() + "! You have successfully
logged in.\n" +
"Name: " + user.getName() + "\n" +
"Birthday: " + user.getBirthday().format(outputFormatter) +
"\n" +
"Age: " + age, "Log In",
JOptionPane.INFORMATION_MESSAGE);
}
}
class User {
private String name;
private LocalDate birthday;
private String username;
private String password;
public User(String name, LocalDate birthday, String username, String password)
{
this.name = name;
this.birthday = birthday;
this.username = username;
this.password = password;
}
public String getName() {
return name;
}
public LocalDate getBirthday() {
return birthday;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}