Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding works #1398

Closed
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
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;
}
18 changes: 16 additions & 2 deletions homework/password-check/validation.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
// 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);
Loading