bhalash

Last version of bands.cs - I give up.

May 18th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 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.     // ++++GLOBAL VARIABLES++++
  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.         string[] data = File.ReadAllLines(path);
  90.         return data;
  91.     }
  92.  
  93.     //Search.
  94.     static int Search(string query)
  95.     {  
  96.         string[] data = LoadDataFile(path);
  97.         int rec     = 0;
  98.         int sentinel    = -1;
  99.  
  100.         do
  101.         {
  102.             sentinel = data[rec].IndexOf(query);
  103.             if ( sentinel >= 0 )
  104.                 break;
  105.             rec++;
  106.  
  107.         } while (rec < data.Length);
  108.  
  109.         if (sentinel < 0)
  110.             rec = -1;
  111.  
  112.         return rec;
  113.     }
  114.  
  115.     // Viewer.
  116.     static void DataInterface(int rec)
  117.     {
  118.         string[] data = LoadDataFile(path);
  119.  
  120.         Console.Clear();
  121.  
  122.         // Print all records.
  123.         if (rec == -1)
  124.         {
  125.             PrintHeader();
  126.             for (int i = 0; i < data.Length; i++)
  127.                 PrintRecord(data[i]);
  128.             PrintAverageVotes();
  129.             PrintTopBand();
  130.         }
  131.         // Print a specific record.
  132.         else if (rec >= 0)
  133.         {
  134.             PrintHeader();
  135.             PrintRecord(data[rec]);
  136.         }
  137.  
  138.         GoBack();
  139.     }
  140.  
  141.     static void SearchInterface()
  142.     {
  143.         string[] data = LoadDataFile(path);
  144.  
  145.         string query = null;
  146.         int record;
  147.  
  148.         Console.Clear();
  149.         Console.Write("String to search for (case-sensitive): ");
  150.             query = Convert.ToString(Console.ReadLine());
  151.  
  152.         record = Search(query);
  153.  
  154.         if (record < 0)
  155.             StringNotMatched();
  156.         else if (record >= 0)
  157.             StringMatched(record);
  158.     }
  159.    
  160.     static string TopBand()
  161.     {
  162.         string[] data = LoadDataFile(path);
  163.         string[] names  = new string[data.Length];
  164.         int[] votes     = new int[data.Length];
  165.  
  166.         string[] record;
  167.         string topBand = "";
  168.         int topVotes;
  169.    
  170.         for (int i = 0; i < data.Length; i++)
  171.         {
  172.             record   = data[i].Split(sep);
  173.             names[i] = record[1];
  174.             votes[i] = Convert.ToInt32(record[2]);
  175.         }
  176.  
  177.         topVotes = votes.Max();
  178.  
  179.         for (int i = 0; i < votes.Length; i++)
  180.         {
  181.             if (votes[i] == topVotes)
  182.             {  
  183.                 topBand = names[i];
  184.                 break;
  185.             }
  186.         }
  187.  
  188.         return topBand;
  189.     }
  190.  
  191.     // Vote "stars".
  192.     static string BandStars(string votes)
  193.     {
  194.         int voteCnt      = Convert.ToInt32(votes);
  195.         string[] stars   = {"*","**","***","****"};
  196.         string voteStars = "";
  197.  
  198.         if (voteCnt < 100)
  199.             voteStars = stars[0];
  200.         else if ((voteCnt >= 100) && (voteCnt <= 299))
  201.             voteStars = stars[1];
  202.         else if ((voteCnt >= 300) && (voteCnt <= 499))
  203.             voteStars = stars[2];
  204.         else if (voteCnt > 499)
  205.             voteStars = stars[3];
  206.  
  207.         return voteStars;
  208.     }
  209.  
  210.     // Average votes.
  211.     static double CalculateAverageVotes()
  212.     {
  213.         string[] data = LoadDataFile(path);
  214.         double[] num    = new double[data.Length];
  215.         double average  = 0;
  216.         string[] record;
  217.  
  218.         for (int i = 0; i < (data.Length - 1); i++)
  219.         {
  220.             record = data[i].Split(sep);
  221.             num[i] = Convert.ToDouble(record[2]);
  222.         }
  223.  
  224.         average = num.Average();
  225.  
  226.         return average;
  227.     }
  228.  
  229.     // Enumerate fields in a row.
  230.     static int CountCols(string path)
  231.     {
  232.         string[] data   = LoadDataFile(path);
  233.         string[] record     = data[0].Split(sep);
  234.         int fields      = record.Length;
  235.         return fields;
  236.     }
  237.  
  238.     // Go back.
  239.     static void GoBack()
  240.     {
  241.         Console.WriteLine("\nPress [RETURN] to continue. ");
  242.         Console.ReadLine();
  243.     }
  244.  
  245.     // ++++PRINTED MESSAGES+++++
  246.         static void PrintTopBand()
  247.     {
  248.         Console.WriteLine("\nTop band (most votes): {0}", TopBand());
  249.     }
  250.    
  251.     static void StringNotMatched()
  252.     {
  253.         Console.WriteLine("\nString not matched.");
  254.         GoBack();
  255.     }
  256.  
  257.     static void StringMatched(int rec)
  258.     {
  259.         Console.WriteLine("\nString matched!");
  260.         DataInterface(rec);
  261.     }
  262.  
  263.         // These next methods handle one specific aspect of printing.
  264.     // These are "quality of life" methods.
  265.     static void PrintHeader()
  266.     {
  267.         int cols        = CountCols(path);
  268.         // Formatting material.
  269.         string[] header = { "Band name:", "Total votes:", "Star rating:" };
  270.         string divider  = "---------------";
  271.  
  272.         // Header.
  273.         for (int i = 0; i < cols; i++)
  274.             Console.Write(String.Format(spacer, header[i]));
  275.             Console.WriteLine();
  276.         for (int i = 0; i < cols; i++)
  277.             Console.Write(String.Format(spacer, divider));
  278.             Console.WriteLine();
  279.     }
  280.  
  281.     // Print the specified record.
  282.     static void PrintRecord(string record)
  283.     {
  284.         // Take one record in, split it, print it.
  285.         string[] row    = record.Split(sep);
  286.         int cols        = CountCols(path);
  287.  
  288.         for (int i = 1; i < cols; i++)
  289.             Console.Write(String.Format(spacer, row[i]));
  290.             Console.Write(String.Format(spacer, BandStars(row[2])));
  291.             Console.WriteLine();
  292.     }
  293.  
  294.     // Print /only/ the band name.
  295.     static string PrintBandName(string record)
  296.     {
  297.         string[] row    = record.Split(sep);
  298.         string bandName = row[1];
  299.         return bandName;
  300.     }
  301.  
  302.     // Print /only/ the band's votes.
  303.     static string PrintBandVotes(string record)
  304.     {
  305.         string[] row    = record.Split(sep);
  306.         string bandVotes = row[2];
  307.         return bandVotes;
  308.     }
  309.  
  310.     static void PrintAverageVotes()
  311.     {
  312.         Console.WriteLine("\nAverage votes per band: {0}", CalculateAverageVotes());
  313.     }
  314.  
  315.     // +++++ERRORS++++
  316.     static void ParseErr()
  317.     {
  318.         Console.WriteLine("\nPlease input a valid number!");
  319.     }
  320.    
  321.     static void ChkFileErr()
  322.     {
  323.         Console.WriteLine("\nFatal error: File {0} not found!\n", path);
  324.     }
  325.  
  326.     static void SearchErr()
  327.     {
  328.         Console.WriteLine("\nString not matched!");
  329.     }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment