//Read .csv into a list of custom objects for querying and manipulation then return them back to the program public static List <SightingData> ReadSightings(string fileName) { var sightings = new List <SightingData>(); using (var reader = new StreamReader(fileName)) { string line = ""; while ((line = reader.ReadLine()) != null) { var sightingData = new SightingData(); string[] values = line.Split(','); DateTime sightingDate; if (DateTime.TryParse(values[0], out sightingDate)) { sightingData.SightingDate = sightingDate; } sightingData.City = values[1]; sightingData.State = values[2]; sightingData.Country = values[3]; sightingData.Shape = values[4]; sightingData.Duration = values[5]; sightingData.Comments = values[6]; sightingData.DatePosted = values[7]; Double latitude; if (double.TryParse(values[8], out latitude)) { sightingData.Latitude = latitude; } Double longitude; if (double.TryParse(values[9], out longitude)) { sightingData.Longitude = longitude; } sightings.Add(sightingData); } } return(sightings); }
//gives the user an option to add a custom sighting and provide information to the program public static List <SightingData> NewSighting(List <SightingData> fileContents) { Console.WriteLine("Would you like to add a sighting? y/n"); string addSighting = Console.ReadLine().ToLower(); if (addSighting == "y") { // allows for multiple iterations through the loop enabling adding multiple entries at one time bool keepAdding = true; while (keepAdding) { //custom object creation var newSighting = new SightingData(); Console.WriteLine("Please enter a date and time in the format mm/dd/yyyy hh:mm."); DateTime sightingDate; var newSightingDate = Console.ReadLine(); if (DateTime.TryParse(newSightingDate, out sightingDate)) { newSighting.SightingDate = sightingDate; } Console.WriteLine("Please enter a city."); newSighting.City = Console.ReadLine().ToLower(); Console.WriteLine("Please enter a state by two letter abbreviation or if in a non-US country just leave blank."); newSighting.State = Console.ReadLine().ToLower(); Console.WriteLine("Please enter a country. If in US type us or if international use two letter abbreviation."); newSighting.Country = Console.ReadLine().ToLower(); Console.WriteLine("Please enter the UFOs shape."); newSighting.Shape = Console.ReadLine().ToLower(); Console.WriteLine("Please enter the duration of this sighting."); newSighting.Duration = Console.ReadLine().ToLower(); Console.WriteLine("Please enter any comments regarding this sighting."); newSighting.Comments = Console.ReadLine(); Console.WriteLine("Please enter the current date in the format mm/dd/yyyy hh:mm."); newSighting.DatePosted = Console.ReadLine(); Console.WriteLine("If known please enter the latitude of the sighting."); var newLatitude = Console.ReadLine(); Double latitude; if (double.TryParse(newLatitude, out latitude)) { newSighting.Latitude = latitude; } Console.WriteLine("If known please enter the longitude of the sighting."); var newLongitude = Console.ReadLine(); Double longitude; if (double.TryParse(newLongitude, out longitude)) { newSighting.Longitude = longitude; } fileContents.Add(newSighting); Console.WriteLine("Do you have another entry to add? y/n"); string keepGoing = Console.ReadLine().ToLower(); if (keepGoing != "y") { keepAdding = false; } else { continue; } } return(fileContents); } else { return(fileContents); } }