bhalash

bands.cs (fixed)

May 17th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. class BattleReader
  8. {
  9.  
  10.     // /path/to/file
  11.     static string path      = "bands.txt";
  12.     // Formatting information. Used in more than one method.
  13.     static string spacer    = "{0,-20}";
  14.     // Separator.
  15.     static char sep         = ',';
  16.  
  17.     static void Main ()
  18.     {
  19.         // Menu Choice.
  20.         int mc = ChkFile(path);
  21.  
  22.         while (mc != 4)
  23.         {
  24.             string[] menu = {
  25.                 "Main menu:",
  26.                 "\n\t1. View all band entries.",
  27.                 "\t2. Search for a specific band by name or total votes.",
  28.                 "\t4. Exit."
  29.             };
  30.  
  31.             Console.Clear();
  32.             for (int i = 0; i < menu.Length; i++)
  33.                 Console.WriteLine(menu[i]);
  34.                 mc = ParseInteger("\nSelection: ");
  35.  
  36.             switch (mc)
  37.             {
  38.                 case 1:
  39.                     // View all records.
  40.                     DataInterface(-1);
  41.                     break;
  42.                 case 2:
  43.                     // Search for a record.
  44.                     SearchInterface();
  45.                     break;
  46.                 default:
  47.                     break;
  48.             }
  49.         }
  50.     }
  51.    
  52.     // Check if the file exists.
  53.     static int ChkFile(string path)
  54.     {
  55.         // I will expand on this later. Right now I just want to gracefully fail if the file isn't there.
  56.         int mc = 42;
  57.  
  58.         if (!File.Exists(path))
  59.         {
  60.             mc = 4;
  61.             ChkFileErr();
  62.         }
  63.                        
  64.         return mc;
  65.     }
  66.  
  67.     // Parse input.
  68.     static int ParseInteger(string query)
  69.     {
  70.         bool parse      = false;
  71.         int parsedInt   = 0;
  72.  
  73.         do
  74.         {
  75.             Console.Write(query);
  76.             parse = Int32.TryParse(Console.ReadLine(), out parsedInt);
  77.  
  78.             if (!parse)
  79.                 ParseErr();
  80.  
  81.         } while(!parse);
  82.  
  83.         return parsedInt;
  84.     }
  85.  
  86.     // Load a file.
  87.     static string[] LoadDataFile(string path)
  88.     {
  89.         // This doesn't *have* to be a standalone method, but doing this
  90.         // leaves me open for future improvements.
  91.         string[] data = File.ReadAllLines(path);
  92.         return data;
  93.     }
  94.  
  95.     // Viewer.
  96.     static void DataInterface(int rec)
  97.     {
  98.         // Grab data.
  99.         string[] data   = LoadDataFile(path);
  100.  
  101.         Console.Clear();
  102.  
  103.         // Print all lines.
  104.         if (rec == -1)
  105.         {
  106.             PrintHeader();
  107.             for (int i = 0; i < data.Length; i++)
  108.                 PrintRecord(data[i]);
  109.             PrintAverageVotes();
  110.             PrintTopBand();
  111.         }
  112.         // Print a specific line.
  113.         else if (rec >= 0)
  114.         {
  115.             PrintHeader();
  116.             PrintRecord(data[rec]);
  117.         }
  118.  
  119.         GoBack();
  120.     }
  121.  
  122.     // These next methods handle one specific aspect of printing.
  123.     // These are "quality of life" methods.
  124.     static void PrintHeader()
  125.     {
  126.         int cols        = CountCols(path);
  127.         // Formatting material.
  128.         string[] header = { "Band name:", "Total votes:", "Star rating:" };
  129.         string divider  = "---------------";
  130.  
  131.         // Header.
  132.         for (int i = 0; i < cols; i++)
  133.             Console.Write(String.Format(spacer, header[i]));
  134.             Console.WriteLine();
  135.         for (int i = 0; i < cols; i++)
  136.             Console.Write(String.Format(spacer, divider));
  137.             Console.WriteLine();
  138.     }
  139.  
  140.     // Print the specified line.
  141.     static void PrintRecord(string line)
  142.     {
  143.         // Take one line in, split it, print it.
  144.         string[] row    = line.Split(sep);
  145.         int cols        = CountCols(path);
  146.  
  147.         for (int i = 1; i < cols; i++)
  148.             Console.Write(String.Format(spacer, row[i]));
  149.             Console.Write(String.Format(spacer, BandStars(row[2])));
  150.             Console.WriteLine();
  151.     }
  152.  
  153.     // Print /only/ the band name.
  154.     static string PrintBandName(string line)
  155.     {
  156.         string[] row    = line.Split(sep);
  157.         string bandName = row[1];
  158.         return bandName;
  159.     }
  160.  
  161.     // Print /only/ the band's votes.
  162.     static string PrintBandVotes(string line)
  163.     {
  164.         string[] row    = line.Split(sep);
  165.         string bandVotes = row[2];
  166.         return bandVotes;
  167.     }
  168.  
  169.     static void PrintAverageVotes()
  170.     {
  171.         Console.WriteLine("\nAverage votes per band: {0}", CalculateAverageVotes());
  172.     }
  173.  
  174.     //Search.
  175.     static int Search(string query)
  176.     {
  177.         string[] data   = LoadDataFile(path);      
  178.         int rec     = 0;
  179.         int sentinel    = -1;
  180.  
  181.         do
  182.         {
  183.             // sentinel changes to -1 if string isn't matched.
  184.             sentinel = data[rec].IndexOf(query);
  185.             if ( sentinel >= 0 )
  186.                 break;
  187.             rec++;
  188.  
  189.         } while (rec < data.Length);
  190.    
  191.         // 0 or above: found.
  192.         // -1 not found.
  193.         if (sentinel < 0)
  194.             rec = -1;
  195.  
  196.         return rec;
  197.     }
  198.  
  199.     static void SearchInterface()
  200.     {
  201.         string query = null;
  202.         int record;
  203.  
  204.         Console.Clear();
  205.         Console.Write("String to search for (case-sensitive): ");
  206.             query = Convert.ToString(Console.ReadLine());
  207.  
  208.         record = Search(query);
  209.  
  210.         if (record < 0)
  211.             StringNotMatched();
  212.         else if (record >= 0)
  213.             StringMatched(record);
  214.     }
  215.  
  216.     static void StringNotMatched()
  217.     {
  218.         Console.WriteLine("\nString not matched.");
  219.         GoBack();
  220.     }
  221.  
  222.     static void StringMatched(int rec)
  223.     {
  224.         Console.WriteLine("\nString matched!");
  225.         DataInterface(rec);
  226.     }
  227.    
  228.     static void PrintTopBand()
  229.     {
  230.         Console.WriteLine("\nTop band (most votes): {0}", TopBand());
  231.     }
  232.    
  233.     static string TopBand()
  234.     {
  235.         string[] data   = LoadDataFile(path);
  236.         string[] names  = new string[data.Length];
  237.         int[] votes     = new int[data.Length];
  238.         string[] line;
  239.         int topVotes;
  240.         string topBand;
  241.         string topBandQuery;
  242.    
  243.         for (int i = 0; i < data.Length; i++)
  244.         {
  245.             // 1. Split the record into votes and names.
  246.             line = data[i].Split(sep);
  247.             names[i] = line[1];
  248.             votes[i] = Convert.ToInt32(line[2]);
  249.         }
  250.        
  251.         // 2. Find the largest number in the votes array.
  252.         topVotes = votes.Max();
  253.         // 3. Convert this number back into a string.
  254.         topBandQuery = Convert.ToString(topVotes);
  255.         // 4. Search for this string in the main array.
  256.         topVotes = Search(topBandQuery);
  257.         // 5. With our element number in hand, we use it to recover the name of the band.
  258.         topBand = names[topVotes];
  259.        
  260.         return topBand;
  261.     }
  262.  
  263.     // Vote "stars".
  264.     static string BandStars(string votes)
  265.     {
  266.         int voteCnt      = Convert.ToInt32(votes);
  267.         string[] stars   = {"*","**","***","****"};
  268.         string voteStars = "";
  269.  
  270.         if (voteCnt < 100)
  271.             voteStars = stars[0];
  272.         else if ((voteCnt >= 100) && (voteCnt <= 299))
  273.             voteStars = stars[1];
  274.         else if ((voteCnt >= 300) && (voteCnt <= 499))
  275.             voteStars = stars[2];
  276.         else if (voteCnt > 499)
  277.             voteStars = stars[3];
  278.  
  279.         return voteStars;
  280.     }
  281.  
  282.     // Average votes.
  283.     static double CalculateAverageVotes()
  284.     {
  285.         string[] data   = LoadDataFile(path);
  286.         double[] num    = new double[data.Length];
  287.         double average  = 0;
  288.         string[] record;
  289.  
  290.         for (int i = 0; i < (data.Length - 1); i++)
  291.         {
  292.             record = data[i].Split(sep);
  293.             num[i] = Convert.ToDouble(record[2]);
  294.         }
  295.  
  296.         average = num.Average();
  297.  
  298.         return average;
  299.     }
  300.  
  301.     // Enumerate fields in a row.
  302.     static int CountCols(string path)
  303.     {
  304.         string[] data   = LoadDataFile(path);
  305.         string[] line   = data[0].Split(sep);
  306.         int fields      = line.Length;
  307.         return fields;
  308.     }
  309.  
  310.     // Go back.
  311.     static void GoBack()
  312.     {
  313.         Console.WriteLine("\nPress [RETURN] to continue. ");
  314.         Console.ReadLine();
  315.     }
  316.  
  317.     // +++++ERRORS++++
  318.     static void ParseErr()
  319.     {
  320.         Console.WriteLine("\nPlease input a valid number!");
  321.     }
  322.    
  323.     static void ChkFileErr()
  324.     {
  325.         Console.WriteLine("\nFatal error: File {0} not found!\n", path);
  326.     }
  327.  
  328.     static void SearchErr()
  329.     {
  330.         Console.WriteLine("\nString not matched!");
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment