SHARE
TWEET

Author Validation

Vdzhambazova Jul 4th, 2016 268 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Book
  2. {
  3.     private string author;
  4.     private string title;
  5.     private double price;
  6.  
  7.     public Book(string author, string title, double price)
  8.     {
  9.         this.Author = author;
  10.         this.Title = title;
  11.         this.Price = price;
  12.     }
  13.  
  14.     public string Author
  15.     {
  16.         get { return this.author; }
  17.         set
  18.         {
  19.             int num = 0;
  20.             if (CheckIfAuthorHasSurname(value))
  21.             {
  22.                 if (int.TryParse(GetAuthorSurname(value).Substring(0, 1), out num) || string.IsNullOrWhiteSpace(value))
  23.                 {
  24.                     throw new ArgumentException("Author not valid!");
  25.                 }
  26.  
  27.                 this.author = value;
  28.             }
  29.         }
  30.     }
  31.  
  32.     private bool CheckIfAuthorHasSurname(string value)
  33.     {
  34.         string[] name = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  35.         return name.Length == 2;
  36.     }
  37.  
  38.     private string GetAuthorSurname(string value)
  39.     {
  40.  
  41.         string[] nameArgs = value.Split();
  42.         return nameArgs[1];
  43.     }
  44.  
  45.     public string Title
  46.     {
  47.         get { return this.title; }
  48.         set
  49.         {
  50.             if (value.Length < 3)
  51.             {
  52.                 throw new ArgumentException("Title not valid!");
  53.             }
  54.             this.title = value;
  55.         }
  56.     }
  57.  
  58.     public virtual double Price
  59.     {
  60.         get { return this.price; }
  61.         set
  62.         {
  63.             if (value <= 0)
  64.             {
  65.                 throw new ArgumentException("Price not valid!");
  66.             }
  67.  
  68.             this.price = value;
  69.         }
  70.     }
  71.  
  72.     public override string ToString()
  73.     {
  74.         StringBuilder sb = new StringBuilder();
  75.         sb.Append("Type: ").Append(this.GetType().Name)
  76.                 .Append(Environment.NewLine)
  77.                 .Append("Title: ").Append(this.Title)
  78.                 .Append(Environment.NewLine)
  79.                 .Append("Author: ").Append(this.Author)
  80.                 .Append(Environment.NewLine)
  81.                 .Append("Price: ").Append($"{this.Price:F1}")
  82.                 .Append(Environment.NewLine);
  83.  
  84.         return sb.ToString();
  85.     }
  86.  
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top