o 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, pri

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter11: Exception Handling
Section: Chapter Questions
Problem 5E
icon
Related questions
Question

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.

my errors are

Test Case

Incomplete
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 120 pages.

Results
For Book2, ratio is invalid....Price is $19.99 for 100 pages.For Book4, ratio is invalid....Price is $5.00 for 20 pages.Book1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $10.00 100 pages.Book3 by Author2 Price $25.00 600 pages.Book4 by Author3 Price $2.00 20 pages.Book5 by Author4 Price $8.00 120 pages.

and

Unit TestIncomplete
BookException test

Build Status
Build Failed
Build Output
Compilation failed: 4 error(s), 1 warnings

NtTest1bbd64fe.cs(5,1): warning CS0105: The using directive for `System' appeared previously in this namespace
NtTest1bbd64fe.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)
NtTest1bbd64fe.cs(10,74): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal'
NtTest1bbd64fe.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)
NtTest1bbd64fe.cs(15,65): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal'
Test Contents
[TestFixture]
public class TestBookException {
     [Test]
     public void FirstBookExceptionTest() {
         Assert.Throws<BookException>(() => new Book("Book1", "Author1", 5.00, 25));
    }

    [Test]
    public void SecondExceptionTest() {
         Assert.DoesNotThrow(() => new Book("Book2", "Author2", 5.00, 100));
    }
}

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);
        }
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

im stil getting the erros 

 

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 120 pages.

results for this are in the image

my other error is 

Unit TestIncomplete
BookException test

Build Status
Build Failed
Build Output
Compilation failed: 4 error(s), 1 warnings

NtTeste181f334.cs(5,1): warning CS0105: The using directive for `System' appeared previously in this namespace
NtTeste181f334.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)
NtTeste181f334.cs(10,74): error CS1503: Argument `#3' cannot convert `double' expression to type `decimal'
NtTeste181f334.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)
NtTeste181f334.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);
        }
    }
}

 

Results Ⓡ
For Book2, ratio is invalid.
….Price is $19.99 for 100 pages.
For Book4, ratio is invalid.
...Price is $5.00 for 20 pages.
Show Details
es.
es.
es.
s.
Transcribed Image Text:Results Ⓡ For Book2, ratio is invalid. ….Price is $19.99 for 100 pages. For Book4, ratio is invalid. ...Price is $5.00 for 20 pages. Show Details es. es. es. s.
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning