Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following question is tagged to c# programmming: I have a large text file, I

ID: 662721 • Letter: T

Question

The following question is tagged to c# programmming:

I have a large text file, I split it into two groups: A and B. This is a sample of what each line in the groups look like:

5/21/2015 11:57:12 PM | Batch 10|501/766|65.40 %|UNVM-R|Processed WJ77H6...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $50.00. Actual Savings: $748.00. Savings found: $748.00 (43.37 %). Savings were previously found.


Not every line will though will have a savings. I am writing a program that will look into these groups and will write to the console the lines that have savings as well as lace an asterisk on those that have savings of 30% or greater and will place two asterisks for lines that have 30% savings and $500 or greater in savings.

Below I have a foreach statement, but with nothing in it. I need the code/syntax that will help me find my solution. Please help with a detailed answer. Thanks!

          foreach(var line in a.Where(w => w.Contains("Savings found:")

             {

             }

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;


namespace ConsoleApplication1
{
class Program
{
public static string GetSubstringByString(string a, string b, string c)
{
return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
}

static void Main()
{
int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("Newfile.txt");
while ((line = file.ReadLine()) != null)
{
  
if (line.Contains("Savings found:"))
{
  
String sval = GetSubstringByString("(", ")", line);
double percent=0.0;
Regex regex = new Regex(@"^-?d+(?:.d+)?");
Match match = regex.Match(sval);
if (match.Success)
{
percent = (double)decimal.Parse(match.Value, CultureInfo.InvariantCulture);
  
}
if (percent >= 30.00)
{
Console.WriteLine("**" + line);
}
else
{
Console.WriteLine(line);
}
}
counter++;
}

file.Close();
}
}
}

In the above code, we are trying to find "Savings found:" in each line using while loop and then displaying the line as per the given conditions.