/
Form1.cs
235 lines (218 loc) · 7.87 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace InternationalTradingData
{
public partial class Form1 : Form
{
static string[] headers = new string[6];
AVLTree<Country> tree;
LinkedList<Country> countries = new LinkedList<Country>();
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Method to populate the list view, called when the tree is edited or searched
/// </summary>
/// <param name="search">Boolean determines if method called as a search</param>
private void populateListView(Boolean search)
{
listView1.Items.Clear();
if (search == true && CountryinfoCB.Checked == true)
{
countries = tree.search(searchBox.Text.Trim(), false);
}
else if (search == true && tradePartCB.Checked == true)
{
countries = tree.search(searchBox.Text.Trim(), true);
}
else
{
countries = tree.inOrder();
}
foreach (Country c in countries)
{
string tPart = "";
foreach (String s in c.TradePartners)
{
tPart += s + " ";
}
string[] row = {c.Name, c.GDP, c.Inflation, c.TradeBalance, c.HDI, tPart};
ListViewItem lvi = new ListViewItem(row);
listView1.Items.Add(lvi);
}
}
private void treeDepth()
{
int depth = tree.Height();
label3.Text = depth.ToString();
int count = tree.count();
label4.Text = count.ToString();
label6.Text = tree.tradePotential();
}
/// <summary>
/// Load button click
/// Reads csv file and creates country objects to be added to the tree
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
const int MAX_LINES_FILE = 50000;
string[] AllLines = new string[MAX_LINES_FILE];
tree = new AVLTree<Country>();
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "csv files (*.csv)|*.csv";
if (openFile.ShowDialog() == DialogResult.OK)
{
try
{
AllLines = File.ReadAllLines(openFile.FileName);
foreach (string line in AllLines)
{
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
string[] columns = line.Split(',');
string[] partners = columns[5].Split(';', '[', ']');
LinkedList<String> tPartners = new LinkedList<string>();
for (int i = 1; i < partners.Length - 1; i++)
{
tPartners.AddLast(partners[i]);
}
//Creates a new country object and adds to tree
Country country = new Country(columns[0], columns[1], columns[2], columns[3], columns[4], tPartners);
tree.InsertItem(country);
}
}
populateListView(false);
treeDepth();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
/// <summary>
/// Remove button click event
/// Removes the selected item from the tree
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
if (listView1.Items.Count != 0)
{
if (listView1.SelectedItems.Count == 1)
{
String countryName = listView1.SelectedItems[0].Text;
tree.RemoveItem(tree.returnCountry(countryName));
populateListView(false);
treeDepth();
}
else
{
MessageBox.Show("Please select an item to remove", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
MessageBox.Show("Please load data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// Edit Button click event
/// Opens up the Edit form containing the relative country information of the country selected
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
if (listView1.Items.Count != 0)
{
if (listView1.SelectedItems.Count == 1)
{
String countryName = listView1.SelectedItems[0].Text;
editForm eform = new editForm(tree.returnCountry(countryName));
if (eform.ShowDialog() == DialogResult.OK)
{
tree.RemoveItem(tree.returnCountry(countryName));
Country newCountry = eform.getCountry;
tree.InsertItem(newCountry);
}
populateListView(false);
treeDepth();
}
else
{
MessageBox.Show("Please select an item to edit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
MessageBox.Show("Please load data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// Check box click event
/// Repopulates listview appropriately if a search is being conducted
/// </summary>
private void CountryinfoCB_CheckedChanged(object sender, EventArgs e)
{
if (CountryinfoCB.Checked == true)
{
tradePartCB.Checked = false;
if (!string.IsNullOrWhiteSpace(searchBox.Text) && tree != null)
{
populateListView(true);
}
}
else
{
tradePartCB.Checked = true;
}
}
/// <summary>
/// Check box click event
/// Repopulates listview appropriately if a search is being conducted
/// </summary>
private void tradePartCB_CheckedChanged(object sender, EventArgs e)
{
if (tradePartCB.Checked == true)
{
CountryinfoCB.Checked = false;
if (!string.IsNullOrWhiteSpace(searchBox.Text) && tree != null)
{
populateListView(true);
}
}
else
{
CountryinfoCB.Checked = true;
}
}
/// <summary>
/// Search box, if text is entered searches the tree for data containing the text entered
/// </summary>
private void searchBox_TextChanged(object sender, EventArgs e)
{
if (tree != null )
{
if (!string.IsNullOrWhiteSpace(searchBox.Text))
{
populateListView(true);
}
else
{
populateListView(false);
}
}
}
}
}