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
Loading
Amit ChoudharyPosted May 3, 2010, 12:02 PM
i have modified your code little bit and its working now. look at the lines i have changed and added new
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");
Console.ReadKey();
}
}
public class Book
{
protected string isbn;
protected string title;
protected string author;
protected double price;
public string ISBN
{
get
{
return isbn;
}
set
{
isbn = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
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 base.price;
}
set
{
if (value < 20)
Price = 20;
if (value > 80)
base.price = 80;
else
base.price = value;
}
}
}
public class CoffeeTableBook : Book
{
public new double Price
{
get
{
return base.price;
}
set
{
if (value < 35)
Price = 35;
if (value > 100)
base.price = 100;
else
base.price = value;
}
}
}
Cheers programming.. and don't depend on teachers..
Please mark as "Do you like this post" if it really helps you.
Thomas KlovertPosted May 3, 2010, 4:11 PM
Amit ChoudharyPosted May 3, 2010, 2:54 PM
Cheers
Thomas KlovertPosted May 3, 2010, 12:44 PM
It works now... thank you for your help Amit.
Thomas KlovertPosted May 3, 2010, 12:21 PM
Is it necessary to declare the Book variables as protected?
also
I am now having another problem where book "two" is considered equal (by the Equals() method) to book "three". See, I tried to override Equals() to make it consider books with the same ISBN as "equal". Is the problem because I did not override GetHashCode()?
Updated Code:
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: {0:C}", braveNewWorld.Price);
Console.WriteLine(computerScience.GetType() + ": " + computerScience.Title + " by: " + computerScience.Author + " ISBN = " + computerScience.ISBN + " Price: {0:C}" +" Grade Level = " + computerScience.GradeLevel, computerScience.Price);
Console.WriteLine(ofMiceAndMen.GetType() + ": " + ofMiceAndMen.Title + " by: " + ofMiceAndMen.Author + " ISBN = " + ofMiceAndMen.ISBN + " Price: {0:C}", 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");
Console.ReadKey();
}
}
public class Book
{
protected string isbn;
protected string title;
protected string author;
protected double price;
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 base.price;
}
set
{
if (value < 20)
base.price = 20;
if (value > 80)
base.price = 80;
else
base.price = value;
}
}
}
public class CoffeeTableBook : Book
{
public new double Price
{
get
{
return base.price;
}
set
{
if (value < 35)
base.price = 35;
if (value > 100)
base.price = 100;
else
base.price = value;
}
}
}