bhalash

Submitted Exam Code

May 15th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 KB | None | 0 0
  1. /*
  2.  *  Date: 2012-05-12
  3.  *  Author: Mark Grealish ([email protected])
  4.  *  Description: A "battle of the bands" CSV parser.
  5.  */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.IO;
  12.  
  13. namespace ConsoleApplication1
  14. {
  15.     class Program
  16.     {
  17.         // Path to file. Easier to make it global.
  18.         static private string path = "votes.txt";
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             // Menu choice.
  23.             int mc = 42;
  24.  
  25.             do
  26.             {
  27.                 Console.Clear();
  28.                 Console.WriteLine("Menu Choice:");
  29.                 Console.WriteLine();
  30.                 Console.WriteLine("1. Generate report.");
  31.                 Console.WriteLine("2. Search for a band.");
  32.                 Console.WriteLine("4. Exit.");
  33.                 Console.WriteLine();
  34.                 mc = intParse("Selection: ");
  35.  
  36.                 switch (mc)
  37.                 {
  38.                     case 1:
  39.                         csvView(-1);
  40.                         break;
  41.                     case 2:
  42.                         // TODO :[
  43.                         break;
  44.                     case 4:
  45.                         break;
  46.                     default:
  47.                         break;
  48.                 }
  49.             } while(mc != 4);
  50.         }
  51.  
  52.         private static void csvView(int rec)
  53.         {
  54.             // Grab our parsed data.
  55.             string[] data   = csvParse(path);
  56.             // Formatting data.
  57.             string[] header = { "Band name:", "Total votes:", "Star rating:" };
  58.             string divider  = "------------";
  59.             string format   = "{0,-20}";
  60.  
  61.             string[] record;
  62.  
  63.             Console.Clear();
  64.             // Header.
  65.             for (int i = 0; i < header.Length; i++)
  66.                 Console.Write(String.Format(format, header[i]));
  67.             Console.WriteLine();
  68.             // Divider.
  69.             for (int i = 0; i < header.Length; i++)
  70.                 Console.Write(String.Format(format, divider));
  71.             Console.WriteLine();
  72.  
  73.             if (rec < 0)
  74.             {
  75.                 // Body.
  76.                 for (int i = 0; i < (data.Length - 1); i++)
  77.                 {
  78.                     record = data[i].Split(',');
  79.  
  80.                     for (int j = 1; j < record.Length; j++)
  81.                     {
  82.                         Console.Write(String.Format(format, record[j]));
  83.                     }
  84.  
  85.                     Console.Write(String.Format(format, bandVotes(record[2])));
  86.                     Console.WriteLine();
  87.                 }
  88.             }
  89.             else if (rec <= 0)
  90.             {
  91.                 record = data[rec].Split(',');
  92.                 for (int i = 1; i < record.Length; i++)
  93.                     Console.Write(String.Format(format, record[i]));
  94.             }
  95.  
  96.             // Output average votes.
  97.             averageVotes();
  98.  
  99.             Console.WriteLine();
  100.             Console.Write("Press [RETURN] to return to the main menu. ");
  101.             Console.ReadLine();
  102.         }
  103.  
  104.         // Votes.
  105.         private static string bandVotes(string inVotes)
  106.         {
  107.             int votes           = Convert.ToInt32(inVotes);
  108.             string[] stars      = {"*","**","***","****"};
  109.             string voteStars    = "";
  110.  
  111.             if (votes < 100)
  112.                 voteStars = stars[0];
  113.             else if ((votes >= 100) && (votes <= 299))
  114.                 voteStars = stars[1];
  115.             else if ((votes >= 300) && (votes <= 499))
  116.                 voteStars = stars[2];
  117.             else if (votes > 499)
  118.                 voteStars = stars[3];
  119.  
  120.             return voteStars;
  121.         }
  122.  
  123.         // Average votes.
  124.         private static void averageVotes()
  125.         {
  126.             string[] data       = csvParse(path);
  127.             double[] numbers    = new double[data.Length];
  128.             double average      = 0;
  129.             string[] record;
  130.             string format       = "{0,-20}{1,-20}";
  131.  
  132.             for (int i = 0; i < (data.Length - 1); i++)
  133.             {
  134.                 record = data[i].Split(',');
  135.                 numbers[i] = Convert.ToDouble(record[2]);
  136.             }
  137.  
  138.             average = numbers.Average();
  139.  
  140.             Console.WriteLine();
  141.             Console.Write(String.Format(format, "Average votes:", average));
  142.             Console.WriteLine();
  143.         }
  144.  
  145.         // Parse the data from the file and into an array.
  146.         private static string[] csvParse(string path)
  147.         {
  148.             // Temp list.
  149.             List<string> temp   = new List<string>();
  150.             string line         = "";
  151.  
  152.             using (StreamReader voteData = new StreamReader(path))
  153.             {
  154.                 while (line != null)
  155.                 {
  156.                     // Read the file one line at a time into the list.
  157.                     line = voteData.ReadLine();
  158.                     temp.Add(line);
  159.                 }
  160.             }
  161.  
  162.             // Convert the list to an array so I can use it elsewhere.
  163.             string[] parsedVoteData = temp.ToArray();
  164.  
  165.             // And then home.
  166.             return parsedVoteData;
  167.         }
  168.  
  169.         // Parse variable input.
  170.         private static int intParse(string userAsk)
  171.         {
  172.             string userInput;
  173.             int number = 0;
  174.             bool parsed;
  175.  
  176.             do
  177.             {
  178.                 // Ask the user for input.
  179.                 Console.Write(userAsk);
  180.                 // Read it.
  181.                 userInput = Console.ReadLine();
  182.                 // Parse it.
  183.                 parsed = Int32.TryParse(userInput, out number);
  184.  
  185.                 if (!parsed)
  186.                 {
  187.                     // If it doesn't parse, throw out an error.
  188.                     Console.WriteLine();
  189.                     Console.WriteLine("Please enter a valid number!");
  190.                     Console.WriteLine();
  191.                 }
  192.             // Continue to do this while input doesn't parse.
  193.             } while (!parsed);
  194.  
  195.             // Return parsed number.
  196.             return number;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment