diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..e2b7fee3 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,56 @@ -#include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file + #include "validation.hpp" + #include + #include + #include + + std::string getErrorMessage(ErrorCode test) + { + switch (test) { + case ErrorCode::Ok: + return "Ok"; + case ErrorCode::PasswordNeedsAtLeastNineCharacters: + return "Password needs to have at least nine characters"; + case ErrorCode::PasswordNeedsAtLeastOneNumber: + return "Password needs to have at least one number"; + case ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter: + return "Password needs to have at least one special character"; + case ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter: + return "Password needs to have at least one uppercase letter"; + case ErrorCode::PasswordsDoNotMatch: + return "Passwords do not match"; + default: + return "Nieznany blad"; + } + }; + + bool doPasswordsMatch(std::string a, std::string b) { + + return a == b; + } + + ErrorCode checkPasswordRules(std::string a) { + if (a.size() >= 9) { + if(std::any_of(a.begin(), a.end(), [](char x) { return isdigit(x); } )){ + if (std::any_of(a.begin(), a.end(), [](char x) { return ispunct(x); })) { + if (std::any_of(a.begin(), a.end(), [](char x) { return isupper(x); })) { + return ErrorCode::Ok; + } + else { return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; } + } + else { + return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter; + } + } + else { return ErrorCode::PasswordNeedsAtLeastOneNumber;} + } + else { + return ErrorCode::PasswordNeedsAtLeastNineCharacters;} + } + + ErrorCode checkPassword(std::string a, std::string b) { + if (doPasswordsMatch(a, b)) { + return checkPasswordRules(a); + } + else return ErrorCode::PasswordsDoNotMatch; + } + diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 85160868..05980ba1 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,2 +1,19 @@ -// TODO: I'm empty :) Put enum and function headers here. -// Don't forget the header guard - #pragma once \ No newline at end of file +#ifndef VALIDATION_HPP +#define VALIDATION_HPP +#include + +enum class ErrorCode { + Ok = 2, + PasswordNeedsAtLeastNineCharacters, + PasswordNeedsAtLeastOneNumber, + PasswordNeedsAtLeastOneSpecialCharacter, + PasswordNeedsAtLeastOneUppercaseLetter, + PasswordsDoNotMatch +}; + +std::string getErrorMessage(ErrorCode test); +bool doPasswordsMatch(std::string a, std::string b); +ErrorCode checkPasswordRules(std::string a); +ErrorCode checkPassword(std::string a, std::string b); + +#endif