Commit 69f76ef2 authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Add Main class for Task 9: User input validation with rules

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="ms-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ValidationFramework.iml" filepath="$PROJECT_DIR$/ValidationFramework.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class Main {
public static void main(String[] args) {
String reddit = new String();
User user = new User("Anita", "reddit", "596534123"); // Invalid user
Validator<User> validator = new Validator<User>()
.addRule(ValidationRule.notBlank("Name", User::name))
.addRule(ValidationRule.notBlank("Email", User::email))
.addRule(ValidationRule.minLength("Password", User::password, 6));
try {
validator.validate(user);
} catch (ValidationException ve) {
System.out.println("Validation failed with errors:");
ve.getErrors().forEach(System.out::println);
}
}
}
package PACKAGE_NAME;
public class User {
}
package PACKAGE_NAME;
public class ValidationException {
}
@FunctionalInterface
public interface ValidationRule<T> {
String validate(T object);
// Static helper methods to define common rules
static <T> ValidationRule<T> notNull(String fieldName, java.util.function.Function<T, Object> getter) {
return object -> getter.apply(object) == null ? fieldName + " must not be null" : null;
}
static <T> ValidationRule<T> notBlank(String fieldName, java.util.function.Function<T, String> getter) {
return object -> {
String val = getter.apply(object);
return (val == null || val.trim().isEmpty()) ? fieldName + " must not be blank" : null;
};
}
static <T> ValidationRule<T> minLength(String fieldName, java.util.function.Function<T, String> getter, int min) {
return object -> {
String val = getter.apply(object);
return (val != null && val.length() < min) ? fieldName + " must be at least " + min + " characters" : null;
};
}
}
package PACKAGE_NAME;
public class Validator {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment