So this is a pretty simple code, I think it was?
It asks for the user's birth day and month, and gives it back with the day a discount reminder email will be sent (the day before their birthday)
Now I tried to optimize as much as I could, possible wrong inputs and if the user's birthday is the first of a month
Even though I'm still pretty new to coding, I still want you to criticize my code as much as I could, I would like to improve as much as I can
using System.Text.RegularExpressions;
using System.Linq;
namespace Exercice14
{
class Program
{
static void Main(string[] _)
{
// declaring some variables
int birthDay;
int reminderDay;
string suffix = "th";
string reminderSuffix = "th";
string birthDayT;
string birthMonth;
string reminderMonth;
string[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
bool exceptionFirst = false;
// prompts for birth month and capitalize first letter
Console.WriteLine("Hello User!");
Console.Write("Please enter your birth month in letters: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
// check if birth month contains only letters
while(Regex.IsMatch(birthMonth, @"^[a-zA-Z]+$") == false)
{
Console.WriteLine("Birth month should only contain letters!");
Console.Write("Please enter your birth month in letters: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
}
// check if month is right
while (months.Contains(birthMonth) == false)
{
Console.WriteLine("Invalid month?! Please enter a valid english month");
Console.Write("Please enter your birth month: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
}
// prompts for birth day
Console.Write("Please enter your birth day in numbers: ");
birthDayT = Console.ReadLine();
// check for valid day
while (int.TryParse(birthDayT, out int _) == false)
{
Console.WriteLine("Invalid argument! Please enter day in numerals");
Console.Write("Please enter your birth day in numbers: ");
birthDayT = Console.ReadLine();
}
// check for valid day number
while (int.Parse(birthDayT) < 1 || int.Parse(birthDayT) > 31)
{
Console.WriteLine("Invalid date! Please enter a day between 1 and 31");
Console.Write("Please enter birth day in numbers: ");
birthDayT = Console.ReadLine();
}
// assign birth day to variable once tested
birthDay = int.Parse(birthDayT);
// set reminder day and month
reminderDay = birthDay - 1;
reminderMonth = birthMonth;
// check which suffix to use for days AND calculate reminder day and month if exception
if (birthDay == 1) //exception
{
exceptionFirst = true;
suffix = "st";
reminderMonth = months[Array.IndexOf(months, birthMonth) - 1];
}
if (birthDay == 2)
{
suffix = "nd";
reminderSuffix = "st";
reminderDay = 1;
}
if (birthDay == 3)
{
suffix = "th";
reminderSuffix = "nd";
}
if (birthDay > 3)
{
suffix = "th";
reminderSuffix = "th";
}
// print values
Console.WriteLine();
Console.WriteLine("Yer birthday is on the " + birthDay + suffix + " of " + birthMonth );
if (exceptionFirst == true)
{
Console.WriteLine("A reminder email for your birthday discount " +
"\nwill be sent on the last day of " + reminderMonth);
}
else
{
Console.WriteLine("A reminder email for your birthday discount " +
"\nwill be sent on the " + reminderDay + reminderSuffix + " of " + reminderMonth);
}
}
}
}