Skip to content

Commit

Permalink
adding works
Browse files Browse the repository at this point in the history
  • Loading branch information
TodorokiBoy committed Nov 23, 2024
1 parent 299b2fc commit 373eca7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions homework/password-check/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <string>
#include <cctype>
#include "validation.hpp"

int main() {
Expand Down
53 changes: 52 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
#include "validation.hpp"
// TODO: Put implementations here
#include <algorithm>
#include <cctype>

std::string getErrorMessage(ErrorCode error) {
switch (error) {
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";
}
return "Undefined error";
}

bool doPasswordsMatch(std::string password1, std::string password2) {
return password1 == password2;
}

ErrorCode checkPasswordRules(std::string password) {
if (password.length() < 9) {
return ErrorCode::PasswordNeedsAtLeastNineCharacters;
}
if (!std::any_of(password.begin(), password.end(), [](unsigned char c) { return std::isdigit(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneNumber;
}
if (!std::any_of(password.begin(), password.end(), [](unsigned char c) { return std::ispunct(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
}
if (!std::any_of(password.begin(), password.end(), [](unsigned char c) { return std::isupper(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter;
}
return ErrorCode::Ok;
}

ErrorCode checkPassword(std::string password1, std::string password2) {
if (doPasswordsMatch(password1, password2)) {
return checkPasswordRules(password1);
}
return ErrorCode::PasswordsDoNotMatch;
}
21 changes: 19 additions & 2 deletions homework/password-check/validation.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
// TODO: I'm empty :) Put enum and function headers here.
// Don't forget the header guard - #pragma once
#pragma once
#include <string>

enum class ErrorCode {
Ok,
PasswordNeedsAtLeastNineCharacters,
PasswordNeedsAtLeastOneNumber,
PasswordNeedsAtLeastOneSpecialCharacter,
PasswordNeedsAtLeastOneUppercaseLetter,
PasswordsDoNotMatch
};

std::string getErrorMessage(ErrorCode error);

bool doPasswordsMatch(std::string password1, std::string password2);

ErrorCode checkPasswordRules(std::string password);

ErrorCode checkPassword(std::string password1, std::string password2);

0 comments on commit 373eca7

Please sign in to comment.