Design 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.
Educational tip: The aim of this exercise is to help you understand that classes should encapsulate data AND behaviour around that data. Many developers (even those with years of experience) tend to create classes that are purely data containers, and other classes that are purely behaviour (methods) providers. This is not object-oriented programming. This is procedural programming. Such programs are very fragile. Making a change breaks many parts of the code.
That is the information given to me for this exercise in my C# tutorial. The following is my working code. Still trying to grasp Object Oriented Programming. I believe I did set the properties so that you can get the information but not give the ability to set the Vote property from the outside by setting the property to Private. I also tried to protect the state of voting so that you could not up-vote or down-vote consecutively, yet allow it to change your vote and show the vote count along with protecting the state of the vote count.
Anyhow if anyone can see how to improve on this, point out a better way, or teach me something new I would greatly appreciate it.
using System;
namespace ExerciseTwo
{
class Post
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime TimeDateCreated { get; private set; }
public int VoteCount { get; private set; }
private bool HasVotedUp;
private bool HasVotedDown;
public Post(string title, string description)
{
Title = title;
Description = description;
TimeDateCreated = DateTime.UtcNow;
VoteCount = 0;
}
public void VoteUp()
{
if (HasVotedUp)
{
throw new Exception("You have already up-voted.");
}
else
{
VoteCount++;
HasVotedUp = true;
HasVotedDown = false;
}
}
public void VoteDown()
{
if (HasVotedDown)
{
throw new Exception("You have already down-voted.");
}
else
{
VoteCount--;
HasVotedDown = true;
HasVotedUp = false;
}
}
}
}
Script to demo how it could work.
namespace ExerciseTwo
{
class Program
{
static void Main()
{
Post post = new Post("Does my post work?", "Test to see if my post works.");
System.Console.WriteLine($"Title: {post.Title}");
System.Console.WriteLine($"Description: {post.Description}");
System.Console.WriteLine($"Date Created: {post.TimeDateCreated}");
System.Console.WriteLine($"Post Count: {post.VoteCount}");
post.VoteDown();
System.Console.WriteLine($"Post Count: {post.VoteCount}");
post.VoteUp();
System.Console.WriteLine($"Post Count: {post.VoteCount}");
}
}
}
Thank you for your help!