Question: Using the C# code below, please respond with a solution to part c of the original problem: c. Using the Book class, write an application
Using the C# code below, please respond with a solution to part c of the original problem:
c. Using the Book class, write an application named BookExceptionDemo2 that creates 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.
Thank you :-)
SourceCode:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace BookExceptionDemo { class Program { static void Main() { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; Console.Clear(); Book book_obj = new Book(); const double PRICE_PER_PAGE = 0.10; Console.Write("Please enter Book Title : "); book_obj.Title = Console.ReadLine(); Console.Write("Please enter Book Title : "); book_obj.Author = Console.ReadLine(); Console.Write("Please give number of pages in the book : "); book_obj.Pages = Convert.ToInt32(Console.ReadLine()); book_obj.MaxPrice = book_obj.Pages * PRICE_PER_PAGE; try { Console.Write("Please provide book price : "); book_obj.Price = Convert.ToDouble(Console.ReadLine()); } catch (BookException be) { Console.Write (be.Message); Console.WriteLine("For {0} the maximum price allowed for a {1} page is {2}$", book_obj.Title, book_obj.Pages, book_obj.Pages * PRICE_PER_PAGE); } Console.WriteLine("The title of Book 1 is {0} by {1} it contains {2} pages and is priced at {3}$.", book_obj.Title, book_obj.Author, book_obj.Pages, book_obj.Price.ToString("C")); Console.ReadLine(); } } }
class Book { 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 { public BookException(string title, double price, int pages) : base("For" + title + ", ratio is invalid. Price is" + price.ToString("C") + "for" + pages + "pages") { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
