HristoGrigorov

Bookshop

Apr 22nd, 2017
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _02.BookShop
  8. {
  9. public class Book
  10. {
  11. private string author;
  12. private string title;
  13. private double price;
  14.  
  15. public Book(string author, string title, double price)
  16. {
  17. this.setAuthor(author);
  18. this.setTitle(title);
  19. this.setPrice(price);
  20. }
  21.  
  22. public string Author
  23. {
  24. get { return this.author; }
  25. protected set
  26. {
  27. var names = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  28.  
  29. if (names.Length > 1 && char.IsDigit(names[1][0]))
  30. {
  31. throw new ArgumentException("Author not valid!");
  32. }
  33. this.author = value;
  34. }
  35. }
  36.  
  37. public string Title
  38. {
  39. get { return this.title; }
  40. protected set
  41. {
  42. if (value.Length < 3)
  43. {
  44. throw new ArgumentException("Title not valid!");
  45. }
  46.  
  47. this.title = value;
  48. }
  49. }
  50.  
  51. public virtual double Price
  52. {
  53. get { return this.price; }
  54. protected set
  55. {
  56.  
  57. if (value <= 0 )
  58. {
  59. throw new AggregateException("Price not valid!");
  60. }
  61. this.price = value;
  62. }
  63. }
  64.  
  65. private void setPrice(double price)
  66. {
  67. this.Price = price;
  68. }
  69.  
  70. private void setTitle(string title)
  71. {
  72. this.Title = title;
  73. }
  74.  
  75. private void setAuthor(string author)
  76. {
  77. this.Author = author;
  78. }
  79.  
  80. public override string ToString()
  81. {
  82. StringBuilder sb = new StringBuilder();
  83.  
  84. sb.Append("Type: ").Append(this.GetType().Name)
  85. .Append(Environment.NewLine)
  86. .Append("Title: ").Append(this.Title)
  87. .Append(Environment.NewLine)
  88. .Append("Author: ").Append(this.Author)
  89. .Append(Environment.NewLine)
  90. .Append("Price: ").Append($"{ this.Price:F1}")
  91. .Append(Environment.NewLine);
  92.  
  93. return sb.ToString();
  94. }
  95. }
  96.  
  97. public class GoldenEditionBook : Book
  98. {
  99. public GoldenEditionBook(string author, string title, double price)
  100. : base(author, title, price)
  101. {
  102. }
  103.  
  104. public override double Price
  105. {
  106. get
  107. {
  108. return base.Price * 1.3;
  109. }
  110. }
  111. }
  112. public class Startup
  113. {
  114. public static void Main()
  115. {
  116. try
  117. {
  118. string author = Console.ReadLine();
  119. string title = Console.ReadLine();
  120. double price = double.Parse(Console.ReadLine());
  121.  
  122. Book book = new Book(author, title, price);
  123. GoldenEditionBook goldenEditionBook = new GoldenEditionBook(author, title, price);
  124.  
  125. Console.WriteLine(book);
  126. Console.WriteLine(goldenEditionBook);
  127. }
  128. catch (ArgumentException ae)
  129. {
  130. Console.WriteLine(ae.Message);
  131. }
  132.  
  133. }
  134. }
  135. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×