Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,60 @@
#include "validation.hpp"
// TODO: Put implementations here
#include <algorithm>
#include <cctype>
#include <string>

std::string getErrorMessage(const ErrorCode errorCode) {
std::string errorMessage{};
switch (errorCode) {
case ErrorCode::Ok:
errorMessage = "Ok";
break;
case ErrorCode::PasswordNeedsAtLeastNineCharacters:
errorMessage = "Password needs to have at least nine characters";
break;
case ErrorCode::PasswordNeedsAtLeastOneNumber:
errorMessage = "Password needs to have at least one number";
break;
case ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter:
errorMessage = "Password needs to have at least one special character";
break;
case ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter:
errorMessage = "Password needs to have at least one uppercase letter";
break;
case ErrorCode::PasswordsDoNotMatch:
errorMessage = "Passwords do not match";
break;
default:
errorMessage = "An unkown error occured";
break;
}
return errorMessage;
}

bool doPasswordsMatch(const std::string& password, const std::string& repeatedPassword) {
return password == repeatedPassword;
}

ErrorCode checkPasswordRules(const std::string& password) {
ErrorCode errorCode{ErrorCode::Ok};
if (password.size() < 9) {
errorCode = ErrorCode::PasswordNeedsAtLeastNineCharacters;
}
if (std::none_of(password.cbegin(), password.cend(), [](const char c) { return std::isdigit(c); })) {
errorCode = ErrorCode::PasswordNeedsAtLeastOneNumber;
}
if (std::none_of(password.cbegin(), password.cend(), [](const char c) { return std::ispunct(c); })) {
errorCode = ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
}
if (std::none_of(password.cbegin(), password.cend(), [](const char c) { return std::isupper(c); })) {
errorCode = ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter;
}
return errorCode;
}

ErrorCode checkPassword(const std::string& password, const std::string& repeatedPassowrd) {
if (!doPasswordsMatch(password, repeatedPassowrd)) {
return ErrorCode::PasswordsDoNotMatch;
}
return checkPasswordRules(password);
}
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(const ErrorCode errorCode);

bool doPasswordsMatch(const std::string& password, const std::string& repeatedPassword);

ErrorCode checkPasswordRules(const std::string& password);

ErrorCode checkPassword(const std::string& password, const std::string& repeatedPassowrd);