Below I have a piece of code I have been working on for quite sometime:
enum SavingsIndicator
{
OKSavings,
GoodSavings,
GreatSavings,
}
static void Main(string[] args)
{
foreach (string line in gdsLines)
{
Match m = Regex.Match(line, @"^(?<date>[^|]+)\|.*Processed\s+(?<locator>[^.]+)\.{3}.*?Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
if (m.Success)
{
Savings s = new Savings();
s.Prefix = string.Empty;
s.GDSDate = m.Groups["date"].Value.Trim();
s.GDSLocator = m.Groups["locator"].Value.Trim();
s.GDSDollarSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
s.GDSPercentSavings = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);
gdsSavings.Add(s);
gdsTotalSavings += s.GDSDollarSavings;
gdsTotalPercentage += s.GDSPercentSavings;
if (s.GDSDollarSavings >= 500)
{
s.Prefix = SavingsIndicator.GreatSavings;
}
else if (s.GDSDollarSavings >= 100 && s.GDSDollarSavings < 500)
{
s.Prefix = SavingsIndicator.GoodSavings;
}
else
{
s.Prefix = SavingsIndicator.OKSavings;
}
}
}
foreach (var savings in gdsSavings.OrderByDescending(savingsInstance => savingsInstance.GDSDollarSavings))
{
Console.WriteLine(savings.ToString());
}
}
}
Below is the class that I have for this to work:
class Savings
{
public string Prefix { get; set; }
public string GDSDate { get; set; }
public string GDSLocator { get; set; }
public decimal GDSDollarSavings { get; set; }
public decimal GDSPercentSavings { get; set; }
public override string ToString()
{
return Prefix + "\nDate: " + GDSDate + " \nPNR Locator: " + GDSLocator + " \nDollar Savings: $" + GDSDollarSavings + " \nPercent Savings: " + GDSPercentSavings + " % " + "\n";
}
}
The problem I am having is where I have the prefixes. I want those enumerated values to be in the Prefix variable of my class, but I can't convert my enumerated values to a string. Is there a way to successfully convert this into a string, or is there another way to go around this?