Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ServerTest
- {
- using System;
- using System.Text;
- public class Book
- {
- private double price = 0;
- private string title = null;
- private string author = null;
- public Book(string title, string author, double price)
- {
- this.Title = title;
- this.Author = author;
- this.Price = price;
- }
- public string Title
- {
- get
- {
- return this.title;
- }
- set
- {
- // We check the value of the "value" keyword, because the
- // "value" keyword holds the new value
- if (string.IsNullOrEmpty(value))
- {
- throw new ArgumentNullException("The book musn`t have a title");
- }
- this.title = value;
- }
- }
- public string Author
- {
- get
- {
- return this.author;
- }
- set
- {
- // We check the value of the "value" keyword, because the
- // "value" keyword holds the new value
- if (string.IsNullOrEmpty(value))
- {
- throw new ArgumentNullException("The book musn`t have a author");
- }
- this.author = value;
- }
- }
- public double Price
- {
- get
- {
- return this.price;
- }
- set
- {
- if (value < 0)
- {
- throw new ArgumentOutOfRangeException("The price should be bigger than 0");
- }
- // We check the value of the "value" keyword, because the
- // "value" keyword holds the new value
- this.price = value;
- }
- }
- // Virtual property :)
- public virtual double ChangedPrice
- {
- get
- {
- // In this class we return the original
- return this.Price;
- }
- }
- public override string ToString()
- {
- var output = new StringBuilder();
- output.AppendFormat("-Type: {0}{1}", this.GetType().Name, Environment.NewLine);
- output.AppendFormat("-Title: {0}{1}", this.Title, Environment.NewLine);
- output.AppendFormat("-Author: {0}{1}", this.Author, Environment.NewLine);
- // We need the ChangedPrice value, not the original one
- output.AppendFormat("-Price: {0:F2}{1}", this.ChangedPrice, Environment.NewLine);
- return output.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement