Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.IO;
- namespace Program4
- {
- class CSV
- {
- private static String[] rows;
- private static String[] elements;
- static void Main(string[] args)
- {
- Console.WindowWidth = 100;//Set the window width/height due to the size of content
- Console.WindowHeight = 50;
- CSV csv = new CSV();
- bool whileTrue = true;
- Directory.SetCurrentDirectory(@"C:\Users\Leif\College\BIT 142 (C#)\WindReports" + "\n");//sets directory for StreamReader
- StreamReader reader = new StreamReader(@"050825_rpts_wind.csv");//CSV file being read from
- StreamReader layout = new StreamReader(@"CSVlayout.txt");//Text file used to display clean format
- Console.WriteLine(Directory.GetCurrentDirectory());//Displays current directory
- string txtlayout = layout.ReadToEnd();
- string line = reader.ReadToEnd();
- rows = line.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);//Used this line.Split method to separate each row properly
- //when displayed after a query
- while (whileTrue)
- {
- Console.WriteLine(txtlayout + Environment.NewLine);
- Console.WriteLine("\t\t\tCSV Wind data for 25 August, 2005\n");
- string userInput;
- int input;
- Console.WriteLine("Please select from the following options: " + Environment.NewLine);
- Console.WriteLine("\t-Press '1' to query by Time\n\t-Press '2' to query Speed");
- Console.WriteLine("\t-Press '3' to query by County\n\t-Press '4' to query by State" + Environment.NewLine);
- userInput = Console.ReadLine();
- input = Convert.ToInt32(userInput);
- Console.WriteLine();
- if (input == 1)
- {
- int startTime;
- int stopTime;
- Console.Write("\nPlease enter a start time: ");
- string sInput = Console.ReadLine();
- startTime = Convert.ToInt32(sInput);
- Console.Write("Please enter a stop time: ");
- string s1Input = Console.ReadLine();
- stopTime = Convert.ToInt32(s1Input);
- Console.WriteLine();
- for (int i = 1; i <= rows.Length - 1; i++)//start "i" at one to skip header row
- {
- elements = rows[i].Split(new char[] { ',' });//separates comma delimited elements of the current row into an array called "elements"
- int timeColumn;//null reference for time
- timeColumn = Convert.ToInt32(elements[0]);//converted first entry elements[] to int time
- if ((timeColumn >= startTime) && (timeColumn <= stopTime))
- {
- Console.WriteLine(" " + rows[0]);
- Console.WriteLine(" -" + rows[i] + Environment.NewLine);//displays reports for start and stop times annotated...Environment.NewLine = \r\n
- }
- }
- Console.WriteLine();
- Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
- string Restart = Console.ReadLine().ToUpper();
- Console.WriteLine();
- while (Restart != "N" && Restart != "Y")
- {
- Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
- Restart = Console.ReadLine().ToUpper();
- }
- if (Restart == "N")
- {
- whileTrue = false;
- }
- }
- if (input == 2)
- {
- int lowerSpeedLimit;
- int speedColumn;
- Console.Write("\nPlease enter a minimum wind desired speed (includes wind reports greater than value entered): ");
- string sInput = Console.ReadLine();
- lowerSpeedLimit = Convert.ToInt32(sInput);
- Console.WriteLine();
- for (int i = 1; i <= rows.Length - 1; i++)
- {
- elements = rows[i].Split(new char[] { ',' });
- bool convert = Int32.TryParse(elements[1], out speedColumn);//bool declaration testing ability to convert to Int32, if able, it converts
- if (convert)
- {
- if (speedColumn >= lowerSpeedLimit)
- {
- Console.WriteLine(" " + rows[0]);
- Console.WriteLine(" -" + rows[i] + Environment.NewLine);
- }
- }
- else
- {
- rows[i] = null;//if unable to convert to Int32 it sets the row to null and does not display
- }
- }
- Console.WriteLine();
- Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
- string Restart = Console.ReadLine().ToUpper();
- Console.WriteLine();
- while (Restart != "N" && Restart != "Y")
- {
- Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
- Restart = Console.ReadLine().ToUpper();
- }
- if (Restart == "N")
- {
- whileTrue = false;
- }
- }
- if (input == 3)
- {
- string county;
- string countyColumn;
- Console.Write("Please enter a county: ");
- county = Console.ReadLine();
- Console.WriteLine();
- for (int i = 1; i <= rows.Length - 1; i++)
- {
- elements = rows[i].Split(new char[] { ',' });
- countyColumn = elements[3];
- if (countyColumn == county.ToUpper())//used ToUpper() to allow user to type in lowercase and still be able to search CSV
- {
- Console.WriteLine(" " + rows[0]);
- Console.WriteLine(" -" + rows[i] + Environment.NewLine);
- }
- }
- Console.WriteLine();
- Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
- string Restart = Console.ReadLine().ToUpper();
- Console.WriteLine();
- while (Restart != "N" && Restart != "Y")
- {
- Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
- Restart = Console.ReadLine().ToUpper();
- }
- if (Restart == "N")
- {
- whileTrue = false;
- }
- }
- if (input == 4)
- {
- string state;
- string stateColumn;
- Console.Write("Please enter a state: ");
- state = Console.ReadLine();
- Console.WriteLine();
- for (int i = 1; i <= rows.Length - 1; i++)
- {
- elements = rows[i].Split(new char[] { ',' });
- stateColumn = elements[4];
- if (stateColumn == state.ToUpper())
- {
- Console.WriteLine(" " + rows[0]);
- Console.WriteLine(" -" + rows[i] + Environment.NewLine);
- }
- }
- Console.WriteLine();
- Console.Write("Would you like to search again (Y/N)? ");//allows for user to repeatedly search
- string Restart = Console.ReadLine().ToUpper();
- Console.WriteLine();
- while(Restart != "N" && Restart != "Y")
- {
- Console.WriteLine("Invalid response. Please answer with either 'Y' or 'N'. ");
- Restart = Console.ReadLine().ToUpper();
- }
- if (Restart == "N")
- {
- whileTrue = false;
- }
- }
- }
- layout.Close();
- reader.Close();
- Console.WriteLine("Press any key to exit the program...");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement