From 3959360cff85aff9c0a5a15ed7588653b3c31b19 Mon Sep 17 00:00:00 2001 From: MichalG315 Date: Sat, 19 Jul 2025 15:08:19 +0200 Subject: [PATCH 1/2] password-check works --- homework/password-check/validation.cpp | 62 +++++++++++++++++++++++++- homework/password-check/validation.hpp | 19 +++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index a2f12ff3..732c7f90 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -1,2 +1,62 @@ #include "validation.hpp" -// TODO: Put implementations here \ No newline at end of file +#include +#include +#include + +std::string getErrorMessage(ErrorCode errorCode) { + switch (errorCode) { + 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 "-1"; + } +} + +bool doPasswordsMatch(std::string pass1, std::string pass2) { + + if (pass1.empty() && pass2.empty()) { + return true; + } + + if (pass1.empty() || pass2.empty()) { + return false; + } + + if (pass1.compare(pass2) == 0) { + return true; + } else { + return false; + } +} + +ErrorCode checkPasswordRules(std::string pass) { + if (pass.length() < 9) { + return ErrorCode::PasswordNeedsAtLeastNineCharacters; + } else if (!std::any_of(pass.begin(), pass.end(), isdigit)) { + return ErrorCode::PasswordNeedsAtLeastOneNumber; + } else if (std::all_of(pass.begin(), pass.end(), isalnum)) { + return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter; + } else if (!std::any_of(pass.begin(), pass.end(), isupper)) { + return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter; + } else { + return ErrorCode::Ok; + } +} + +ErrorCode checkPassword(std::string pass1, std::string pass2) { + if (doPasswordsMatch(pass1, pass2)) { + return checkPasswordRules(pass1); + } else { + return ErrorCode::PasswordsDoNotMatch; + } +} \ No newline at end of file diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 85160868..9abcfcbe 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 +// Don't forget the header guard - #pragma once + +#pragma once +#include + +enum class ErrorCode { + Ok, + PasswordNeedsAtLeastNineCharacters, + PasswordNeedsAtLeastOneNumber, + PasswordNeedsAtLeastOneSpecialCharacter, + PasswordNeedsAtLeastOneUppercaseLetter, + PasswordsDoNotMatch +}; + +std::string getErrorMessage(ErrorCode errorCode); +bool doPasswordsMatch(std::string pass1, std::string pass2); +ErrorCode checkPasswordRules(std::string pass); +ErrorCode checkPassword(std::string pass1, std::string pass2); \ No newline at end of file From 5f8030e02363103b910964da10d0592f19437547 Mon Sep 17 00:00:00 2001 From: MichalG315 Date: Sat, 19 Jul 2025 15:13:00 +0200 Subject: [PATCH 2/2] format update --- homework/password-check/validation.cpp | 1 - homework/password-check/validation.hpp | 3 --- 2 files changed, 4 deletions(-) diff --git a/homework/password-check/validation.cpp b/homework/password-check/validation.cpp index 732c7f90..ed9ef4dc 100644 --- a/homework/password-check/validation.cpp +++ b/homework/password-check/validation.cpp @@ -23,7 +23,6 @@ std::string getErrorMessage(ErrorCode errorCode) { } bool doPasswordsMatch(std::string pass1, std::string pass2) { - if (pass1.empty() && pass2.empty()) { return true; } diff --git a/homework/password-check/validation.hpp b/homework/password-check/validation.hpp index 9abcfcbe..e0bd8b69 100644 --- a/homework/password-check/validation.hpp +++ b/homework/password-check/validation.hpp @@ -1,6 +1,3 @@ -// TODO: I'm empty :) Put enum and function headers here. -// Don't forget the header guard - #pragma once - #pragma once #include