StackOverFlow Exercise: Classes

Can someone please review my solution to the StackOverFlow exercise for C#.


using System;

namespace StackOverflow
{

    public class Post
    {
        public string _title;
        public string _description;
        public DateTime _dateCreated;

        private int _upVote= 0;
        private int _downVote = 0;

        public Post(string title, string description, DateTime datetime)
        {
            this._title = title;
            this._description = description;
            this._dateCreated = datetime;
        }

        public int UpVote()
        {
            return _upVote += 1;
        }

        public int DownVote()
        {
            _downVote--;

            if(_downVote < 0)
            {
                _downVote = 0;
            }

            return _downVote;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Post post1 = new Post("Hello", "This is my post", new DateTime(2023, 12, 31));

            Console.WriteLine("Post Title: {0}\n Post Description: {1}\n Date Created: {2}", post1._title, post1._description, post1._dateCreated);
            
            Console.WriteLine("Enter Vote. Choose 1 for UpVote, 0 for DownVote: ");
            var choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                var upVoteCount = post1.UpVote();
                Console.WriteLine("Your Upvote = " + upVoteCount);
            }

            if (choice == 0)
            {
                var downVoteCount = post1.DownVote();
                Console.WriteLine("Your DownVote = " + downVoteCount);
            }
        }
    }
}

Hi,

I do not know the assignement instructions so I don’t know what is asked. I’ll give you my feedback in the limits of what I can observe.

For Downvote() you can simply do a check and do nothing.

        public int DownVote()
        {
            _downVote--;

            if(_downVote < 0)
            {
                _downVote = 0;
            }

            return _downVote;
        }

I would have done that instead

        public int DownVote()
        {
            if(_downVote > 0)
            {
                _downVote--;
            }

            return _downVote;
        }

You may want to go an extra mile in case the value would for any reason be negative so that it at least goes back to 0. Not sure it is needed though.

        public int DownVote()
        {
            if(_downVote > 0)
                _downVote--;

            if(_downVote < 0)
                _downVote = 0;

            return _downVote;
        }

In the main program this line is perfectly OK.

Console.WriteLine("Post Title: {0}\n Post Description: {1}\n Date Created: {2}", post1._title, post1._description, post1._dateCreated);

But I have a taste for string interpolation when available.

Console.WriteLine($"Post Title: {post1._title}\n Post Description: {post1._description}\n Date Created: {post1._dateCreated}");

When writing multiline I would suggest using a tab \t on the other lines for readability.

The rest is assignement dependant so it may be just as good as it is now.

That said I would make this a bit more interesting by giving the possibility to loop around the question so that you could vote several times. You can play around and try on your own.

It seems this is a fairly early in the course exercise so you maybe haven’t learnt what’s needed for now. Give it a look when you have.

In term of design I would use properties instead of fields for anything you need to access outside of the class. Such as your Upvote count. You may decide to have those methods return the result or not. Depends on your needs.

Overall this looks fine to me.

Cheers.