C# Intermediate course Exercise-2, Need a community guidance, what is good in this exercise? what should be Improve

Exercise 2: Design a StackOverflow PostDesign a class called Post.
This class models a StackOverflow post. It should have properties for title, description and the date/time it was created. We should be able to up-vote or down-vote a post. We should also be able to see the current vote value. In the main method, create a post, up-vote and down-vote it a few times and then display the the current vote value. In this exercise, you will learn that a StackOverflow post should provide methods for up-voting and down-voting. You should not give the ability to set the Vote property from the outside, because otherwise, you may accidentally change the votes of a class to 0 or to a random number. And this is how we create bugs in our programs. The class should always protect its state and hide its implementation detail.

solution I prepared

Post class

using System;

namespace Classes_Exercise_Mosh
{
public class Post
{
public string _title { get; set; }
public string _description { get; set; }
public DateTime _createdDate { get; set; }
public int _upVote { get; private set; } = 0;
public int _downVote { get; private set; } = 0;

    public Post(string Title,string Description)
    {
        _title = Title;
        _description = Description;
        _createdDate = DateTime.Now;
    }
  
    public int UpVote()
    {
        _upVote++;
        return _upVote;
    }
    public int DownVote()
    {
        _downVote++;
        return _downVote;
    }
}

}

Main Program class

using System;
using System.Threading;

namespace Classes_Exercise_Mosh
{
class Program
{
static void Main(string[] args)
{
ConsoleKeyInfo cki;
var post = new Post(“How to Throw NullReferenceException?”, “Using TryCatch Block”);
Console.WriteLine(“Post Title : {0}”,post._title);
Console.WriteLine(“Post Description: {0}”, post._description);
int choice;
do
{
cki = Console.ReadKey();
Console.WriteLine(“Enter Vote choice 1 for UpVote, 0 for DownVote:”);
choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                var votecount = post.UpVote();
                Console.WriteLine("Your Upvote is:{0}", votecount);
            }

            if (choice == 0)
            {
                var downvotecount = post.DownVote();
                Console.WriteLine("Your Downvote is:{0}", downvotecount);
            }
        } while (cki.Key != ConsoleKey.Escape);
    }
}

}

Hello, I might recommend a couple things. Your public fields upvote and downvote don’t need to be properties and they can be private. You’re already returning them in your methods. They can just be fields. Per Microsoft’s convention, public properties should use CamelCase.

public string Title { get; set; }
public string Description { get; set; }
public DateTime CreatedDate { get; set; }

private int _upVote = 0;
private int _downVote = 0;
1 Like

Thank you for your feedback.

Hey! I have another approach. Can someone give me a feedback. Thanks in advance!

public class Post
{
private int _upvalue = 0;
private int _downvalue = 0;
public string Title{ get; set; }
public string Description { get; set; }
public DateTime Datecreated { get; set; }

    public int Upvoting()
    {
        _upvalue ++;
        return _upvalue;

    }
    public int Downvoting()
    {
        _downvalue ++;
        return _downvalue;

    }
   

}

// This is the Main class

class Program
{
static void Main(string[] args)
{
var post = new Post();
post.Description=“C# Intermediate Level”;
post.Datecreated = new DateTime(2021, 10,27);
try
{

                Console.WriteLine(post.Description);
                Console.WriteLine(post.Datecreated.Date);
                Console.WriteLine("*Like =1* or *Dislike=2* or *Exit=3*");
            var input= '0';
               


            do
            {
                input = Convert.ToChar(Console.ReadLine());

                switch (input)
                {
                    case '1':
                        {
                            Console.WriteLine("Vote value {0} like", post.Upvoting());
                            break;
                        }
                    case '2':
                        {
                            Console.WriteLine("Vote value {0} unlike", post.Downvoting());
                            break;
                        }
                    case '3':
                        {
                            Console.WriteLine("Exiting application");
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("Invalid input");
                            break;
                        }

                }
   

            }
            while (input == '1' || input == '2');
            
             
            }
        catch (Exception)
        {

            Console.WriteLine("Invalid input");
        }
       

        

    }
}

I did something similir to you. But I created a method called DysplayingPost() and Snippet

DisplayingVotes(). As follow:

Main Method:

using StackOverflow_Post;

var posting = new Post();

Console.WriteLine(“-----STACKOVERFLOW FORUM AREA-----\n”);

Console.WriteLine("Title of post: ");
var title = Console.ReadLine();
posting.Title(title);

Console.WriteLine("\nDescription of the post: ");
var description = Console.ReadLine();
posting.Description(description);

posting.TimeOfPost();

posting.DisplayingPost();

while (true)
{
Console.WriteLine("\nFor Up-vote → ‘1’ / For Down-vote press → ‘2’ / To Exit → 999 ");
var vote = Convert.ToInt32(Console.ReadLine());
posting.CalculatingVotes(vote);
posting.DisplayingVotes();
if (voto == 999)
break;
}

Post Class:

internal class Post
{
	private String _title;
	private String _description;
	private DateTime _dateOfPost;
	private int _upVotes = 0;
	private int _downVotes = 0;
	public void Title(string title)
	{
		_title = title;
	}

	public void Description(string description)
	{
		_description = description;
	}

	public void TimeOfPost()
	{
		_dateOfPost = DateTime.Now;
	}

	public void DisplayingPost()
	{
		Console.WriteLine("\n------POSTAGEM STACKOVERFLOW------");
		Console.WriteLine($"Título: {_title}");
		Console.WriteLine($"Criado em: {_dateOfPost}");
		Console.WriteLine($"Post: {_description}");
	}
	public void CalculatingVotes(int vote)
	{
		if (vote == 1)
			_upVotes++;
		else if (vote == 2)
			_downVotes++;
		
	}
	public void DisplayingVotes()
	{
		Console.WriteLine($"\nUp-votes: {_upVotes}  /  Down-votes: {_downVotes}");	
	}
}
1 Like