The Peterman Publishing 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-pages ratio. For example an error message might be:

For Goodnight Moon,raio is incalid
...Price is $12.99 for 25 pages

Create a Book class that contains fields for title, author, price , and number of pager. Include properties for each filds. 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 were the ration is acceptable and the others where it is not. Catch any thrown exceptions and display the BookException Message.

OK...yeh thats not a bad assignment...

You need to make a start yourself though...

What code have you got so far?

namespace Book
{
    class Program
    {
        static void Main(string[] args)
        {string InputPages ;
        string InputPrice ;

        Book book1 = new Book();
        Console.Write("Please enter the book's title : ");
        book1.Title = Console.ReadLine();

        Console.Write("Please enter the book's author : ");
        book1.Author = Console.ReadLine();

        Console.Write("Please enter the number of pages in the book : ");
        InputPages = Console.ReadLine();
        book1.Pages = Convert.ToInt32(InputPages);
        book1.MaxPrice = book1.Pages * 0.10;
        
        while ()
        {
            try
            {
                Console.Write("Please enter the book's price :  ");
                InputPrice = Console.ReadLine();
                book1.Price = Convert.ToDouble(InputPrice);

            }
            catch (BookException be )
            {
                Console.Write ( be.Message);
                Console.WriteLine("For {0} the maximum price allowed for a {1} page is {2}$", book1.Title, book1.Pages, book1.Pages * 0.10);
            }
        }
        Console.WriteLine("The title of Book 1 is {0} by {1} it contains {2} pages and is priced at {3}$.", book1.Title, book1.Author, book1.Pages, book1.Price);
        Console.ReadLine();

        
    }
    }
}




//class

class BookExceptions
    {
        private string title;
        private string author;
        private int pages;
        private double price;
        private double maxPrice;

        public string Title { get; set; }
        public string Author { get; set; }
        public int Pages { get; set; }
        public double Price { get; set; }
        public double MaxPrice { get; set; }

    }

    public class BookException : Exception
    {

        private static string msg = "The ratio is invalid.";
        public BookException()
            : base(msg)
        {

        }

    }
}

OK my understanding of this is that the BookException class needs to look like this

public class BookException : Exception
{
//BookException class

    private string _title { get; set; }
    private int _pages { get; set; }
    private double _price { get; set; }

    //Message that get returned
    public override string Message
    {
        get
        {
            StringBuilder Error =  new StringBuilder(); 
            Error.Append("For " + _title + ",ratio is invalid./n");
            Error.Append("Price is " + _price + " for " + _pages + " pages");
            return Error.ToString();
        }
    }

    //constructor requires three arguments: a string Book title, a double  price and an int number of pages.
    public BookException(string Title, double Price, int Pages)
    {
        _title = Title;
        _pages = Pages;
        _price = Price;
    }
}

Then to use the BookException in your application

try
{
    PricePerPage = Price / pages;
    if (PricePerPage > 10)
    {
        throw new BookException(Title, Price, Pages); //Throws the BookException
    }
}
catch (Exception ex)
{
    Console.Write(ex.message);
}

This will work, don't know it this is what you needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.