1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class EntryValidator {
  6. /**
  7. * Validates a single entry based on predefined rules.
  8. * @param entry The entry to validate.
  9. * @param validationRules A map of field names to validation functions.
  10. * @return A map of error messages. Empty if no errors found.
  11. */
  12. public static Map<String, String> validateEntry(String entry, Map<String, ValidationFunction> validationRules) {
  13. Map<String, String> errors = new HashMap<>();
  14. for (Map.Entry<String, ValidationFunction> entryRule : validationRules.entrySet()) {
  15. String fieldName = entryRule.getKey();
  16. ValidationFunction validationFunction = entryRule.getValue();
  17. String fieldValue = getFieldValue(entry, fieldName); // Extract value for field
  18. if (validationFunction.validate(fieldValue)) {
  19. // Validation successful, no error
  20. } else {
  21. errors.put(fieldName, validationFunction.getErrorMessage());
  22. }
  23. }
  24. return errors;
  25. }
  26. /**
  27. * Extracts the value of a specific field from the entry string.
  28. * @param entry The entry string.
  29. * @param fieldName The name of the field.
  30. * @return The value of the field, or null if not found.
  31. */
  32. private static String getFieldValue(String entry, String fieldName) {
  33. String regex = "\\b" + fieldName + ":\\s*(.*?)(?:\\n|$)\\b"; //regex to extract field value
  34. Pattern pattern = Pattern.compile(regex);
  35. Matcher matcher = pattern.matcher(entry);
  36. if (matcher.find()) {
  37. return matcher.group(1).trim();
  38. }
  39. return null;
  40. }
  41. /**
  42. * Functional interface for validation functions.
  43. */
  44. @FunctionalInterface
  45. public interface ValidationFunction {
  46. boolean validate(String value);
  47. String getErrorMessage();
  48. }
  49. public static void main(String[] args) {
  50. // Example usage
  51. Map<String, ValidationFunction> rules = new HashMap<>();
  52. // Example rule: Name must be at least 3 characters
  53. rules.put("Name", value -> {
  54. if (value == null) return false;
  55. return value.length() >= 3;
  56. }, "Name must be at least 3 characters.");
  57. // Example rule: Email must be a valid email format
  58. rules.put("Email", value -> {
  59. if (value == null) return false;
  60. String emailRegex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";
  61. Pattern pattern = Pattern.compile(emailRegex);
  62. return pattern.matcher(value).matches();
  63. }, "Invalid email format.");
  64. // Example rule: Age must be a number between 0 and 150
  65. rules.put("Age", value -> {
  66. if(value == null) return false;
  67. try {
  68. int age = Integer.parseInt(value);
  69. return age >= 0 && age <= 150;
  70. } catch (NumberFormatException e) {
  71. return false;
  72. }
  73. }, "Age must be a number between 0 and 150.");
  74. String entry1 = "Name: John Doe\nEmail: john.doe@example.com\nAge: 30";
  75. String entry2 = "Name: Jo\nEmail: invalid-email\nAge: 200";
  76. String entry3 = "Name: Jane\nEmail: jane@example.com\nAge: abc";
  77. System.out.println("Entry 1 Errors: " + validateEntry(entry1, rules));
  78. System.out.println("Entry 2 Errors: " + validateEntry(entry2, rules));
  79. System.out.println("Entry 3 Errors: " + validateEntry(entry3, rules));
  80. }
  81. }

Add your comment