public class Password { private final int nrUpperShould, nrLowerShould, nrSpecialShould, nrNumbersShould, lengthShould; private final char[] illegalChars; public Password(int nrUpperShould, int nrLowerShould, int nrSpecialShould, int nrNumbersShould, int lengthShould, char[] illegalChars) { this.nrUpperShould = nrUpperShould; this.nrLowerShould = nrLowerShould; this.nrSpecialShould = nrSpecialShould; this.nrNumbersShould = nrNumbersShould; this.lengthShould = lengthShould; this.illegalChars = illegalChars; } public void checkFormat(String pwd) throws IllegalCharExc, NotEnoughExc, NotLongEnoughExc{ int countUpper = 0, countLower = 0, countSpecial = 0, countNumber = 0; if (pwd.length() < lengthShould) throw new NotLongEnoughExc(lengthShould, pwd.length()); for (int i = 0; i < pwd.length(); i++) { char c = pwd.charAt(i); // zuerst ungültige Zeichen prüfen for (int j = 0; j < illegalChars.length; j++) if (c == illegalChars[j]) throw new IllegalCharExc(c); if (c >= 'a' && c <= 'z') countLower++; else if (c >= 'A' && c <= 'Z') countUpper++; else if (c >= '0' && c <= '9') countNumber++; else // optional hier noch eine Bedingung für Sonderzeichen? countSpecial++; } if (countUpper < nrUpperShould) throw new NotEnoughUpper(nrUpperShould, countUpper); if (countLower < nrLowerShould) throw new NotEnoughLower(nrLowerShould, countLower); if (countNumber < nrNumbersShould) throw new NotEnoughNumber(nrNumbersShould, countNumber); if (countSpecial < nrSpecialShould) throw new NotEnoughSpecial(nrSpecialShould, countSpecial); } public static void main(String[] args) { int reqUpper = 2, reqLower = 1, reqSpecial = 1, reqNumbers = 2, reqLength = 5; char[] forbidden = {'\n', ' '}; String pass = "A_.B1'b\t,2"; Password pw = new Password(reqUpper, reqLower, reqSpecial, reqNumbers, reqLength, forbidden); try { pw.checkFormat(pass); System.out.println("Erfolgreich!"); } catch (IllegalCharExc | NotEnoughExc | NotLongEnoughExc e) { System.out.println("Fehler: " + e.toString()); } } }