Class BaseUI
- Direct Known Subclasses:
ApplicantUI
,HDBManagerUI
,HDBOfficerUI
,LoginUI
,MainMenuUI
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 Summary
FieldsModifier and TypeFieldDescriptionprotected static final DateTimeFormatter
Standard date formatter used consistently across the UI for displaying and parsing dates in ISO format (YYYY-MM-DD).protected Scanner
Scanner object for reading user input from the console. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Clears the console.protected void
displayError
(String message) Displays an error message, typically formatted differently.protected void
displayHeader
(String title) Displays a standard menu header.protected <T> void
displayList
(List<T> items, Function<T, String> formatter) Displays a list of items with custom formatting.void
displayMessage
(String message) Displays a standard message to the console.protected String
formatDateSafe
(LocalDate date) Formats a LocalDate safely for display, handling null values.protected String
formatEnumName
(Enum<?> enumConstant) Formats an enum constant for user-friendly display.protected void
pause()
Pauses execution until the user presses Enter.boolean
promptForConfirmation
(String prompt) Prompts the user for confirmation (Y/N).promptForDate
(String prompt) Prompts the user for a date input with basic error handling.double
promptForDouble
(String prompt) Prompts the user for a double input with basic error handling.<E extends Enum<E>>
EpromptForEnum
(String prompt, Class<E> enumClass, List<E> allowedValues) Prompts the user to select an item from a list of enum values.promptForInput
(String prompt) Prompts the user for string input.int
promptForInt
(String prompt) Prompts the user for an integer input with basic error handling.promptForPassword
(String prompt) Prompts the user for a password input, masking the input for security.promptForPasswordWithToggle
(String prompt) Prompts the user for potentially sensitive input (like a password), offering an option to make the input visible.
-
Field Details
-
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
Standard date formatter used consistently across the UI for displaying and parsing dates in ISO format (YYYY-MM-DD).
-
-
Constructor Details
-
BaseUI
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
Displays a standard message to the console.- Parameters:
message
- The message to display.
-
displayError
Displays an error message, typically formatted differently.- Parameters:
message
- The error message to display.
-
promptForInput
Prompts the user for string input.- Parameters:
prompt
- The message to display before input.- Returns:
- The string entered by the user.
-
promptForPassword
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
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
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
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
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 optionsenumClass
- The class of the enumallowedValues
- The list of enum values to display as options- Returns:
- The selected enum value, or null if the selection was cancelled
-
formatEnumName
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
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
Displays a standard menu header.- Parameters:
title
- The title of the menu.
-
displayList
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
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
-