// CSV parser by Mark Grealish (s00123474@itsligo.ie).
using System;
using System.IO;
using System.Collections.Generic;
public class CsvReader
{
public static string path = "players.csv";
static void Main()
{
int menuOpt = 42;
do
{
Console.Clear();
Console.WriteLine("1. View the contents of {0}.", path);
Console.WriteLine("2. Search for a specific record in {0}.", path);
Console.WriteLine();
Console.WriteLine("4. Exit.");
Console.WriteLine();
menuOpt = IntParse("Selection: ");
switch (menuOpt)
{
case 1:
CsvOutput(path, -1);
break;
case 2:
CsvSearch(path);
break;
case 4:
break;
default:
break;
}
} while (menuOpt != 4 );
}
// ++++LOAD CSV FILE++++
private static string[] CsvLoad(string path)
{
// Read the CSV file into a temp list, and then convert the list into a usable array.
// The general philosophy here is to refresh the array whenever it is called for.
List<string> temp = new List<string>();
string line = "";
using (StreamReader csv = new StreamReader(path))
{
line = csv.ReadLine();
while (line != null)
{
temp.Add(line);
line = csv.ReadLine();
}
}
string[] data = temp.ToArray();
return data;
}
// ++++VIEW CSV FILE+++
static void CsvOutput( string path, int rec )
{
// Fill the array using the above method and output it per an input int:
// If -1, output the entire array.
// If >= 0, output that specific line.
string[] csv = CsvLoad(path);
string[] row = csv[0].Split(',');
string[] header = new string[4] {"Player #:", "Name:", "Score:", "Citation:"};
string dash = "---------------";
string spacer ="{0,-20}";
// Header.
Console.Clear();
for (int i = 0; i < header.Length; i++)
Console.Write(String.Format(spacer, header[i]));
Console.WriteLine();
for (int i = 0; i < header.Length; i++)
Console.Write(String.Format(spacer, dash));
if (rec == -1)
{
// Output the entire array/CSV.
for (int i = 0; i < csv.Length; i++)
{
row = csv[i].Split(',');
for (int j = 0; j < row.Length; j++)
Console.Write(String.Format(spacer, row[j]));
Console.Write(String.Format(spacer, Citation(row[2])));
}
}
else if (rec >= 0)
{
// Output a single row.
row = csv[rec].Split(',');
for (int i = 0; i < row.Length; i++)
Console.Write(String.Format(spacer, row[i]));
Console.Write(String.Format(spacer, Citation(row[2])));
}
Back();
}
// ++++SEARCH FOR A RECORD++++
static void CsvSearch(string path)
{
string[] csv = CsvLoad(path);
int n = 0;
int s = 0;
Console.Clear();
Console.Write( "Enter search string (case sensitive): " );
string query = Convert.ToString(Console.ReadLine());
try
{
do
{
s = csv[n].IndexOf(query);
if ( s >= 0 )
break;
n++;
} while ((n < csv.Length) );
}
catch (IndexOutOfRangeException)
{
}
if (s >= 0)
{
Console.Clear();
Console.WriteLine("Record matched!");
CsvOutput( path, n );
}
else if (s < 0)
{
Console.Clear();
Console.WriteLine("String not matched, sorry. :(");
Back();
}
}
// ++++CITATIONS++++
private static string Citation(string input)
{
// Return a citation based on the player's score.
string[] cit = new string [5] {
"Sluggish Snail",
"Ambling Armadillo",
"Bobbing Bobcat",
"Rocketing Rabbit",
"Turbocharged Cheetah"
};
int score = Convert.ToInt32(input);
string citation = "";
if (score < 4000)
citation = cit[0];
if ((score >= 4000) && (score <= 5999))
citation = cit[1];
if ((score >= 6000) && (score <= 6999))
citation = cit[2];
if ((score >= 7000) && (score <= 9999))
citation = cit[3];
if ((score > 9999))
citation = cit[4];
return citation;
}
// ++++GO BACK++++
static void Back()
{
Console.WriteLine();
Console.Write("Press [RETURN] to return to the main menu.");
Console.ReadLine();
}
// ++++PARSE INT++++
private static int IntParse(string userPrompt)
{
bool evalInt = false;
int parsedInt = 0;
do
{
Console.Write(userPrompt);
evalInt = Int32.TryParse(Console.ReadLine(), out parsedInt);
if (!evalInt)
InputFail();
} while (!evalInt);
return parsedInt;
}
// ++++PARSE DOUBLE++++
private static double DoubleParse(string userPrompt)
{
bool evalDouble = false;
double parsedDouble = 0;
do
{
Console.Write( userPrompt );
evalDouble = Double.TryParse(Console.ReadLine(), out parsedDouble);
if (!evalDouble)
InputFail();
} while (!evalDouble);
return parsedDouble;
}
// ++++ERROR++++
static void InputFail()
{
Console.WriteLine("Invalid input. Please enter a numerical value.");
}
}