Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- public class Book
- {
- private string author;
- private string title;
- private decimal price;
- public Book(string author, string title, decimal price)
- {
- this.Author = author;
- this.Title = title;
- this.Price = price;
- }
- public virtual decimal Price
- {
- get { return price; }
- protected set
- {
- if (!ValidatePrice(value))
- {
- throw new ArgumentException("Price not valid!");
- }
- this.price = value;
- }
- }
- public string Author
- {
- get { return author; }
- protected set
- {
- if (!ValidateAuthorName(value))
- {
- throw new ArgumentException("Author not valid!");
- }
- this.author = value;
- }
- }
- public string Title
- {
- get { return title; }
- protected set
- {
- if (!ValidateTitle(value))
- {
- throw new ArgumentException("Title not valid!");
- }
- this.title = value;
- }
- }
- protected bool ValidateTitle(string value)
- {
- return value.Length > 2;
- }
- protected bool ValidateAuthorName(string value)
- {
- var authorName = value.Split();
- if (authorName.Length > 1 && char.IsDigit(authorName[1][0]))
- {
- return false;
- }
- return true;
- }
- protected bool ValidatePrice(decimal price)
- {
- return price > 0;
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- sb.AppendLine($"Type: {this.GetType().Name}");
- sb.AppendLine($"Title: {this.Title}");
- sb.AppendLine($"Author: {this.Author}");
- sb.AppendLine($"Price: {this.Price:f2}");
- return sb.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement