import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class FormDebugger {
private static final String DEBUG_MODE_KEY = "debug";
private static final String SANITY_CHECKS_KEY = "sanity_checks";
private static final Map<String, Object> formDebugConfig = new HashMap<>();
public static boolean isDebuggingEnabled() {
// Check if debug mode is enabled in the configuration.
return formDebugConfig.containsKey(DEBUG_MODE_KEY) && (Boolean) formDebugConfig.get(DEBUG_MODE_KEY);
}
public static boolean areSanityChecksEnabled() {
// Check if sanity checks are enabled.
return formDebugConfig.containsKey(SANITY_CHECKS_KEY) && (Boolean) formDebugConfig.get(SANITY_CHECKS_KEY);
}
public static void configureFormDebug(Map<String, Object> config) {
// Allow configuration of debug settings.
formDebugConfig.putAll(config);
}
public static boolean validateForm(Map<String, String> formData) {
// Perform sanity checks if enabled.
if (!areSanityChecksEnabled()) {
return true; // Skip validation if not enabled.
}
// Example sanity checks - customize as needed.
if (formData.get("name") == null || formData.get("name").trim().isEmpty()) {
System.err.println("Error: Name is required.");
return false;
}
if (formData.get("email") == null || !formData.get("email").matches(".*@.*")) {
System.err.println("Error: Invalid email format.");
return false;
}
if (formData.get("age") == null) {
System.err.println("Error: Age is required.");
return false;
}
try {
int age = Integer.parseInt(formData.get("age"));
if (age < 0 || age > 150) {
System.err.println("Error: Age must be between 0 and 150.");
return false;
}
} catch (NumberFormatException e) {
System.err.println("Error: Age must be a number.");
return false;
}
return true; // All checks passed.
}
public static void main(String[] args) {
//Example Usage
Map<String,Object> debugConfig = new HashMap<>();
debugConfig.put(DEBUG_MODE_KEY, true);
debugConfig.put(SANITY_CHECKS_KEY, true);
configureFormDebug(debugConfig);
Map<String, String> sampleForm = new HashMap<>();
sampleForm.put("name", "John Doe");
sampleForm.put("email", "john.doe@example.com");
sampleForm.put("age", "30");
if (validateForm(sampleForm)) {
System.out.println("Form is valid.");
} else {
System.out.println("Form is invalid.");
}
Map<String, String> invalidForm = new HashMap<>();
invalidForm.put("name", "");
invalidForm.put("email", "invalid");
invalidForm.put("age", "abc");
if (validateForm(invalidForm)) {
System.out.println("Form is valid.");
} else {
System.out.println("Form is invalid.");
}
}
}
Add your comment