q4
import java.util.HashMap;
import java.util.Scanner;
public class RoleBasedMenu {
// Simulated database for storing user credentials and roles
static HashMap<String, String> userDatabase = new HashMap<>();
static HashMap<String, String> userRoles = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Pre-register a user for testing login
userDatabase.put("user2024", "Secure@123");
userRoles.put("user2024", "Customer"); // Assign a role
System.out.println("Welcome to the Login Console");
// Input for User ID
System.out.print("Enter User ID (5-20 characters): ");
String userID = scanner.nextLine();
if (userID.length() < 5 || userID.length() > 20) {
System.out.println("Error: User ID must be between 5 and 20 characters.");
return;
}
// Input for Password
System.out.print("Enter Password (Max 30 characters, include upper case, lower case, and special character): ");
String password = scanner.nextLine();
if (password.length() > 30 || !isValidPassword(password)) {
System.out.println("Error: Password must be max 30 characters and include at least one uppercase letter, one lowercase letter, and one special character.");
return;
}
// Check credentials
if (userDatabase.containsKey(userID) && userDatabase.get(userID).equals(password)) {
System.out.println("Login Successful!");
String role = userRoles.get(userID);
showMenu(role, scanner);
} else {
System.out.println("Error: Invalid User ID or Password.");
}
scanner.close();
}
// Validate password requirements
private static boolean isValidPassword(String password) {
boolean hasUppercase = false;
boolean hasLowercase = false;
boolean hasSpecialChar = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUppercase = true;
} else if (Character.isLowerCase(c)) {
hasLowercase = true;
} else if (!Character.isLetterOrDigit(c)) {
hasSpecialChar = true;
}
}
return hasUppercase && hasLowercase && hasSpecialChar;
}
// Display the menu based on role
private static void showMenu(String role, Scanner scanner) {
while (true) {
System.out.println("\nMenu Options for Role: " + role);
System.out.println("1. Home");
System.out.println("2. Booking Service");
System.out.println("3. Tracking");
System.out.println("4. Previous Booking");
System.out.println("5. Contact Support");
System.out.println("6. Logout");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Clear the buffer
switch (choice) {
case 1:
System.out.println("Navigating to Home...");
break;
case 2:
System.out.println("Navigating to Booking Service...");
proceedWithBooking(scanner);
break;
case 3:
System.out.println("Navigating to Tracking Screen...");
break;
case 4:
System.out.println("Navigating to Previous Booking Screen...");
break;
case 5:
System.out.println("Redirecting to Contact Support...");
break;
case 6:
System.out.println("Logging out...");
System.out.println("You have been logged out.");
return; // Exit the menu loop
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
// Simulate booking process
private static void proceedWithBooking(Scanner scanner) {
System.out.println("Booking Service:");
System.out.print("Enter Booking Details: ");
String bookingDetails = scanner.nextLine();
System.out.println("Booking Confirmed for: " + bookingDetails);
}
}