So this can be a fairly easy code, I believe it was?
It asks for the consumer’s start day and month, and provides it again with the day a reduction reminder e-mail might be despatched (the day earlier than their birthday)
Now I attempted to optimize as a lot as I may, attainable unsuitable inputs and if the consumer’s birthday is the primary of a month
Regardless that I am nonetheless fairly new to coding, I nonetheless need you to criticize my code as a lot as I may, I wish to enhance as a lot as I can
utilizing System.Textual content.RegularExpressions;
utilizing System.Linq;
namespace Exercice14
{
class Program
{
static void Essential(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", "Might", "June", "July", "August", "September",
"October", "November", "December" };
bool exceptionFirst = false;
// prompts for start month and capitalize first letter
Console.WriteLine("Hiya Consumer!");
Console.Write("Please enter your start month in letters: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
// examine if start month accommodates solely letters
whereas(Regex.IsMatch(birthMonth, @"^[a-zA-Z]+$") == false)
{
Console.WriteLine("Beginning month ought to solely include letters!");
Console.Write("Please enter your start month in letters: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
}
// examine if month is correct
whereas (months.Comprises(birthMonth) == false)
{
Console.WriteLine("Invalid month?! Please enter a legitimate english month");
Console.Write("Please enter your start month: ");
birthMonth = Console.ReadLine();
birthMonth = char.ToUpper(birthMonth[0]) + birthMonth.Substring(1);
}
// prompts for start day
Console.Write("Please enter your start day in numbers: ");
birthDayT = Console.ReadLine();
// examine for legitimate day
whereas (int.TryParse(birthDayT, out int _) == false)
{
Console.WriteLine("Invalid argument! Please enter day in numerals");
Console.Write("Please enter your start day in numbers: ");
birthDayT = Console.ReadLine();
}
// examine for legitimate day quantity
whereas (int.Parse(birthDayT) < 1 || int.Parse(birthDayT) > 31)
{
Console.WriteLine("Invalid date! Please enter a day between 1 and 31");
Console.Write("Please enter start day in numbers: ");
birthDayT = Console.ReadLine();
}
// assign start day to variable as soon as examined
birthDay = int.Parse(birthDayT);
// set reminder day and month
reminderDay = birthDay - 1;
reminderMonth = birthMonth;
// examine which suffix to make use of 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 e-mail on your birthday low cost " +
"nwill be despatched on the final day of " + reminderMonth);
}
else
{
Console.WriteLine("A reminder e-mail on your birthday low cost " +
"nwill be despatched on the " + reminderDay + reminderSuffix + " of " + reminderMonth);
}
}
}
}