protected void submitBtn_Click(object sender, EventArgs e) { Country newCountry = new Country() { Name = countryTxtBox.Text, Capital = capitalTxtBox.Text, Population = long.Parse(populationTxtBox.Text) }; using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["FilePath"], true)) { writer.WriteLine(newCountry); } }
protected void addButton_Click(object sender, EventArgs e) { var addedCountry = new Country( this.capitalTextBox.Text, this.countryTextBox.Text, int.Parse(this.populationTextBox.Text)); var currentDatabase = Controller.SearchByPopulation(); var countryListDisplay = new List<Country>(); foreach (var country in currentDatabase) { countryListDisplay.Add(country); } try { for (int i = 0; i < countryListDisplay.Count; i++) { foreach (var item in countryListDisplay) { if(this.countryTextBox.Text == item.theCountry) { throw new Exception(); } } } if (Controller.addCountry(addedCountry)) { this.capitalTextBox.Text = string.Empty; this.countryTextBox.Text = string.Empty; this.populationTextBox.Text = string.Empty; this.MessageMessenger.Text = "Success!"; } else { this.capitalTextBox.Text = string.Empty; this.countryTextBox.Text = string.Empty; this.populationTextBox.Text = string.Empty; this.MessageMessenger.ForeColor = System.Drawing.Color.Red; this.MessageMessenger.Text = "Failure!"; } } catch(Exception) { this.MessageMessenger.Text = "Invalid input!"; } }
public static bool addCountry(Country addedCountry) { try { using (var Writer = new StreamWriter(Controller.FilePathing, true)) { Writer.WriteLine(addedCountry); } return true; } catch (Exception) { return false; } }
public static List<Country> SearchByPopulation() { var countriesList = new List<Country>(); using (var Reader = new StreamReader(Controller.FilePathing, true)) { var currentRow = Reader.ReadLine(); while(currentRow != null) { var currentCountry = currentRow.Split('|'); var indCountry = new Country( currentCountry[0].Trim(), currentCountry[1].Trim(), int.Parse(currentCountry[2].Trim())); countriesList.Add(indCountry); currentRow = Reader.ReadLine(); } } return countriesList; }