diff --git a/EmailCarbonFootprint/Assignment1.csproj b/EmailCarbonFootprint/Assignment1.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/EmailCarbonFootprint/Assignment1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/EmailCarbonFootprint/Constant/CarbonFootprintConstant.cs b/EmailCarbonFootprint/Constant/CarbonFootprintConstant.cs new file mode 100644 index 0000000..9f01123 --- /dev/null +++ b/EmailCarbonFootprint/Constant/CarbonFootprintConstant.cs @@ -0,0 +1,9 @@ +namespace EmailCarbonFootprint +{ + internal static class CarbonFootprintConstants + { + internal const double _inboxCarbonEmissionPerGram = 4.0; + internal const double _sentCarbonEmissionPerGram = 0.2; + internal const double _spamCarbonEmissionPerGram = 0.3; + } +} \ No newline at end of file diff --git a/EmailCarbonFootprint/EmailCarbonFootprintCalculatorApp.cs b/EmailCarbonFootprint/EmailCarbonFootprintCalculatorApp.cs new file mode 100644 index 0000000..5790d17 --- /dev/null +++ b/EmailCarbonFootprint/EmailCarbonFootprintCalculatorApp.cs @@ -0,0 +1,70 @@ +namespace EmailCarbonFootprint +{ + internal class EmailCarbonFootprintCalculatorApp + { + internal static void Main() + { + Console.WriteLine("This is the Program for calculating the Email Carbon Footprint"); + EmailInfo emailInfo = GetUserInput(); + CarbonFootprintCalculator calculator = new CarbonFootprintCalculator(emailInfo); + CarbonFootprintResponse response = calculator.CalculateCarbonFootprint(); + PrintResponse(response); + } + + private static EmailInfo GetUserInput() + { + string email; + int inbox, spam, sent; + + while (true) + { + Console.WriteLine("Enter your email id:"); + email = Console.ReadLine(); + + Console.WriteLine("Enter the number of emails present in your inbox:"); + if (!TryParseIntInput("inbox", out inbox)) + { + continue; + } + + Console.WriteLine("Enter the number of emails present in your spam/junk folder:"); + if (!TryParseIntInput("spam", out spam)) + { + continue; + } + + Console.WriteLine("Enter the number of emails present in your sent folder:"); + if (!TryParseIntInput("sent", out sent)) + { + continue; + } + break; + } + + return new EmailInfo(email, inbox, spam, sent); + } + + private static bool TryParseIntInput(string inputName, out int result) + { + if (int.TryParse(Console.ReadLine(), out result)) + { + return true; + } + else + { + Console.WriteLine($"Invalid input for {inputName}. Please enter a valid integer."); + return false; + } + } + + private static void PrintResponse(CarbonFootprintResponse response) + { + Console.WriteLine("\nYour Current Email Carbon Footprint Response is:"); + Console.WriteLine("emailId : " + response._email); + Console.WriteLine("source : " + response._source); + Console.WriteLine("inbox : " + response._inboxCarbonFootPrint + "g"); + Console.WriteLine("sent : " + response._sentCarbonFootPrint + "g"); + Console.WriteLine("spam : " + response._spamCarbonFootPrint + "g"); + } + } +} \ No newline at end of file diff --git a/EmailCarbonFootprint/Helper/CarbonFootprintCalculator.cs b/EmailCarbonFootprint/Helper/CarbonFootprintCalculator.cs new file mode 100644 index 0000000..8682ce7 --- /dev/null +++ b/EmailCarbonFootprint/Helper/CarbonFootprintCalculator.cs @@ -0,0 +1,61 @@ +namespace EmailCarbonFootprint +{ + internal class CarbonFootprintCalculator + { + private EmailInfo entity; + private CarbonFootprintResponse response; + + internal CarbonFootprintCalculator(EmailInfo entity) + { + this.entity = entity; + this.response = new CarbonFootprintResponse(); + } + + internal CarbonFootprintResponse CalculateCarbonFootprint() + { + SetResponse(); + return response; + } + + private void SetResponse() + { + response._email = entity._emailId; + response._source = FetchSourceNameFromEmailId(); + response._inboxCarbonFootPrint = GetInboxCarbonFootPrint(); + response._sentCarbonFootPrint = GetSentCarbonFootPrint(); + response._spamCarbonFootPrint = GetSpamCarbonFootPrint(); + } + + private string FetchSourceNameFromEmailId() + { + string email = entity._emailId; + int startIndex = email.IndexOf('@'); + + if (startIndex >= 0 && startIndex < email.Length - 1) + { + string domain = email.Substring(startIndex + 1); + int dotIndex = domain.IndexOf('.'); + if (dotIndex >= 0) + { + return domain.Substring(0, dotIndex); + } + } + return "Invalid Domain"; + } + + private double GetInboxCarbonFootPrint() + { + return entity._totalInboxMessages * CarbonFootprintConstants._inboxCarbonEmissionPerGram; + } + + private double GetSentCarbonFootPrint() + { + return entity._totalSentMessages * CarbonFootprintConstants._sentCarbonEmissionPerGram; + } + + private double GetSpamCarbonFootPrint() + { + return entity._totalSpamMessages * CarbonFootprintConstants._spamCarbonEmissionPerGram; + } + } +} diff --git a/EmailCarbonFootprint/Model/CarbonFootprintResponse.cs b/EmailCarbonFootprint/Model/CarbonFootprintResponse.cs new file mode 100644 index 0000000..a8fcee4 --- /dev/null +++ b/EmailCarbonFootprint/Model/CarbonFootprintResponse.cs @@ -0,0 +1,11 @@ +namespace EmailCarbonFootprint +{ + internal class CarbonFootprintResponse + { + internal string _email { get; set; } + internal string _source { get; set; } + internal double _inboxCarbonFootPrint { get; set; } + internal double _sentCarbonFootPrint { get; set; } + internal double _spamCarbonFootPrint { get; set; } + } +} \ No newline at end of file diff --git a/EmailCarbonFootprint/Model/EmailInfo.cs b/EmailCarbonFootprint/Model/EmailInfo.cs new file mode 100644 index 0000000..1c12bd0 --- /dev/null +++ b/EmailCarbonFootprint/Model/EmailInfo.cs @@ -0,0 +1,18 @@ +namespace EmailCarbonFootprint +{ + internal class EmailInfo + { + internal string _emailId { get; } + internal int _totalInboxMessages { get; } + internal int _totalSentMessages { get; } + internal int _totalSpamMessages { get; } + + internal EmailInfo(string email, int inbox, int spam, int sent) + { + _emailId = email; + _totalInboxMessages = inbox; + _totalSpamMessages = spam; + _totalSentMessages = sent; + } + } +} \ No newline at end of file