Advertisement
Guest User

Untitled

a guest
Jun 9th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. namespace ServerTest
  2. {
  3.     using System;
  4.     using System.Text;
  5.  
  6.     public class Book
  7.     {
  8.         private double price = 0;
  9.         private string title = null;
  10.         private string author = null;
  11.  
  12.         public Book(string title, string author, double price)
  13.         {
  14.             this.Title = title;
  15.             this.Author = author;
  16.             this.Price = price;
  17.         }
  18.  
  19.         public string Title
  20.         {
  21.             get
  22.             {
  23.                 return this.title;
  24.             }
  25.  
  26.             set
  27.             {
  28.                 // We check the value of the "value" keyword, because the
  29.                 // "value" keyword holds the new value
  30.                 if (string.IsNullOrEmpty(value))
  31.                 {
  32.                     throw new ArgumentNullException("The book musn`t have a title");
  33.                 }
  34.  
  35.                 this.title = value;
  36.             }
  37.         }
  38.  
  39.         public string Author
  40.         {
  41.             get
  42.             {
  43.                 return this.author;
  44.             }
  45.  
  46.             set
  47.             {
  48.                 // We check the value of the "value" keyword, because the
  49.                 // "value" keyword holds the new value
  50.                 if (string.IsNullOrEmpty(value))
  51.                 {
  52.                     throw new ArgumentNullException("The book musn`t have a author");
  53.                 }
  54.  
  55.                 this.author = value;
  56.             }
  57.         }
  58.  
  59.         public double Price
  60.         {
  61.             get
  62.             {
  63.                 return this.price;
  64.             }
  65.  
  66.             set
  67.             {
  68.                 if (value < 0)
  69.                 {
  70.                     throw new ArgumentOutOfRangeException("The price should be bigger than 0");
  71.                 }
  72.  
  73.                 // We check the value of the "value" keyword, because the
  74.                 // "value" keyword holds the new value
  75.                 this.price = value;
  76.             }
  77.         }
  78.  
  79.         // Virtual property :)
  80.         public virtual double ChangedPrice
  81.         {
  82.             get
  83.             {
  84.                 // In this class we return the original
  85.                 return this.Price;
  86.             }
  87.         }
  88.  
  89.         public override string ToString()
  90.         {
  91.             var output = new StringBuilder();
  92.             output.AppendFormat("-Type: {0}{1}", this.GetType().Name, Environment.NewLine);
  93.             output.AppendFormat("-Title: {0}{1}", this.Title, Environment.NewLine);
  94.             output.AppendFormat("-Author: {0}{1}", this.Author, Environment.NewLine);
  95.  
  96.             // We need the ChangedPrice value, not the original one
  97.             output.AppendFormat("-Price: {0:F2}{1}", this.ChangedPrice, Environment.NewLine);
  98.             return output.ToString();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement