title: Inheritance and Exception Handling
body: <p>I am trying to complete a homework assignment however I am not sure what to do next. I have been struggling with Inheritance and Exception Handling...this problem combines the two.
  Could you please give me a shove in the right direction?</p>
  
  <p>Here is the question:
  Create a program named BookExceptionDemo for the Peterman Publishing Company. The company has decided that no published book should cost more than 10 cents per page. Create a BookException class whose constructor requires three arguments: a string Book title, a double price, and an int number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a book does not meet the price-to-page ratio. For example, an error message might be:</p>
  
  <p>For Goodnight Moon, ratio is invalid.
  ...Price is $12.99 for 25 pages.</p>
  
  <p>Create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. throw a BookException if a client program tries to construct a Book object for which the price is more than 10 cents per page. Create a program that creates at least four Book objects - some where the ratio is acceptable and others where it is not. Catch any thrown exceptions and display the BookException Message.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>namespace BookExceptionDemo
  {
      class BookException : Exception
      {
          public BookException(string title, double price, int numberPages)
               : base("For " + title + ", ratio is invalid. \nPrice is $" + price + " for " + numberPages + " pages.") //p.470
          {
          }
      }
  
      class Book
      {
          public Book(string title, double price, int numberPages)  //he said this should check to see if the ratio is correct, if not, then throw exception.
          { 
          }
  
          public string Title { get; private set; }
  
          public double Price { get; private set; }
  
          public int NumberPages { get; private set; }
      }
  
      // my teacher put this here
      // but at the end of class he said the contents should be moved somewhere else?
      // Does it go in the BookException constructor? 
      class Program
      {
          static void Main(string[] args)
          {
  
              try
              {
                  throw (new BookException("Goodnight Moon", 12.99, 25));
              }
              catch (BookException e)
              {
                  Console.WriteLine(e.Message);
              }
  
              Console.Read();
         }
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  My school switched programming languages this semester, so I came into this intermediate C# course with no prior knowledge of the language. I understood most things up until the past few class sessions. I was getting frustrated in my confusion and was feeling disheartened, but now I can honestly say that I am excited to have a better understanding of these concepts. Programming is really fun and I really appreciate people like you who are willing to help others. Thank you all for the time and effort you have given in assisting me.</p>
  
  <p>Here is what I believe to be the correct answer to the question I posted.        </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace BookExceptionDemo
  {
      class BookException : Exception
      {
          public BookException(string title, double price, int numberPages)
              : base("For " + title + ", ratio is invalid. \nPrice is $" + price + " for " + numberPages + " pages.")
          {
  
          }
      }
  
      class Book
      {
          public Book(string title, string author, double price, int numberPages)
          {
              if ((price / numberPages) &gt; .1)
              {
                  throw new BookException(title, price, numberPages);
              }
          }
  
          private string title;
          private string author;
          private double price;
          private int numberPages;
  
  
          public string Title
          {
              get
              {
                  return title;
              }
              set 
              {
                  title = value;
              }
          }
  
          public string Author
          {
              get
              {
                  return author;
              }
              set
              {
                  author = value;
              }
          }
  
          public double Price
          {
              get
              {
                  return price;
              }
              set
              {
                  price = value;
              }
          }
  
          public int NumberPages
          {
              get
              {
                  return numberPages;
              }
              set
              {
                  numberPages = value;
              }
          }
  
      }
  
      class Program 
      {
          static void Main(string[] args)
          {
              try
              {
                  Book junglebook = new Book("Jungle Book","Kipling", 25.99, 50);   //should throw exception
              }
              catch (BookException ex)
              {
              Console.WriteLine(ex.Message);
              }
  
              try
              {
                  Book hobbit = new Book("The Hobbit","Tolkien", 2.99, 30);   //should NOT throw exception
              }
              catch (BookException ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              try
              {
                  Book book1984 = new Book("1984","Orwell", 31.32, 15);   //should throw exception
              }
              catch (BookException ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              try
              {
                  Book guidetonetworks = new Book("Guide to Networks","Somebody", 17.56, 500);   //should NOT throw exception
              }
              catch (BookException ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              Console.Read();
          }
      }
  }
  </code></pre>