Advertisement
Guest User

BookShop

a guest
Jul 5th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace BookShop
  5. {
  6.     public class BookShopMain
  7.     {
  8.         public static void Main()
  9.         {
  10.             try
  11.             {
  12.                 string author = Console.ReadLine();
  13.                 string title = Console.ReadLine();
  14.                 double price = double.Parse(Console.ReadLine());
  15.  
  16.                 Book book = new Book(author, title, price);
  17.                 GoldenEditionBook goldenEditionBook = new GoldenEditionBook(author, title, price);
  18.  
  19.                 Console.WriteLine(book);
  20.                 Console.WriteLine(goldenEditionBook);
  21.             }
  22.             catch (ArgumentException ae)
  23.             {
  24.                 Console.WriteLine(ae.Message);
  25.             }
  26.         }
  27.     }
  28.  
  29.     public interface IBook
  30.     {
  31.         string Author { get; }
  32.         string Title { get; }
  33.         double Price { get; }
  34.     }
  35.  
  36.     public class Book : IBook
  37.     {
  38.         private string title;
  39.         private string author;
  40.         private double price;
  41.  
  42.         public Book(string author, string title, double price)
  43.         {
  44.             this.Author = author;
  45.             this.Title = title;
  46.             this.Price = price;
  47.         }
  48.  
  49.  
  50.         public string Author
  51.         {
  52.             get
  53.             {
  54.                 return this.author;
  55.             }
  56.  
  57.             protected set
  58.             {
  59.                 if (!ValidAuthorName(value))
  60.                 {
  61.                     throw new ArgumentException("Author not valid!");
  62.                 }
  63.  
  64.                 this.author = value;
  65.             }
  66.         }
  67.  
  68.         public string Title
  69.         {
  70.             get
  71.             {
  72.                 return this.title;
  73.             }
  74.  
  75.             protected set
  76.             {
  77.                 if (!ValidTitle(value))
  78.                 {
  79.                     throw new ArgumentException("Title not valid!");
  80.                 }
  81.  
  82.                 this.title = value;
  83.             }
  84.         }
  85.  
  86.         public virtual double Price
  87.         {
  88.             get
  89.             {
  90.                 return this.price;
  91.             }
  92.  
  93.             protected set
  94.             {
  95.                 if (value <= 0)
  96.                 {
  97.                     throw new ArgumentException("Price not valid!");
  98.                 }
  99.  
  100.                 this.price = value;
  101.             }
  102.         }
  103.  
  104.  
  105.         private bool ValidTitle(string bookTitle)
  106.         {
  107.             if (bookTitle.Length >= 3)
  108.             {
  109.                 return true;
  110.             }
  111.  
  112.             return false;
  113.         }
  114.  
  115.         private bool ValidAuthorName(string authorName)
  116.         {
  117.             var fullName = authorName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  118.  
  119.             if (fullName.Length < 2)
  120.             {
  121.                 return false;
  122.             }
  123.  
  124.             string secondName = fullName[1];
  125.  
  126.             int digit;
  127.             if (int.TryParse(secondName[0].ToString(), out digit))
  128.             {
  129.                 return false;
  130.             }
  131.  
  132.             return true;
  133.         }
  134.  
  135.         public override string ToString()
  136.         {
  137.             string priceOut = string.Format("{0:F1}", this.Price);
  138.  
  139.             StringBuilder stringBuilder = new StringBuilder();
  140.             stringBuilder.Append("Type: ").Append(this.GetType().Name)
  141.                     .Append(Environment.NewLine)
  142.                     .Append("Title: ").Append(this.Title)
  143.                     .Append(Environment.NewLine)
  144.                     .Append("Author: ").Append(this.Author)
  145.                     .Append(Environment.NewLine)
  146.                     .Append("Price: ").Append(priceOut)
  147.                     .Append(Environment.NewLine);
  148.  
  149.             return stringBuilder.ToString();
  150.         }
  151.     }
  152.  
  153.     public class GoldenEditionBook : Book
  154.     {
  155.         private const double GoldenEditionPriceModifier = 1.3;
  156.  
  157.         public GoldenEditionBook(string author, string title, double price)
  158.             : base(author, title, price)
  159.         {
  160.         }
  161.  
  162.         public override double Price
  163.         {
  164.             get
  165.             {
  166.                 return base.Price * GoldenEditionPriceModifier;
  167.             }
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement