Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _02.BookShop
- {
- public class Book
- {
- private string author;
- private string title;
- private double price;
- public Book(string author, string title, double price)
- {
- this.setAuthor(author);
- this.setTitle(title);
- this.setPrice(price);
- }
- public string Author
- {
- get { return this.author; }
- protected set
- {
- var names = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- if (names.Length > 1 && char.IsDigit(names[1][0]))
- {
- throw new ArgumentException("Author not valid!");
- }
- this.author = value;
- }
- }
- public string Title
- {
- get { return this.title; }
- protected set
- {
- if (value.Length < 3)
- {
- throw new ArgumentException("Title not valid!");
- }
- this.title = value;
- }
- }
- public virtual double Price
- {
- get { return this.price; }
- protected set
- {
- if (value <= 0 )
- {
- throw new AggregateException("Price not valid!");
- }
- this.price = value;
- }
- }
- private void setPrice(double price)
- {
- this.Price = price;
- }
- private void setTitle(string title)
- {
- this.Title = title;
- }
- private void setAuthor(string author)
- {
- this.Author = author;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("Type: ").Append(this.GetType().Name)
- .Append(Environment.NewLine)
- .Append("Title: ").Append(this.Title)
- .Append(Environment.NewLine)
- .Append("Author: ").Append(this.Author)
- .Append(Environment.NewLine)
- .Append("Price: ").Append($"{ this.Price:F1}")
- .Append(Environment.NewLine);
- return sb.ToString();
- }
- }
- public class GoldenEditionBook : Book
- {
- public GoldenEditionBook(string author, string title, double price)
- : base(author, title, price)
- {
- }
- public override double Price
- {
- get
- {
- return base.Price * 1.3;
- }
- }
- }
- public class Startup
- {
- public static void Main()
- {
- try
- {
- string author = Console.ReadLine();
- string title = Console.ReadLine();
- double price = double.Parse(Console.ReadLine());
- Book book = new Book(author, title, price);
- GoldenEditionBook goldenEditionBook = new GoldenEditionBook(author, title, price);
- Console.WriteLine(book);
- Console.WriteLine(goldenEditionBook);
- }
- catch (ArgumentException ae)
- {
- Console.WriteLine(ae.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement