Hello,
Code below has requirements commented in. I don't get any errors, but when I go to run it, I immediately get a stack overflow and program closes. I don't even know where to begin to look. I am too new at this. Without a road map to errors, I am lost Thanks.
namespace Assignment_7a_Exception
{
public class Assignment_7a_Exception
{
public static void Main()
{
int x = 0;
/* 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. */
for (x=0; x < 4; ++x)
{
Book mybook = new Book();
Console.WriteLine("Please enter book title: ");
mybook.Title = Console.ReadLine();
Console.WriteLine("Please enter name of the author: ");
mybook.Author = Console.ReadLine();
Console.WriteLine("Please enter the price of the book: ");
mybook.Price = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the number of pages in the book: ");
mybook.NumPages = Convert.ToInt32(Console.ReadLine());
try
{
mybook.checkRatio();
} // End
// Catch any thrown exceptions and display BookException Message.
catch(BookException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
} // End for
} // End main
} // End Assignment class
/* Create a Book class that has the following fields:
* string title, string author, double price and int number of pages. */
public class Book
{
// Create properties (accessors) for each field.
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
private string author;
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
private double price;
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
private int numPages;
public int NumPages
{
get
{
return numPages;
}
set
{
numPages = value;
}
}
public bool validRatio { get; set; }
public Book()
: this("zzz", "zzz", 0.0, 9)
{
}
public Book(string Title, string Author, double Price, int NumPages)
{
}
Book mybook = new Book();
public void checkRatio()
{
const double RATE = .10;
// The price-to-pages ratio is determined by checking the following: price > RATE * pages where RATE = 0.10
if (Price > (RATE * NumPages))
{
mybook.validRatio = true;
}
// Throw a BookException if a book object is created that has a price that is more than 10 cents per page
else
{
mybook.validRatio = false;
BookException be = new BookException();
/* 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. */
Console.WriteLine("Ratio for {0} is invalid. Price is {1} for {2} pages.", Title, Price, NumPages);
throw(be);
}
}
} //End Book Class
/*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. */
public class BookException : Exception
{
public BookException(string Title, double Price, double NumPages)
: base ()
{
}
public BookException()
{
}
} //End BookException Class
// Internal Documentation.