Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Date: 2012-05-12
- * Author: Mark Grealish ([email protected])
- * Description: A "battle of the bands" CSV parser.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace ConsoleApplication1
- {
- class Program
- {
- // Path to file. Easier to make it global.
- static private string path = "votes.txt";
- static void Main(string[] args)
- {
- // Menu choice.
- int mc = 42;
- do
- {
- Console.Clear();
- Console.WriteLine("Menu Choice:");
- Console.WriteLine();
- Console.WriteLine("1. Generate report.");
- Console.WriteLine("2. Search for a band.");
- Console.WriteLine("4. Exit.");
- Console.WriteLine();
- mc = intParse("Selection: ");
- switch (mc)
- {
- case 1:
- csvView(-1);
- break;
- case 2:
- // TODO :[
- break;
- case 4:
- break;
- default:
- break;
- }
- } while(mc != 4);
- }
- private static void csvView(int rec)
- {
- // Grab our parsed data.
- string[] data = csvParse(path);
- // Formatting data.
- string[] header = { "Band name:", "Total votes:", "Star rating:" };
- string divider = "------------";
- string format = "{0,-20}";
- string[] record;
- Console.Clear();
- // Header.
- for (int i = 0; i < header.Length; i++)
- Console.Write(String.Format(format, header[i]));
- Console.WriteLine();
- // Divider.
- for (int i = 0; i < header.Length; i++)
- Console.Write(String.Format(format, divider));
- Console.WriteLine();
- if (rec < 0)
- {
- // Body.
- for (int i = 0; i < (data.Length - 1); i++)
- {
- record = data[i].Split(',');
- for (int j = 1; j < record.Length; j++)
- {
- Console.Write(String.Format(format, record[j]));
- }
- Console.Write(String.Format(format, bandVotes(record[2])));
- Console.WriteLine();
- }
- }
- else if (rec <= 0)
- {
- record = data[rec].Split(',');
- for (int i = 1; i < record.Length; i++)
- Console.Write(String.Format(format, record[i]));
- }
- // Output average votes.
- averageVotes();
- Console.WriteLine();
- Console.Write("Press [RETURN] to return to the main menu. ");
- Console.ReadLine();
- }
- // Votes.
- private static string bandVotes(string inVotes)
- {
- int votes = Convert.ToInt32(inVotes);
- string[] stars = {"*","**","***","****"};
- string voteStars = "";
- if (votes < 100)
- voteStars = stars[0];
- else if ((votes >= 100) && (votes <= 299))
- voteStars = stars[1];
- else if ((votes >= 300) && (votes <= 499))
- voteStars = stars[2];
- else if (votes > 499)
- voteStars = stars[3];
- return voteStars;
- }
- // Average votes.
- private static void averageVotes()
- {
- string[] data = csvParse(path);
- double[] numbers = new double[data.Length];
- double average = 0;
- string[] record;
- string format = "{0,-20}{1,-20}";
- for (int i = 0; i < (data.Length - 1); i++)
- {
- record = data[i].Split(',');
- numbers[i] = Convert.ToDouble(record[2]);
- }
- average = numbers.Average();
- Console.WriteLine();
- Console.Write(String.Format(format, "Average votes:", average));
- Console.WriteLine();
- }
- // Parse the data from the file and into an array.
- private static string[] csvParse(string path)
- {
- // Temp list.
- List<string> temp = new List<string>();
- string line = "";
- using (StreamReader voteData = new StreamReader(path))
- {
- while (line != null)
- {
- // Read the file one line at a time into the list.
- line = voteData.ReadLine();
- temp.Add(line);
- }
- }
- // Convert the list to an array so I can use it elsewhere.
- string[] parsedVoteData = temp.ToArray();
- // And then home.
- return parsedVoteData;
- }
- // Parse variable input.
- private static int intParse(string userAsk)
- {
- string userInput;
- int number = 0;
- bool parsed;
- do
- {
- // Ask the user for input.
- Console.Write(userAsk);
- // Read it.
- userInput = Console.ReadLine();
- // Parse it.
- parsed = Int32.TryParse(userInput, out number);
- if (!parsed)
- {
- // If it doesn't parse, throw out an error.
- Console.WriteLine();
- Console.WriteLine("Please enter a valid number!");
- Console.WriteLine();
- }
- // Continue to do this while input doesn't parse.
- } while (!parsed);
- // Return parsed number.
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment