Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- class BattleReader
- {
- // /path/to/file
- static string path = "bands.txt";
- // Formatting information. Used in more than one method.
- static string spacer = "{0,-20}";
- // Separator.
- static char sep = ',';
- static void Main ()
- {
- // Menu Choice.
- int mc = ChkFile(path);
- while (mc != 4)
- {
- string[] menu = {
- "Main menu:",
- "\n\t1. View all band entries.",
- "\t2. Search for a specific band by name or total votes.",
- "\t4. Exit."
- };
- Console.Clear();
- for (int i = 0; i < menu.Length; i++)
- Console.WriteLine(menu[i]);
- mc = ParseInteger("\nSelection: ");
- switch (mc)
- {
- case 1:
- // View all records.
- DataInterface(-1);
- break;
- case 2:
- // Search for a record.
- SearchInterface();
- break;
- default:
- break;
- }
- }
- }
- // Check if the file exists.
- static int ChkFile(string path)
- {
- // I will expand on this later. Right now I just want to gracefully fail if the file isn't there.
- int mc = 42;
- if (!File.Exists(path))
- {
- mc = 4;
- ChkFileErr();
- }
- return mc;
- }
- // Parse input.
- static int ParseInteger(string query)
- {
- bool parse = false;
- int parsedInt = 0;
- do
- {
- Console.Write(query);
- parse = Int32.TryParse(Console.ReadLine(), out parsedInt);
- if (!parse)
- ParseErr();
- } while(!parse);
- return parsedInt;
- }
- // Load a file.
- static string[] LoadDataFile(string path)
- {
- // This doesn't *have* to be a standalone method, but doing this
- // leaves me open for future improvements.
- string[] data = File.ReadAllLines(path);
- return data;
- }
- // Viewer.
- static void DataInterface(int rec)
- {
- // Grab data.
- string[] data = LoadDataFile(path);
- Console.Clear();
- // Print all lines.
- if (rec == -1)
- {
- PrintHeader();
- for (int i = 0; i < data.Length; i++)
- PrintRecord(data[i]);
- PrintAverageVotes();
- PrintTopBand();
- }
- // Print a specific line.
- else if (rec >= 0)
- {
- PrintHeader();
- PrintRecord(data[rec]);
- }
- GoBack();
- }
- // These next methods handle one specific aspect of printing.
- // These are "quality of life" methods.
- static void PrintHeader()
- {
- int cols = CountCols(path);
- // Formatting material.
- string[] header = { "Band name:", "Total votes:", "Star rating:" };
- string divider = "---------------";
- // Header.
- for (int i = 0; i < cols; i++)
- Console.Write(String.Format(spacer, header[i]));
- Console.WriteLine();
- for (int i = 0; i < cols; i++)
- Console.Write(String.Format(spacer, divider));
- Console.WriteLine();
- }
- // Print the specified line.
- static void PrintRecord(string line)
- {
- // Take one line in, split it, print it.
- string[] row = line.Split(sep);
- int cols = CountCols(path);
- for (int i = 1; i < cols; i++)
- Console.Write(String.Format(spacer, row[i]));
- Console.Write(String.Format(spacer, BandStars(row[2])));
- Console.WriteLine();
- }
- // Print /only/ the band name.
- static string PrintBandName(string line)
- {
- string[] row = line.Split(sep);
- string bandName = row[1];
- return bandName;
- }
- // Print /only/ the band's votes.
- static string PrintBandVotes(string line)
- {
- string[] row = line.Split(sep);
- string bandVotes = row[2];
- return bandVotes;
- }
- static void PrintAverageVotes()
- {
- Console.WriteLine("\nAverage votes per band: {0}", CalculateAverageVotes());
- }
- //Search.
- static int Search(string query)
- {
- string[] data = LoadDataFile(path);
- int rec = 0;
- int sentinel = -1;
- do
- {
- // sentinel changes to -1 if string isn't matched.
- sentinel = data[rec].IndexOf(query);
- if ( sentinel >= 0 )
- break;
- rec++;
- } while (rec < data.Length);
- // 0 or above: found.
- // -1 not found.
- if (sentinel < 0)
- rec = -1;
- return rec;
- }
- static void SearchInterface()
- {
- string query = null;
- int record;
- Console.Clear();
- Console.Write("String to search for (case-sensitive): ");
- query = Convert.ToString(Console.ReadLine());
- record = Search(query);
- if (record < 0)
- StringNotMatched();
- else if (record >= 0)
- StringMatched(record);
- }
- static void StringNotMatched()
- {
- Console.WriteLine("\nString not matched.");
- GoBack();
- }
- static void StringMatched(int rec)
- {
- Console.WriteLine("\nString matched!");
- DataInterface(rec);
- }
- static void PrintTopBand()
- {
- Console.WriteLine("\nTop band (most votes): {0}", TopBand());
- }
- static string TopBand()
- {
- string[] data = LoadDataFile(path);
- string[] names = new string[data.Length];
- int[] votes = new int[data.Length];
- string[] line;
- int topVotes;
- string topBand;
- string topBandQuery;
- for (int i = 0; i < data.Length; i++)
- {
- // 1. Split the record into votes and names.
- line = data[i].Split(sep);
- names[i] = line[1];
- votes[i] = Convert.ToInt32(line[2]);
- }
- // 2. Find the largest number in the votes array.
- topVotes = votes.Max();
- // 3. Convert this number back into a string.
- topBandQuery = Convert.ToString(topVotes);
- // 4. Search for this string in the main array.
- topVotes = Search(topBandQuery);
- // 5. With our element number in hand, we use it to recover the name of the band.
- topBand = names[topVotes];
- return topBand;
- }
- // Vote "stars".
- static string BandStars(string votes)
- {
- int voteCnt = Convert.ToInt32(votes);
- string[] stars = {"*","**","***","****"};
- string voteStars = "";
- if (voteCnt < 100)
- voteStars = stars[0];
- else if ((voteCnt >= 100) && (voteCnt <= 299))
- voteStars = stars[1];
- else if ((voteCnt >= 300) && (voteCnt <= 499))
- voteStars = stars[2];
- else if (voteCnt > 499)
- voteStars = stars[3];
- return voteStars;
- }
- // Average votes.
- static double CalculateAverageVotes()
- {
- string[] data = LoadDataFile(path);
- double[] num = new double[data.Length];
- double average = 0;
- string[] record;
- for (int i = 0; i < (data.Length - 1); i++)
- {
- record = data[i].Split(sep);
- num[i] = Convert.ToDouble(record[2]);
- }
- average = num.Average();
- return average;
- }
- // Enumerate fields in a row.
- static int CountCols(string path)
- {
- string[] data = LoadDataFile(path);
- string[] line = data[0].Split(sep);
- int fields = line.Length;
- return fields;
- }
- // Go back.
- static void GoBack()
- {
- Console.WriteLine("\nPress [RETURN] to continue. ");
- Console.ReadLine();
- }
- // +++++ERRORS++++
- static void ParseErr()
- {
- Console.WriteLine("\nPlease input a valid number!");
- }
- static void ChkFileErr()
- {
- Console.WriteLine("\nFatal error: File {0} not found!\n", path);
- }
- static void SearchErr()
- {
- Console.WriteLine("\nString not matched!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment