Stack Overflow Homework Problem

May 3 2010 10:55 AM
Hello

I am supposed to make a program to create book objects for an assignment, and I can't figure out what is wrong with my code. I don't really understand overriding that well, and didn't override "GetHashCode()", but the error I am getting is about a stack overflow exception on the "Price" overrides in the "text" and "coffee table book" classes. Can someone help this shitty programmer to not fail another semester?

Edit: The BookDemo class is just because my teacher is too lazy to make her own test programs. You can probably ignore it (unless it somehow turns out that the problem is actually in there).

using System;
public class BookDemo
{
    public static void Main()
    {
        Book braveNewWorld = new Book();
        TextBook computerScience = new TextBook();
        CoffeeTableBook ofMiceAndMen = new CoffeeTableBook();
        braveNewWorld.ISBN = "0-06-092987-1";
        braveNewWorld.Title = "Brave New World";
        braveNewWorld.Author = "Huxley, Aldous";
        braveNewWorld.Price = 5.99;
        computerScience.ISBN = "1-4239-0255-6";
        computerScience.Title = "Microsoft Visual C# 2008";
        computerScience.Author = "Farrell, Joyce";
        computerScience.Price = 85.17;
        computerScience.GradeLevel = 14;
        ofMiceAndMen.ISBN = "0-14-018642-5";
        ofMiceAndMen.Title = "Of Mice and Men";
        ofMiceAndMen.Author = "Steinbeck, John";
        ofMiceAndMen.Price = 9.00;

        Console.WriteLine(braveNewWorld.GetType() + " " + braveNewWorld.Title + " by: " + braveNewWorld.Author + " ISBN = " + braveNewWorld.ISBN + " Price: " + braveNewWorld.Price);
        Console.WriteLine(computerScience.GetType() + " " + computerScience.Title + " by: " + computerScience.Author + " ISBN = " + computerScience.ISBN + " Price: " + computerScience.Price + " Grade Level = " + computerScience.GradeLevel);
        Console.WriteLine(ofMiceAndMen.GetType() + " " + ofMiceAndMen.Title + " by: " + ofMiceAndMen.Author + " ISBN = " + ofMiceAndMen.ISBN + " Price: " + ofMiceAndMen.Price);

        Book one = new Book();
        Book two = new Book();
        Book three = new Book();
        one.ISBN = "4-84-946835-7";
        two.ISBN = "4-84-946835-7";
        three.ISBN = "3-95-632643-5";
        if (one.Equals(two) == true)
            Console.WriteLine("Clear");
        else
            Console.WriteLine("Error");
        if (two.Equals(one) == true)
            Console.WriteLine("Clear");
        else
            Console.WriteLine("Error");
        if (two.Equals(three) == false)
            Console.WriteLine("Clear");
        else
            Console.WriteLine("Error");
    }

}
public class Book
{
    public string ISBN { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public double Price { get; set; }
    public override bool Equals(Object e)
    {
        bool equal;
        Book book = (Book)e;
        if (ISBN == book.ISBN)
            equal = true;
        else
            equal = false;
        return equal;
    }
}
public class TextBook : Book
{
    public int GradeLevel { get; set; }
    public new double Price
    {
        get
        {
            return Price;
        }
        set
        {
            if (value < 20)
                Price = 20;
            if (value > 80)
                Price = 80;
            else
                Price = value;
        }
    }
}
public class CoffeeTableBook : Book
{
    public new double Price
    {
        get
        {
            return Price;
        }
        set
        {
            if (value < 35)
                Price = 35;
            if (value > 100)
                Price = 100;
            else
                Price = value;
        }
    }
}

Thanks

Answers (5)