Améliore le code suivant en utilisant au mieux les bonnes pratiques associées au langage…
- User
Améliore le code suivant en utilisant au mieux les bonnes pratiques associées au langage JAVA : package org.javaavance.exception.passwords;
public class LambdaPasswordValidator {
@FunctionalInterface interface Validator<T> { boolean checkRule(T t) throws CustomException; } private final Validator<T> validator; public LambdaPasswordValidator(Validator<T> validator) { this.validator = validator; } public LambdaPasswordValidator andThen(Validator<T> other) { Validator<T> combined = s -> validator.checkRule(s) && other.checkRule(s); return new LambdaPasswordValidator(combined); } // Minimum de taille 8 static Validator<String> lengthRule = (pwd) -> { if (pwd == null || pwd.length() < 8) throw new LengthException(pwd); return true; }; // Contient des lettres minuscules et majuscules static Validator<String> caseRule = (pwd) -> { if (!pwd.matches("^(?=.*[a-z])(?=.*[A-Z]).+$")) throw new LetterCaseException(pwd); return true; }; // Contient au minimum 2 nombres static Validator<String> digitRule = (pwd) -> { long count = pwd.chars().filter(Character::isDigit).count(); if (count < 2) throw new DigitCountException(pwd); return true; }; static LambdaPasswordValidator<String> passwordValidator = new LambdaPasswordValidator<String>(lengthRule).andThen(caseRule).andThen(digitRule); public void checkRule(T t) throws CustomException { validator.checkRule(t); } public static void validatePassword(String password) throws CustomException { // Valide le mot de passe avec les règles passwordValidator.checkRule(password); System.out.println("Le mot de passe " + password + " est valide!"); } public static void main(String[] args) { try { validatePassword(null); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("weak123"); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("StrongPwd5"); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("StrongPwd45"); } catch (CustomException e) { System.err.println(e.getMessage()); } }}
class CustomException extends Exception { CustomException(String message) { super(message); } }
class LengthException extends CustomException { LengthException(String pwd) { super("Le mot de passe " + pwd + " doit avoir une longueur minimale de 8 caractères."); } }
class LetterCaseException extends CustomException { LetterCaseException(String pwd) { super("Le mot de passe " + pwd + " doit contenir des lettres minuscules et majuscules."); } }
class DigitCountException extends CustomException { DigitCountException(String pwd) { super("Le mot de passe " + pwd + " doit contenir au moins 2 chiffres."); } }
- Assistant
package org.javaavance.exception.passwords; public class LambdaPasswordValidator <T> { @FunctionalInterface interface Validator<T> { boolean checkRule(T t) throws CustomException; } private final Validator<T> validator; public LambdaPasswordValidator(Validator<T> validator) { this.validator = validator; } public LambdaPasswordValidator andThen(Validator<T> other) { Validator<T> combined = s -> validator.checkRule(s) && other.checkRule(s); return new LambdaPasswordValidator(combined); } // Minimum length of 8 static Validator<String> lengthRule = (pwd) -> { if (pwd == null || pwd.length() < 8) throw new LengthException(pwd); return true; }; // Contains lowercase and uppercase letters static Validator<String> caseRule = (pwd) -> { if (!pwd.matches("^(?=.*[a-z])(?=.*[A-Z]).+$")) throw new LetterCaseException(pwd); return true; }; // Contains at least 2 digits static Validator<String> digitRule = (pwd) -> { long count = pwd.chars().filter(Character::isDigit).count(); if (count < 2) throw new DigitCountException(pwd); return true; }; static LambdaPasswordValidator<String> passwordValidator = new LambdaPasswordValidator<String>(lengthRule).andThen(caseRule).andThen(digitRule); public void checkRule(T t) throws CustomException { validator.checkRule(t); } public static void validatePassword(String password) throws CustomException { // Validate the password with the rules passwordValidator.checkRule(password); System.out.println("The password " + password + " is valid!"); } public static void main(String[] args) { try { validatePassword(null); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("weak123"); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("StrongPwd5"); } catch (CustomException e) { System.err.println(e.getMessage()); } try { validatePassword("StrongPwd45"); } catch (CustomException e) { System.err.println(e.getMessage()); } } } class CustomException extends Exception { CustomException(String message) { super(message); } } class LengthException extends CustomException { LengthException(String pwd) { super("The password " + pwd + " must have a minimum length of 8 characters."); } } class LetterCaseException extends CustomException { LetterCaseException(String pwd) { super("The password " + pwd + " must contain both lowercase and uppercase letters."); } } class DigitCountException extends CustomException { DigitCountException(String pwd) { super("The password " + pwd + " must contain at least 2 digits."); } }