In: Computer Science
C#:
A book publisher has limited the cost of every book they publish to no more than 10 cents per page.
Create a BookException class with a constructor that requires three (3) arguments for each book: a string 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. The error message might be:
For Goodnight Moon, ratio is invalid.
...Price is $12.99 for 25 pages
The price-to-pages ratio is determined by checking the following: price > RATE * pages where RATE = 0.10
Create a Book class that has the following fields: string title, string author, double price and int number of pages.
Create properties (accessors) for each field.
Throw a BookException if a book object is created that has a price that is more than 10 cents per page.
In Main create 4 Book objects and use the constructor for Book to pass the four inputs when each object is instantiated, some where the ratio is correct and some where the ratio is not correct.
Catch any thrown exceptions and display BookException Message.
Internal Documentation.
Possible output:
The inputs passed were:
("Goodnight Moon", "Brown", 12.99, 25)
("World History", "Stein", 72.99, 900)
("The Grapes of Math", "Stoltz", 30.99, 300)
("Steal This Book", "Hoffman", 72.99, 800)
The results were:
For Goodnight Moon, ratio is invalid.
...Price is $12.99 for 25 pages.
For The Grapes of Math, ratio is invalid.
...Price is $30.99 for 300 pages.
Press any key to continue . . .
Working code implemented in C# and appropriate comments provided for better understanding:
Source code for BookException.cs:
using System;
class BookExceptionDemo
{
static void Main()
{
try
{
Book book1 = new Book("Goodnight Moon", "Brown",
12.99, 25);
}
catch (BookException e)//except thrown in Book-class-objects are
caught here(in client-class)
{
Console.WriteLine(e.Message);
}
try
{
Book book2 = new Book("World History",
"Stein", 72.99, 900);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
try
{
Book book3 = new Book("The Grapes of Math", "Stoltz",
30.99, 300);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
try
{
Book book4 = new Book("Steal This Book",
"Hoffman", 72.99, 800);
}
catch (BookException e)
{
Console.WriteLine(e.Message);
}
}
}
class Book
{
private string title; //since property is NOT auto-implemented, so
we need to declare these fields too.
private string author;
private double price;
private int pages;
const double RATE = 0.10;
public Book(string title, string author, double price,
int pages)
{
Title = title;
Author = author;
if (price > RATE * pages)
throw (new BookException(title, price, pages)); //no "catch" in
this class. excep is caught in the client-class, which must have
try-catch block.
Price = price;
Pages = pages;
}
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 Pages
{
get
{
return pages;
}
set
{
pages = value;
}
}
}
public class BookException : Exception
{
public BookException(string title, double price, int pages) :
base("For " + title + ", ratio is invalid.\n...Price is " +
price.ToString("C") + " for " + pages + " pages.")
{
}
}
Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.