} //end ClearAndLoad /*********AddFarm************** * Method * - Adds a new farm record to the list of farms. *********************************/ public void AddFarm(string farmName, string farmAddress, string farmCity, string farmState, int farmZip) { FarmRecord record = new FarmRecord(farmName, farmAddress, farmCity, farmState, farmZip); farms.Add(record); } //end AddFarm
/*********ClearAndLoad************** * Method * - Clears stored data and loads data from a file *********************************/ public void ClearAndLoad() { //clears the list if (farms != null) { farms.RemoveRange(0, farms.Count); } try { Console.WriteLine("Accessing file:\n"); var lines = File.ReadAllText(@"./National_On-Farm_Directory_Rev.csv"); //reads data from a file if (lines != null) { var rows = lines.Split('\n'); //splits document at newline string[] inputFields; foreach (var row in rows) { if (row != "") { inputFields = row.Split(','); FarmRecord record = new FarmRecord( inputFields[5], inputFields[1], inputFields[2], inputFields[3], Convert.ToInt32(inputFields[4])); farms.Add(record); } } farms.RemoveAt(0); //Removes file headers from the list } else { Console.WriteLine("File is Empty"); } } catch (IOException) { Console.Error.WriteLine("Cannot Read File"); } //end Catch } //end ClearAndLoad