Advertisement
ski900

CSVWeather

Oct 6th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Data;
  8. using System.IO;
  9.  
  10. namespace Program4
  11. {
  12.     class CSV
  13.     {  
  14.         private static String[] rows;
  15.         private static String[] elements;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             Console.WindowWidth = 100;//Set the window width/height due to the size of content
  20.             Console.WindowHeight = 50;
  21.            
  22.             CSV csv = new CSV();
  23.             bool whileTrue = true;
  24.  
  25.             Directory.SetCurrentDirectory(@"C:\Users\Leif\College\BIT 142 (C#)\WindReports" + "\n");//sets directory for StreamReader
  26.             StreamReader reader = new StreamReader(@"050825_rpts_wind.csv");//CSV file being read from
  27.             StreamReader layout = new StreamReader(@"CSVlayout.txt");//Text file used to display clean format
  28.             Console.WriteLine(Directory.GetCurrentDirectory());//Displays current directory            
  29.  
  30.             string txtlayout = layout.ReadToEnd();
  31.             string line = reader.ReadToEnd();
  32.  
  33.  
  34.  
  35.             rows = line.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);//Used this line.Split method to separate each row properly
  36.                                                                                                 //when displayed after a query
  37.             while (whileTrue)
  38.             {
  39.                 Console.WriteLine(txtlayout + Environment.NewLine);
  40.                 Console.WriteLine("\t\t\tCSV Wind data for 25 August, 2005\n");
  41.  
  42.                 string userInput;
  43.                 int input;
  44.  
  45.                 Console.WriteLine("Please select from the following options: " + Environment.NewLine);
  46.                 Console.WriteLine("\t-Press '1' to query by Time\n\t-Press '2' to query Speed");
  47.                 Console.WriteLine("\t-Press '3' to query by County\n\t-Press '4' to query by State" + Environment.NewLine);
  48.                 userInput = Console.ReadLine();
  49.                 input = Convert.ToInt32(userInput);
  50.                 Console.WriteLine();
  51.  
  52.                 if (input == 1)
  53.                 {
  54.                     int startTime;
  55.                     int stopTime;
  56.  
  57.                     Console.Write("\nPlease enter a start time: ");
  58.                     string sInput = Console.ReadLine();
  59.                     startTime = Convert.ToInt32(sInput);
  60.  
  61.                     Console.Write("Please enter a stop time: ");
  62.                     string s1Input = Console.ReadLine();
  63.                     stopTime = Convert.ToInt32(s1Input);
  64.                     Console.WriteLine();
  65.  
  66.                     for (int i = 1; i <= rows.Length - 1; i++)//start "i" at one to skip header row
  67.                     {
  68.                         elements = rows[i].Split(new char[] { ',' });//separates comma delimited elements of the current row into an array called "elements"
  69.                         int timeColumn;//null reference for time
  70.                         timeColumn = Convert.ToInt32(elements[0]);//converted first entry elements[] to int time
  71.  
  72.                         if ((timeColumn >= startTime) && (timeColumn <= stopTime))
  73.                         {
  74.                             Console.WriteLine("   " + rows[0]);
  75.                             Console.WriteLine("  -" + rows[i] + Environment.NewLine);//displays reports for start and stop times annotated...Environment.NewLine = \r\n                                                  
  76.                         }
  77.                     }
  78.  
  79.                     Console.WriteLine();
  80.                     Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
  81.                     string Restart = Console.ReadLine().ToUpper();
  82.                     Console.WriteLine();
  83.  
  84.                     while (Restart != "N" && Restart != "Y")
  85.                     {
  86.                         Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
  87.                         Restart = Console.ReadLine().ToUpper();
  88.                     }
  89.                     if (Restart == "N")
  90.                     {
  91.                         whileTrue = false;
  92.                     }
  93.                 }
  94.  
  95.  
  96.                 if (input == 2)
  97.                 {
  98.                     int lowerSpeedLimit;
  99.                     int speedColumn;
  100.  
  101.                     Console.Write("\nPlease enter a minimum wind desired speed (includes wind reports greater than value entered): ");
  102.                     string sInput = Console.ReadLine();
  103.                     lowerSpeedLimit = Convert.ToInt32(sInput);
  104.                     Console.WriteLine();
  105.  
  106.                     for (int i = 1; i <= rows.Length - 1; i++)
  107.                     {
  108.                         elements = rows[i].Split(new char[] { ',' });
  109.  
  110.                         bool convert = Int32.TryParse(elements[1], out speedColumn);//bool declaration testing ability to convert to Int32, if able, it converts
  111.  
  112.                         if (convert)
  113.                         {
  114.                             if (speedColumn >= lowerSpeedLimit)
  115.                             {
  116.                                 Console.WriteLine("   " + rows[0]);
  117.                                 Console.WriteLine("  -" + rows[i] + Environment.NewLine);
  118.                             }
  119.                         }
  120.                         else
  121.                         {
  122.                             rows[i] = null;//if unable to convert to Int32 it sets the row to null and does not display
  123.                         }
  124.                     }
  125.  
  126.                     Console.WriteLine();
  127.                     Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
  128.                     string Restart = Console.ReadLine().ToUpper();
  129.                     Console.WriteLine();
  130.  
  131.                     while (Restart != "N" && Restart != "Y")
  132.                     {
  133.                         Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
  134.                         Restart = Console.ReadLine().ToUpper();
  135.                     }
  136.                     if (Restart == "N")
  137.                     {
  138.                         whileTrue = false;
  139.                     }
  140.                 }
  141.  
  142.                 if (input == 3)
  143.                 {
  144.                     string county;
  145.                     string countyColumn;
  146.  
  147.                     Console.Write("Please enter a county: ");
  148.                     county = Console.ReadLine();
  149.                     Console.WriteLine();
  150.  
  151.                     for (int i = 1; i <= rows.Length - 1; i++)
  152.                     {
  153.                         elements = rows[i].Split(new char[] { ',' });
  154.                         countyColumn = elements[3];
  155.  
  156.                         if (countyColumn == county.ToUpper())//used ToUpper() to allow user to type in lowercase and still be able to search CSV
  157.                         {
  158.                             Console.WriteLine("   " + rows[0]);
  159.                             Console.WriteLine("  -" + rows[i] + Environment.NewLine);
  160.                         }
  161.                     }
  162.  
  163.                     Console.WriteLine();
  164.                     Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
  165.                     string Restart = Console.ReadLine().ToUpper();
  166.                     Console.WriteLine();
  167.  
  168.                     while (Restart != "N" && Restart != "Y")
  169.                     {
  170.                         Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
  171.                         Restart = Console.ReadLine().ToUpper();
  172.                     }
  173.                     if (Restart == "N")
  174.                     {
  175.                         whileTrue = false;
  176.                     }
  177.                 }
  178.  
  179.                 if (input == 4)
  180.                 {
  181.                     string state;
  182.                     string stateColumn;
  183.  
  184.                     Console.Write("Please enter a state: ");
  185.                     state = Console.ReadLine();
  186.                     Console.WriteLine();
  187.  
  188.                     for (int i = 1; i <= rows.Length - 1; i++)
  189.                     {
  190.                         elements = rows[i].Split(new char[] { ',' });
  191.                         stateColumn = elements[4];
  192.  
  193.                         if (stateColumn == state.ToUpper())
  194.                         {
  195.                             Console.WriteLine("   " + rows[0]);
  196.                             Console.WriteLine("  -" + rows[i] + Environment.NewLine);
  197.                         }
  198.                     }
  199.  
  200.                     Console.WriteLine();
  201.                     Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
  202.                     string Restart = Console.ReadLine().ToUpper();
  203.                     Console.WriteLine();
  204.  
  205.                     while(Restart != "N" && Restart != "Y")
  206.                     {
  207.                         Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
  208.                         Restart = Console.ReadLine().ToUpper();
  209.                     }
  210.                     if (Restart == "N")
  211.                     {
  212.                         whileTrue = false;
  213.                     }                    
  214.                 }                    
  215.             }
  216.            
  217.             layout.Close();
  218.             reader.Close();
  219.             Console.WriteLine("Press any key to exit the program...");
  220.             Console.ReadKey();
  221.         }        
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement