OneCompiler

q5

743

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. Tracking");
        System.out.println("3. Delivery Status");
        System.out.println("4. Pickup Scheduling");
        System.out.println("5. Previous Booking");
        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("You are already on the Home page.");
                break;
            case 2:
                System.out.println("Navigating to Tracking Screen...");
                trackingScreen();
                break;
            case 3:
                System.out.println("Navigating to Delivery Status Screen...");
                deliveryStatusScreen();
                break;
            case 4:
                System.out.println("Navigating to Pickup Scheduling Screen...");
                pickupSchedulingScreen();
                break;
            case 5:
                System.out.println("Navigating to Previous Booking Screen...");
                previousBookingScreen();
                break;
            case 6:
                System.out.println("Logging out...");
                System.out.println("You have been redirected to the first page.");
                return; // Exit the menu loop
            default:
                System.out.println("Invalid choice. Please try again.");
        }
    }
}

// Simulated methods for different screens
private static void trackingScreen() {
    System.out.println("Tracking Screen: Enter Tracking ID to check status.");
}

private static void deliveryStatusScreen() {
    System.out.println("Delivery Status Screen: Enter Order ID to check delivery status.");
}

private static void pickupSchedulingScreen() {
    System.out.println("Pickup Scheduling Screen: Enter Pickup Details to schedule.");
}

private static void previousBookingScreen() {
    System.out.println("Previous Booking Screen: Displaying all previous bookings...");
}

}