private void populateList() { countryData.Clear(); filteredTree = new CountryTree(); CountryTree tradePartnerTree = new CountryTree(); if (tradePartnerFiltering) unfilteredTree.TradePartnersOf(unfilteredTree.GetItem( unfilteredTree.GetCountryByName(partnersOf.Text)), ref tradePartnerTree); else tradePartnerTree = unfilteredTree; if (keywordFiltering) tradePartnerTree.SearchByKeyword(filter.Text.Trim().ToLower(), ref filteredTree); else filteredTree = tradePartnerTree; filteredTree.Reset(); foreach (Country c in filteredTree) if (c != null) countryData.Rows.Add(c.Name, c.GdpGrowth, c.Inflation, c.TradeBalance, c.HdiRanking, c.GetTradePartnersAsString()); countryGrid.Update(); }
public void init() { LinkedList <string> partners = null; CT = new CountryTree(); for (int i = 0; i < 15; i++) { Country country = new Country("demoCountry" + i, 0, 0, 0, 0, partners); CT.InsertItem(country); } }
//<<ADDED ERROR HANDLING (TRY/CATCH) FOR SUMMATIVE>> public static bool ParseCountriesIntoTree(string filePath, ref CountryTree tree) { try { String[] lines = new string[LINE_MAX]; lines = File.ReadAllLines(filePath); foreach (string line in lines) { if (line.StartsWith(COUNTRY)) //ignore header { if (line.Split(',').Length != 6) { return false; } } else { LinkedList<string> partnerNames = new LinkedList<string>(); string[] columns = line.Split(','); string[] partners = columns[PARTNERS_INDEX].Split(';', '[', ']'); foreach (string p in partners) { if (p.Length > 1) { partnerNames.AddLast(p); } } tree.InsertItem(new Country(columns[NAME_INDEX], Double.Parse(columns[GDP_INDEX]), Double.Parse(columns[INFLATION_INDEX]), Double.Parse(columns[TRADE_INDEX]), int.Parse(columns[HDI_INDEX]), partnerNames)); } } return true; } catch (Exception e) { Console.WriteLine(e.StackTrace); return false; } }
private void MainView_Load(object sender, EventArgs e) { countryData = new DataTable(); countryData.Columns.Add(Country.NAME, typeof(string)); countryData.Columns.Add(Country.GDP_GROWTH, typeof(double)); countryData.Columns.Add(Country.INFLATION, typeof(double)); countryData.Columns.Add(Country.TRADE_BALANCE, typeof(double)); countryData.Columns.Add(Country.HDI_RANKING, typeof(int)); countryData.Columns.Add(Country.TRADE_PARTNERS, typeof(string)); countryGrid.DataSource = countryData; countryGrid.Sort(countryGrid.Columns[0], ListSortDirection.Ascending); partnersOptions = new BindingList<string>(); partnersOf.DataSource = partnersOptions; unfilteredTree = new CountryTree(); showTreeInfo(); }
public static bool WriteCountryFile(string filePath, CountryTree tree) { StreamWriter file = null; try { file = new StreamWriter(filePath, false); file.WriteLine(HEADER); tree.Reset(); foreach (Country c in tree) { StringBuilder partnersSb = new StringBuilder(); partnersSb.Append("["); foreach (string p in c.TradePartners) { partnersSb.Append(p + ";"); } partnersSb.Append("]"); file.WriteLine(String.Format(STRING_FORMAT, c.Name, c.GdpGrowth, c.Inflation, c.TradeBalance, c.HdiRanking, partnersSb.ToString())); } } catch (Exception e) { Console.WriteLine(e.StackTrace); return false; } finally { if (file != null) file.Close(); } return true; }
//File opening <<ADDED FOR SUMMATIVE>> private void openFile_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Country File|*.csv"; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { unfilteredTree = new CountryTree(); bool read = CsvParser.ParseCountriesIntoTree(dialog.FileName, ref unfilteredTree); if (read) { dataStatus.Text = "Data imported from '" + dialog.FileName + "'."; refreshAll(); } else { MessageBox.Show("An error occured while reading the file. Check that the file is formatted correctly and not corrupted."); } } }