Class BaseUI

java.lang.Object
com.ntu.fdae.group1.bto.views.BaseUI
Direct Known Subclasses:
ApplicantUI, HDBManagerUI, HDBOfficerUI, LoginUI, MainMenuUI

public abstract class BaseUI extends Object
Abstract base class for common UI functionalities in the BTO Management System.

This class provides a set of reusable UI components and input handling methods that are used across different user interfaces in the system. It centralizes common operations like prompting for input, displaying messages, and formatting data for display.

All specific UI classes should extend this class to inherit its functionality and maintain a consistent user experience throughout the application.

  • Field Details

    • scanner

      protected Scanner scanner
      Scanner object for reading user input from the console. This is initialized in the constructor and used by all input prompting methods.
    • DATE_FORMATTER

      protected static final DateTimeFormatter DATE_FORMATTER
      Standard date formatter used consistently across the UI for displaying and parsing dates in ISO format (YYYY-MM-DD).
  • Constructor Details

    • BaseUI

      public BaseUI(Scanner scanner)
      Constructs a BaseUI with the specified Scanner for input operations.
      Parameters:
      scanner - The Scanner object to use for reading user input
      Throws:
      IllegalArgumentException - if scanner is null
  • Method Details

    • displayMessage

      public void displayMessage(String message)
      Displays a standard message to the console.
      Parameters:
      message - The message to display.
    • displayError

      protected void displayError(String message)
      Displays an error message, typically formatted differently.
      Parameters:
      message - The error message to display.
    • promptForInput

      public String promptForInput(String prompt)
      Prompts the user for string input.
      Parameters:
      prompt - The message to display before input.
      Returns:
      The string entered by the user.
    • promptForPassword

      public String promptForPassword(String prompt)
      Prompts the user for a password input, masking the input for security.

      This method uses System.console() to read the password securely. If System.console() is not available (e.g., in some IDEs), it falls back to regular input.

      Parameters:
      prompt - The message to display before input.
      Returns:
      The password entered by the user.
    • promptForPasswordWithToggle

      public String promptForPasswordWithToggle(String prompt)
      Prompts the user for potentially sensitive input (like a password), offering an option to make the input visible. Defaults to masked input if the user doesn't explicitly choose 'Y'.
      Parameters:
      prompt - The message to display before input.
      Returns:
      The string entered by the user. Returns null if reading fails.
    • promptForInt

      public int promptForInt(String prompt)
      Prompts the user for an integer input with basic error handling.
      Parameters:
      prompt - The message to display before input.
      Returns:
      The integer entered by the user, or -1 if input is invalid.
    • promptForDouble

      public double promptForDouble(String prompt)
      Prompts the user for a double input with basic error handling.
      Parameters:
      prompt - The message to display before input.
      Returns:
      The double entered by the user, or -1.0 if input is invalid.
    • promptForDate

      public LocalDate promptForDate(String prompt)
      Prompts the user for a date input with basic error handling.
      Parameters:
      prompt - The message to display before input.
      Returns:
      The LocalDate entered by the user, or null if input is invalid.
    • promptForEnum

      public <E extends Enum<E>> E promptForEnum(String prompt, Class<E> enumClass, List<E> allowedValues)
      Prompts the user to select an item from a list of enum values.

      Displays a numbered menu of enum options and processes the user's selection. The user can cancel the selection by choosing option 0.

      Type Parameters:
      E - The enum type
      Parameters:
      prompt - The message to display before showing options
      enumClass - The class of the enum
      allowedValues - The list of enum values to display as options
      Returns:
      The selected enum value, or null if the selection was cancelled
    • formatEnumName

      protected String formatEnumName(Enum<?> enumConstant)
      Formats an enum constant for user-friendly display.

      Converts the enum name from uppercase with underscores to a title case format with spaces. For example, "TWO_ROOM" becomes "Two room".

      Parameters:
      enumConstant - The enum constant to format
      Returns:
      A user-friendly string representation of the enum constant
    • promptForConfirmation

      public boolean promptForConfirmation(String prompt)
      Prompts the user for confirmation (Y/N). Accepts 'Y'/'y' for yes, 'N'/'n' or blank/Enter for no. Reprompts on invalid input.
      Parameters:
      prompt - The message to display before input.
      Returns:
      True if user confirms (Y/y), false otherwise (N/n or Enter).
    • pause

      protected void pause()
      Pauses execution until the user presses Enter.
    • displayHeader

      protected void displayHeader(String title)
      Displays a standard menu header.
      Parameters:
      title - The title of the menu.
    • displayList

      protected <T> void displayList(List<T> items, Function<T,String> formatter)
      Displays a list of items with custom formatting.
      Type Parameters:
      T - The type of items in the list.
      Parameters:
      items - The list of items to display.
      formatter - Function to convert each item to a string.
    • clearConsole

      protected void clearConsole()
      Clears the console. Platform dependent, often avoided in simple CLIs.
    • formatDateSafe

      protected String formatDateSafe(LocalDate date)
      Formats a LocalDate safely for display, handling null values.

      Uses the standard DATE_FORMATTER to format the date, or returns "N/A" if the date is null.

      Parameters:
      date - The LocalDate to format
      Returns:
      The formatted date string, or "N/A" if date is null