Advertisement
social1986

Untitled

Feb 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. public class Book
  5. {
  6.     private string author;
  7.     private string title;
  8.     private decimal price;
  9.  
  10.     public Book(string author, string title, decimal price)
  11.     {
  12.         this.Author = author;
  13.         this.Title = title;
  14.         this.Price = price;
  15.     }
  16.  
  17.     public virtual decimal Price
  18.     {
  19.         get { return price; }
  20.         protected set
  21.         {
  22.             if (!ValidatePrice(value))
  23.             {
  24.                 throw new ArgumentException("Price not valid!");
  25.             }
  26.             this.price = value;
  27.         }
  28.     }
  29.  
  30.  
  31.     public string Author
  32.     {
  33.         get { return author; }
  34.         protected set
  35.         {
  36.             if (!ValidateAuthorName(value))
  37.             {
  38.                 throw new ArgumentException("Author not valid!");
  39.             }
  40.             this.author = value;
  41.         }
  42.     }
  43.  
  44.  
  45.     public string Title
  46.     {
  47.         get { return title; }
  48.         protected set
  49.         {
  50.             if (!ValidateTitle(value))
  51.             {
  52.                 throw new ArgumentException("Title not valid!");
  53.             }
  54.             this.title = value;
  55.         }
  56.     }
  57.  
  58.     protected bool ValidateTitle(string value)
  59.     {
  60.         return value.Length > 2;
  61.     }
  62.  
  63.     protected bool ValidateAuthorName(string value)
  64.     {
  65.         var authorName = value.Split();
  66.  
  67.         if (authorName.Length > 1 && char.IsDigit(authorName[1][0]))
  68.         {
  69.             return false;
  70.         }
  71.  
  72.         return true;
  73.     }
  74.  
  75.     protected bool ValidatePrice(decimal price)
  76.     {
  77.         return price > 0;
  78.     }
  79.  
  80.     public override string ToString()
  81.     {
  82.         var sb = new StringBuilder();
  83.         sb.AppendLine($"Type: {this.GetType().Name}");
  84.         sb.AppendLine($"Title: {this.Title}");
  85.         sb.AppendLine($"Author: {this.Author}");
  86.         sb.AppendLine($"Price: {this.Price:f2}");
  87.         return sb.ToString();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement