Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I feel like I am doing everything right on this program but it is still spitting out errors. I am severely frustrated with this right now and I just need someone else to look at it.
I think the problem is something easy like a curley brace in the wrong place but I am getting a ton of errors.
I am just trying to learn about inheritance! I haven't been doing code long enough to properly debug this thing by myself and it's probably something simple like I said before, but I'm getting 40 errors for a simple program.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookConsole
{
    class Program
    {
        static void Main()
        {
            public string input;
            Console.WriteLine("Do you want a book, a textbook, or a coffee table book?");
            string input = Console.ReadLine();

            if (input == "book") 
            {
                Book myBook = new Book();
                 
                Console.WriteLine("Enter Title");
                myBook.Title = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Author?");
                myBook.Author = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Enter price");
                myBook.Price = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("This is book {0} Titled: {1} Written by: {2} Price: {3}", myBook.BookNumber, myBook.Title, myBook.Author, myBook.Price);
            }
           else if (input == "textbook")
            {
                TextBook myText = new TextBook();
                Console.WriteLine("Enter Grade Level");
                myText.Grade = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Enter Title");
                myText.Title = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Author?");
                myText.Author = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Enter price");
                myText.Price = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("This is book {0} Titled: {1} Written by: {2} Price: {3} Grade Level: {4}", myText.BookNumber, myText.Title, myText.Author, myText.Price, myText.Grade);
            }
            else 
            {
                CoffeeTableBook myCoffee = new CoffeeTableBook();
                 
                Console.WriteLine("Enter Title");
                myCoffee.Title = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Author?");
                myCoffee.Author = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Enter price");
                myCoffee.Price = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("This is book {0} Titled: {1} Written by: {2} Price: {3}", myCoffee.BookNumber, myCoffee.Title, myCoffee.Author, myCoffee.Price);
            }
    }
 }   
    static class Book
    {
        public double price;
        public int BookNumber { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public virtual double Price { get; set; }
    }
    static class TextBook : Book
    {
        public override double Price {
        get { return price; }
        set { 
           if (value < 20.00 || value > 80.00)
           { 
               Console.WriteLine("Invalid price.");
           }
           else
           {
               Console.WriteLine("Valid Price");
               price = value;
           }
        }
        }
        public char Grade { get; set; }
    }
    static class CoffeeTableBook : Book
    {
        public override double Price
        {
            get{ return price; }
            set{
                if (value < 35.00 || value > 100.00)
                {
                    Console.WriteLine("invalid Price.");
                }
                else
                {
                    Console.WriteLine("Valid Price");
                    price = value;
                }
            }
        }
    }
}


What I have tried:

Over the past 5 days I've tried changing places of curley brackets, adding and deleting them in different areas as well. I think my classes, inheritance and if-else statements are right. I made almost everything public out of frustration.

And I debugged to the best of my ability.
Posted
Updated 31-Mar-16 11:55am

You can't create instances of static classes:
C#
Book myBook = new Book();

-> Remove the static modifier from your three "book-classes".

Variables declared in methods can't have modifiers because they're automatically and always only visible within that method:
C#
public string input;

The advice would be to remove the public modifier from that variable declaration, but: You then re-declare input here:
C#
string input = Console.ReadLine();

-> Completely remove the first declaration of input.

TextBook.Grade is of type char, so you can't assign a string:
C#
myText.Grade = Convert.ToString(Console.ReadLine());

-> Proposed solution:
C#
string gradeInput = Console.ReadLine();
if (gradeInput.Length == 1)
{
   myText.Grade = gradeInput[0];
}
else
{
   // handle malformed input (e.g. error message, let user try again)
}


With these modifications all compiler errors should be dealt with.

Then there are some other issues which can be considered style/good practice issues:

- Don't validate values in property-setters. Alternatives: Either provide all values through the constructor, validate them there and in case of an invalid value, throw an exception. Or have a dedicated method for validation like:
C#
bool IsValid()
{
   // ...
}

(Which would, in an application where those books would subsequently be stored into a database, be called before the book would be stored and in case it's invalid, the saving-process would be stopped. So, in your case it would be a bit artificial.)

- Console.ReadLine() already returns a string. So passing it into Convert.ToString(..) is completely redundant:
C#
Convert.ToString(Console.ReadLine());


- For converting strings into other types, the preferred (static) methods are .Parse(..) or .TryParse(..) of the according types. The difference being that .Parse(..) returns the parsed value if successful and throws an exception if not successful while .TryParse(..) returns true if successful and places the parsed value into an out-parameter and returns false if not successful. The latter is often the more convenient option:
C#
double price;
if (!Double.TryParse(Console.ReadLine(), out price))
{
   // handle malformed input (e.g. error message, let user try again)
}


A free PDF-book on programming in C#: Introduction to Programming with C# / Java Books[^]
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 31-Mar-16 17:26pm    
If the inquirer fixes the first line you pointed out, the code sill will be sunk in error messages, because much earlier line spoils it all. Please see Solution 2.
—SA
Sascha Lefèvre 31-Mar-16 17:30pm    
I spotted it after I submitted my solution ;-)
Sergey Alexandrovich Kryukov 31-Mar-16 17:32pm    
A 5 then. :-)
—SA
Sascha Lefèvre 31-Mar-16 19:09pm    
Thank you, Sergey.
What would you expect if you are not even trying to write in this language, C#?
And not even trying to understand very clear messages of a compiler. Apparently, you are falling in "frustration" well too early.

The trouble starts with this line:
C#
public string input;

What do you think it possibly could mean? This is a syntax of the declaration of the member of a class, a field. But you are placing it at the level of the implementation of the method Main. Methods don't have members, only types have. Removing "public" would fix it, by turning a member declaration into a variable.

This line screws up the rest of the code. From this point, there nothing to discuss. But on some line below you have
C#
static class TextBook : Book

What, did you fail to read that C# and .NET does not allow extension of static types? This is not a general rule of programming, but not allowed in .NET. This line screws up the whole class. And so on… I honestly explained first bug and don't need to go any further.

Fix it and proceed.

Now, I can see two possibilities: 1) you've don few random mistakes and failed to understand the compiler messages, due to low experience, 2) you have no idea of some very basic programming principles and no glue on how the language you are trying to use works. Second case seems more likely to me. Then you should start with having some manual on the language and platform, with explanation of general programming ideas, and read it all, without skipping a single line. You have to understand that you all you need is understanding; you don't have to memorize anything. But you should solve simple exercises to each chapter, better all of them, unless you feel some are too trivial. This way, most people managed to come to enlightenment, so you can, too.

I would add my simple rule: don't write a single line of code which you don't understand.

—SA
 
Share this answer
 
v2
Comments
Member 12370098 31-Mar-16 18:02pm    
Thanks for the replies. I'm sorry. I admit I don't fully understand it all yet. Like I said I started adding things just to get rid of errors.

I honestly didn't think that would generate any errors. I thought in console apps you were supposed to use static modifiers. My logic behind the public string was to make sure it worked throughout the program, I was getting out of bounds errors.

I'm trying to understand it by doing it, I haven't understood everything I've learned.

Thank you for your advice and I'll definitely keep it in mind.
Sergey Alexandrovich Kryukov 31-Mar-16 18:10pm    
You are very welcome. I wish you to follow my advice, this is quite a constructive way.

Also, don't think in terms "we were supposed to". The static is fundamental problem of programming, but "console" simply tells the system that a console should be shown (and, besides that, a "Console Application" type can be anything, even Windows GUI — try it; the name of this application type is misleading; it's really nothing but "add console").

Always act out of basic principles. There is nothing binging static with console, nothing at all. Again, it's a good idea to take the book and read it all, not missing a line. It's okay if you fail to understand some part of the text, it's more important that you are aware of what is there. You can write what's unclear in your records, bookmark the places and return there later. I would also advice not to be overly goal-oriented in terms of the project goal, don't try to satisfy all requirements by any costs, which is a sure way for abuses. Giving up would be much wiser, to get back to the problems later. Don't put on your plate more than you can eat. Return to harder aspects later, when it would make more sense.

—SA
Sascha Lefèvre 31-Mar-16 18:15pm    
You're welcome! Good luck! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900