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.

I did in a different way if is useful to anyone:
Separate the class Post in a different file (OOP) Post.cs:

using System;

public class Post
{
    // Properties for title, description, and the date/time it was created
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime DateTimeCreated { get; private set; }

    // Private field to keep track of the vote count
    private int voteCount;

    // Constructor to initialize the title, description, and creation date
    public Post(string title, string description)
    {
        Title = title;
        Description = description;
        DateTimeCreated = DateTime.Now;
        voteCount = 0; // Initial vote count is zero
    }

    // Method to up-vote the post
    public void UpVote()
    {
        voteCount++;
    }

    // Method to down-vote the post
    public void DownVote()
    {
        voteCount--;
    }

    // Method to get the current vote count
    public int GetVoteCount()
    {
        return voteCount;
    }
}

and the main Program.cs file:

using System;
using System.Collections.Generic;

namespace post
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a new Post object
            Post post = new Post("My First Post", "This is the description of my first post.");

            // Up-vote and down-vote the post a few times
            post.UpVote();
            post.UpVote();
            post.DownVote();
            post.UpVote();

            // Display the current vote value
            Console.WriteLine("Post Title: " + post.Title);
            Console.WriteLine("Description: " + post.Description);
            Console.WriteLine("Date Created: " + post.DateTimeCreated);
            Console.WriteLine("Current Vote Count: " + post.GetVoteCount());
        }
    }
}

or you can even go ahead and do a more complete version:

using System;
using System.Collections.Generic;

public class Post
{
    // Properties for title, description, and the date/time it was created
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime DateTimeCreated { get; private set; }

    // Private field to keep track of the vote count
    private int voteCount;

    // Constructor to initialize the title, description, and creation date
    public Post(string title, string description)
    {
        Title = title;
        Description = description;
        DateTimeCreated = DateTime.Now;
        voteCount = 0; // Initial vote count is zero
    }

    // Method to up-vote the post
    public void UpVote()
    {
        voteCount++;
    }

    // Method to down-vote the post
    public void DownVote()
    {
        voteCount--;
    }

    // Method to get the current vote count
    public int GetVoteCount()
    {
        return voteCount;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        List<Post> posts = new List<Post>(); // List to store multiple posts

        while (true)
        {
            Console.WriteLine("\nSelect an option:");
            Console.WriteLine("1. Create a new post");
            Console.WriteLine("2. View all posts");
            Console.WriteLine("3. Vote on a post");
            Console.WriteLine("4. Exit");
            string input = Console.ReadLine();

            switch (input)
            {
                case "1":
                    // Create a new post
                    Console.Write("Enter the title of the post: ");
                    string title = Console.ReadLine();

                    Console.Write("Enter the description of the post: ");
                    string description = Console.ReadLine();

                    Post newPost = new Post(title, description);
                    posts.Add(newPost);
                    Console.WriteLine($"\nNew post created successfully!\nTitle: {newPost.Title}\nDescription: {newPost.Description}");
                    break;

                case "2":
                    // Display all posts
                    if (posts.Count == 0)
                    {
                        Console.WriteLine("\nNo posts available.");
                    }
                    else
                    {
                        Console.WriteLine("\nList of Posts:");
                        for (int i = 0; i < posts.Count; i++)
                        {
                            Console.WriteLine($"{i + 1}. {posts[i].Title} (Votes: {posts[i].GetVoteCount()})");
                        }
                    }
                    break;

                case "3":
                    // Vote on a specific post
                    if (posts.Count == 0)
                    {
                        Console.WriteLine("\nNo posts available to vote on.");
                    }
                    else
                    {
                        Console.WriteLine("\nSelect a post to vote on:");
                        for (int i = 0; i < posts.Count; i++)
                        {
                            Console.WriteLine($"{i + 1}. {posts[i].Title}");
                        }

                        Console.Write("Enter the post number: ");
                        if (int.TryParse(Console.ReadLine(), out int postIndex) && postIndex > 0 && postIndex <= posts.Count)
                        {
                            Post selectedPost = posts[postIndex - 1];

                            Console.WriteLine("\n1. Up-vote");
                            Console.WriteLine("2. Down-vote");
                            Console.Write("Choose an option: ");
                            string voteOption = Console.ReadLine();

                            if (voteOption == "1")
                            {
                                selectedPost.UpVote();
                                Console.WriteLine("You up-voted the post.");
                            }
                            else if (voteOption == "2")
                            {
                                selectedPost.DownVote();
                                Console.WriteLine("You down-voted the post.");
                            }
                            else
                            {
                                Console.WriteLine("Invalid vote option.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid post number.");
                        }
                    }
                    break;

                case "4":
                    // Exit the program
                    Console.WriteLine("Exiting the program.");
                    return;

                default:
                    Console.WriteLine("Invalid option. Please select 1, 2, 3, or 4.");
                    break;
            }
        }
    }
}