Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. public class Account
  2. {
  3.     public string UserName { get; private set; }
  4.     public string Password { get; private set; }
  5.  
  6.     public Account(string userName, string password)
  7.     {
  8.         // check to see if the userName or password contain a space. If so, it is an error as they
  9.         // are not allowed to have spaces.
  10.         if ( ValidateNewUserName(userName) || ValidateNewPassword(password) ){
  11.             throw new Exception("You are not allowed to have spaces in the user name or password.");
  12.         }
  13.        
  14.         // You already passed the validation -- no need for the else statement since this will only be run
  15.         // if no error is thrown :)
  16.         this.UserName = userName;
  17.         this.Password = password;
  18.     }
  19.  
  20.     private bool ValidateNewPassword(string password){
  21.         return password.Contains(" ");
  22.     }
  23.  
  24.     private bool ValidateNewUserName(string userName){
  25.         return userName.Contains(" ");
  26.     }
  27. }
  28.  
  29. // main method in some other class
  30. public static Main(){
  31.     try{
  32.         Account account = new Account ("someUserName", "somePassword");
  33.  
  34.         // instead of writing it out to the console, you would just open the
  35.         // filestream and shove it in there.
  36.         Console.Writeln("my user name : " + account.UserName);
  37.         Console.Writeln("my password  : " + account.Password);
  38.     }
  39.     catch(Exception e){
  40.         Console.Writeln("An error occurred. The information is as follows : ");
  41.         Console.Writeln("\tInnerException : " + e.InnerException);
  42.         Console.Writeln("\tMessage : " + e.Message);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement