in c # i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and 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. Next, 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. Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s price to the maximum of 10 cents per page. At the end of the program, display all the entered, and possibly corrected, records. I'm getting the errors Test CaseIncomplete Complete program walkthrough, test 1 Input Book1 Author1 10.99 200 Book2 Author2 19.99 100 Book3 Author2 25.00 600 Book4 Author3 5.00 20 Book5 Author4 8.00 120 Output Book1 by Author1 Price $10.99 200 pages. Book2 by Author2 Price $19.99 100 pages. Book3 by Author2 Price $25.00 600 pages. Book4 by Author3 Price $5.00 20 pages. Book5 by Author4 Price $8.00 my results for this are in the image attached and the error i am getting is Compilation failed: 4 error(s), 1 warnings NtTest8d6f4623.cs(5,1): warning CS0105: The using directive for `System' appeared previously in this namespace NtTest8d6f4623.cs(10,45): error CS1502: The best overloaded method match for `Book.Book(string, string, decimal, int)' has some invalid arguments BookExceptionDemo.cs(12,12): (Location of the symbol related to previous error) NtTest8d6f4623.cs(10,74): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal' NtTest8d6f4623.cs(15,36): error CS1502: The best overloaded method match for `Book.Book(string, string, decimal, int)' has some invalid arguments BookExceptionDemo.cs(12,12): (Location of the symbol related to previous error) NtTest8d6f4623.cs(15,65): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal' my code is using System; class Book { // private fields private string title; private string author; private decimal price; private int numPages; // constructor that takes in arguments and sets the fields public Book(string title, string author, decimal price, int numPages) { this.title = title; this.author = author; this.price = price; this.numPages = numPages; } // public properties that allow access to the private fields public string Title { get { return title; } set { title = value; } } public string Author { get { return author; } set { author = value; } } public decimal Price { // when setting the price, also calculate the price per page // and check if it's greater than a certain threshold get { return price; } set { decimal pricePerPage = value / numPages; if (pricePerPage > 0.1m) { // if the price per page is too high, throw a BookException throw new BookException(title, price, numPages); } price = value; } } public int NumPages { get { return numPages; } set { numPages = value; } } // override the ToString method to return a string representation of the Book object public override string ToString() { return title + " by " + author + " Price $" + price + " " + numPages + " pages."; } } // custom exception class that inherits from Exception class BookException : Exception { // constructor that takes in arguments and calls the base constructor with a formatted message public BookException(string title, decimal price, int numPages) : base("For " + title + ", ratio is invalid....Price is $" + price + " for " + numPages + " pages.") { } } class BookExceptionDemo { static void Main() { // create an array of Book objects Book[] books = new Book[5]; // prompt the user to enter information for each book and create a new Book object for (int i = 0; i < 5; i++) { string title = Console.ReadLine(); string author = Console.ReadLine(); decimal price = decimal.Parse(Console.ReadLine()); int numPages = int.Parse(Console.ReadLine()); books[i] = new Book(title, author, price, numPages); } // print out each Book object using the overridden ToString method foreach (Book book in books) { Console.WriteLine(book); } }
in c # i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and 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.
Next, 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.
Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s price to the maximum of 10 cents per page. At the end of the program, display all the entered, and possibly corrected, records.
I'm getting the errors
Test CaseIncomplete
Complete program walkthrough, test 1
Input
Book1
Author1
10.99
200
Book2
Author2
19.99
100
Book3
Author2
25.00
600
Book4
Author3
5.00
20
Book5
Author4
8.00
120
Output
Book1 by Author1 Price $10.99 200 pages.
Book2 by Author2 Price $19.99 100 pages.
Book3 by Author2 Price $25.00 600 pages.
Book4 by Author3 Price $5.00 20 pages.
Book5 by Author4 Price $8.00
my results for this are in the image attached and the error i am getting is
Compilation failed: 4 error(s), 1 warnings
NtTest8d6f4623.cs(5,1): warning CS0105: The using directive for `System' appeared previously in this namespace
NtTest8d6f4623.cs(10,45): error CS1502: The best overloaded method match for `Book.Book(string, string, decimal, int)' has some invalid arguments
BookExceptionDemo.cs(12,12): (Location of the symbol related to previous error)
NtTest8d6f4623.cs(10,74): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal'
NtTest8d6f4623.cs(15,36): error CS1502: The best overloaded method match for `Book.Book(string, string, decimal, int)' has some invalid arguments
BookExceptionDemo.cs(12,12): (Location of the symbol related to previous error)
NtTest8d6f4623.cs(15,65): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal'
my code is
using System;
class Book
{
// private fields
private string title;
private string author;
private decimal price;
private int numPages;
// constructor that takes in arguments and sets the fields
public Book(string title, string author, decimal price, int numPages)
{
this.title = title;
this.author = author;
this.price = price;
this.numPages = numPages;
}
// public properties that allow access to the private fields
public string Title
{
get { return title; }
set { title = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public decimal Price
{
// when setting the price, also calculate the price per page
// and check if it's greater than a certain threshold
get { return price; }
set
{
decimal pricePerPage = value / numPages;
if (pricePerPage > 0.1m)
{
// if the price per page is too high, throw a BookException
throw new BookException(title, price, numPages);
}
price = value;
}
}
public int NumPages
{
get { return numPages; }
set { numPages = value; }
}
// override the ToString method to return a string representation of the Book object
public override string ToString()
{
return title + " by " + author + " Price $" + price + " " + numPages + " pages.";
}
}
// custom exception class that inherits from Exception
class BookException : Exception
{
// constructor that takes in arguments and calls the base constructor with a formatted message
public BookException(string title, decimal price, int numPages)
: base("For " + title + ", ratio is invalid....Price is $" + price + " for " + numPages + " pages.")
{
}
}
class BookExceptionDemo
{
static void Main()
{
// create an array of Book objects
Book[] books = new Book[5];
// prompt the user to enter information for each book and create a new Book object
for (int i = 0; i < 5; i++)
{
string title = Console.ReadLine();
string author = Console.ReadLine();
decimal price = decimal.Parse(Console.ReadLine());
int numPages = int.Parse(Console.ReadLine());
books[i] = new Book(title, author, price, numPages);
}
// print out each Book object using the overridden ToString method
foreach (Book book in books)
{
Console.WriteLine(book);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images