diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..98b43d78 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,57 @@ #include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file + +std::string getErrorMessage(ErrorCode error) { + switch (error) { + default: + return ""; + + 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"; + } +} + +bool doPasswordsMatch(const std::string &password1, const std::string &password2) { + if (password1 == password2) { + return true; + } else { + return false; + } +} + +ErrorCode checkPasswordRules(const std::string &password) { + + if (password.length() < 9) { + return ErrorCode::PasswordNeedsAtLeastNineCharacters; + } + + else if (std::none_of(password.begin(), password.end(), [](char c) { return std::isdigit(c); })) { + return ErrorCode::PasswordNeedsAtLeastOneNumber; + } else if (std::none_of(password.begin(), password.end(), [](char c) { return std::isupper(c); })) { + return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; + } else if (std::none_of(password.begin(), password.end(), [](char c) { return std::ispunct(c); })) { + return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter; + } else + return ErrorCode::Ok; +} + +ErrorCode checkPassword(const std::string &password1, const std::string &password2) { + if (doPasswordsMatch(password1, password2)) + return checkPasswordRules(password1); + else + return ErrorCode::PasswordsDoNotMatch; +} diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 85160868..9a9c6fd6 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,2 +1,17 @@ -// TODO: I'm empty :) Put enum and function headers here. -// Don't forget the header guard - #pragma once \ No newline at end of file +#pragma once +#include +#include +#include + +enum class ErrorCode { + Ok, + PasswordNeedsAtLeastNineCharacters, + PasswordNeedsAtLeastOneNumber, + PasswordNeedsAtLeastOneSpecialCharacter, + PasswordNeedsAtLeastOneUppercaseLetter, + PasswordsDoNotMatch +}; +std::string getErrorMessage(ErrorCode error); +bool doPasswordsMatch(const std::string &password1, const std::string &password2); +ErrorCode checkPasswordRules(const std::string &password) ; +ErrorCode checkPassword(const std::string &password1, const std::string &password2);