Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace AP_Question5_BManke
- {
- class UserAccount
- {
- private string mUsername;
- private string mPassword;
- public string Username
- {
- get { return mUsername; }
- set
- {
- if (ValidateUsername(value))
- {
- mUsername = value;
- }
- else
- {
- throw new Exception("The username must be 3-15 characters long, and contain only alphanumeric characters (letters A-Z, numbers 0-9) and the underscore character(_).");
- }
- }
- }
- public string Password
- {
- get { return mPassword; }
- set
- {
- if (ValidatePassword(value))
- {
- mPassword = value;
- }
- else
- {
- throw new Exception("The password must be 8-80 characters long with no spaces, contain at least one upper case and one lower case character, " +
- "contain a number ranging from 0-9, and have at least one special character (!@#$%^*_=+.?-)");
- }
- }
- }
- public UserAccount()
- {
- mUsername = "user1";
- mPassword = "P@ssword1";
- }
- public UserAccount(string username, string password)
- {
- Username = username;
- Password = password;
- }
- public bool HasSpecialChars(string username)
- {
- return username.Contains("_");
- }
- static bool ValidatePassword(string password)
- {
- const int MIN_LENGTH = 8;
- const int MAX_LENGTH = 80;
- if (password == null) throw new ArgumentNullException();
- bool meetsLengthRequirements = password.Length >= MIN_LENGTH && password.Length <= MAX_LENGTH;
- bool hasUpperCaseLetter = false;
- bool hasLowerCaseLetter = false;
- bool hasDecimalDigit = false;
- if (meetsLengthRequirements)
- {
- foreach (char c in password)
- {
- if (char.IsUpper(c)) hasUpperCaseLetter = true;
- else if (char.IsLower(c)) hasLowerCaseLetter = true;
- else if (char.IsDigit(c)) hasDecimalDigit = true;
- }
- }
- bool isValid = meetsLengthRequirements
- && hasUpperCaseLetter
- && hasLowerCaseLetter
- && hasDecimalDigit
- ;
- return isValid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement