Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Assignment
  13. {
  14. public partial class Form1 : Form
  15. {
  16. AVLTree<Company> avltree = new AVLTree<Company>();
  17.  
  18. LinkedList<Company> allCompaniesList = new LinkedList<Company>();
  19. LinkedList<String> sa = new LinkedList<String>();
  20.  
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. const int MAX_LINES_FILE = 50000;
  25.  
  26. /*partnersTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
  27. partnersTextBox.AutoCompleteMode = AutoCompleteMode.Append; // or whatever you need
  28. this.partnersTextBox.AutoCompleteCustomSource = collection;
  29. */
  30.  
  31. string[] headers = new string[6]; //column headers
  32.  
  33.  
  34.  
  35.  
  36.  
  37. {
  38.  
  39. string[] AllLines = new string[MAX_LINES_FILE];
  40.  
  41. //set a string variable with the location of the data file. Use ReadAllLines to read the data
  42. string path = @"C:\Users\farid\downloads\companies.csv";
  43. AllLines = File.ReadAllLines(path);
  44.  
  45. foreach (var lineItem in AllLines.Where(l => !string.IsNullOrEmpty(l)).Select((v, i) => new { value = v, index = i }))
  46. {
  47. if (lineItem.value.StartsWith("Company")) //found first line - headers
  48. {
  49. headers = lineItem.value.Split(',');
  50. }
  51. else
  52. {
  53. //split data using commas
  54. string[] columns = lineItem.value.Split(',');
  55. //Console.Write(columns[0] + ","); //first string in line;
  56. //Console.Write(columns[1] + ","); //second string in line;
  57. //add similar instructions to read the other strings
  58.  
  59. // read the full set of buyers
  60. string[] buyers = columns[5].Split(new string[] { ",", "[", "]" }, StringSplitOptions.RemoveEmptyEntries)
  61. .Where(s => !string.IsNullOrEmpty(s))
  62. .ToArray();
  63.  
  64. LinkedList<string> ss = new LinkedList<string>(buyers);
  65.  
  66. Company company = new Company(
  67. columns[0],
  68. Int32.Parse(columns[1]),
  69. Int32.Parse(columns[2]),
  70. Int32.Parse(columns[3]),
  71. Int32.Parse(columns[4]),
  72. ss
  73. );
  74.  
  75. insertCompany(company);
  76. }
  77. }
  78.  
  79.  
  80. SyncListBox();
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. }
  88.  
  89. void SyncListBox()
  90. {
  91.  
  92. companyDataListBox.Items.Clear();
  93. avltree.GetAllNodesInOrder(ref allCompaniesList);
  94.  
  95. foreach (var company in allCompaniesList.Where(c => c != null))
  96. {
  97. companyDataListBox.Items.Add(company.ToString());
  98. }
  99.  
  100. heightLabelValue.Text = avltree.Height().ToString();
  101. numberOfCompaniesLabelValue.Text = avltree.Count().ToString();
  102.  
  103. }
  104.  
  105.  
  106. void insertCompany(Company company)
  107. {
  108. avltree.InsertItem(company);
  109. SyncListBox();
  110. }
  111.  
  112. void removeRecords(Company company)
  113. {
  114. avltree.RemoveItem(company);
  115. SyncListBox();
  116. }
  117.  
  118.  
  119. private void removeButton_Click(object sender, EventArgs e)
  120. {
  121. companyNameTextBox.Text = String.Empty;
  122. netIncomeTextBox.Text = String.Empty;
  123. operatingIncomeTextBox.Text = String.Empty;
  124. totalAssetsTextBox.Text = String.Empty;
  125. numberOfEmployeesTextBox.Text = String.Empty;
  126. partnersListbox.Items.Clear();
  127. if (companyDataListBox.SelectedIndex != -1)
  128. {
  129. int i = companyDataListBox.SelectedIndex;
  130. string companyString = companyDataListBox.Items[i].ToString();
  131. Company selectedCompany = Company.fromString(companyString);
  132. Console.WriteLine($"deleting this company:{selectedCompany.ToString()}");
  133. removeRecords(selectedCompany);
  134. }
  135. }
  136.  
  137. private void insertButton_Click(object sender, EventArgs e)
  138. {
  139.  
  140.  
  141. //to do find a way to do the buyers insert
  142. if (companyNameTextBox.Text != "" && netIncomeTextBox.Text != "" && operatingIncomeTextBox.Text != "" && totalAssetsTextBox.Text != "" && numberOfEmployeesTextBox.Text != "")
  143. {
  144. Company insertedCompany = new Company(
  145. companyNameTextBox.Text,
  146. Int32.Parse(netIncomeTextBox.Text),
  147. Int32.Parse(operatingIncomeTextBox.Text),
  148. Int32.Parse(totalAssetsTextBox.Text),
  149. Int32.Parse(numberOfEmployeesTextBox.Text),
  150. sa
  151. );
  152.  
  153. insertCompany(insertedCompany);
  154. }
  155. }
  156.  
  157. private void companyDataListBox_MouseClick(object sender, MouseEventArgs e)
  158. {
  159. sa.Clear();
  160. partnersListbox.Items.Clear();
  161.  
  162.  
  163. int index = companyDataListBox.IndexFromPoint(e.Location);
  164. string companyString = companyDataListBox.Items[index].ToString();
  165. Company selectedCompany = Company.fromString(companyString);
  166. Console.WriteLine($"avl contains it: {avltree.Contains(selectedCompany)}");
  167.  
  168.  
  169. companyNameTextBox.Text = selectedCompany.Name;
  170. netIncomeTextBox.Text = selectedCompany.NetIncome.ToString();
  171. operatingIncomeTextBox.Text = selectedCompany.OperatingIncome.ToString();
  172. totalAssetsTextBox.Text = selectedCompany.TotalIncome.ToString();
  173. numberOfEmployeesTextBox.Text = selectedCompany.NoOfEmployees.ToString();
  174.  
  175. foreach (string buyer in selectedCompany.Buyers.Where(s => !string.IsNullOrEmpty(s)))
  176. {
  177. sa.AddLast(buyer);
  178. partnersListbox.Items.Add(buyer);
  179. }
  180.  
  181. Console.WriteLine("sa ************ after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
  182.  
  183. }
  184.  
  185.  
  186.  
  187. private void updateButton_Click(object sender, EventArgs e)
  188. {
  189.  
  190. //to do find a way to do the buyers insert
  191.  
  192.  
  193. Console.WriteLine(companyDataListBox.SelectedIndex);
  194.  
  195. if (companyDataListBox.SelectedIndex != -1)
  196. {
  197.  
  198. Console.WriteLine("sa ************after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
  199. int index = companyDataListBox.SelectedIndex;
  200.  
  201. string companyString = companyDataListBox.Items[index].ToString();
  202. Console.WriteLine(companyString);
  203. Company oldCompanyValue = Company.fromString(companyString);
  204. Console.WriteLine("NAME IS THIS: "+oldCompanyValue.Name);
  205.  
  206.  
  207.  
  208. Company newCompanyvalue = new Company(
  209. companyNameTextBox.Text,
  210. Int32.Parse(netIncomeTextBox.Text),
  211. Int32.Parse(operatingIncomeTextBox.Text),
  212. Int32.Parse(totalAssetsTextBox.Text),
  213. Int32.Parse(numberOfEmployeesTextBox.Text),
  214. sa
  215. );
  216.  
  217.  
  218. if (avltree.Contains(newCompanyvalue))
  219. {
  220.  
  221. avltree.RemoveItem(oldCompanyValue);
  222. avltree.InsertItem(newCompanyvalue);
  223.  
  224.  
  225. SyncListBox();
  226.  
  227. Console.WriteLine("sa ************after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
  228.  
  229.  
  230. }
  231.  
  232.  
  233. }
  234.  
  235. companyNameTextBox.Text = String.Empty;
  236. netIncomeTextBox.Text = String.Empty;
  237. operatingIncomeTextBox.Text = String.Empty;
  238. totalAssetsTextBox.Text = String.Empty;
  239. numberOfEmployeesTextBox.Text = String.Empty;
  240. partnersListbox.Items.Clear();
  241.  
  242.  
  243. }
  244.  
  245. private void buttonHome_Click(object sender, EventArgs e)
  246. {
  247. SyncListBox();
  248. }
  249.  
  250. private void searchTextBox_TextChanged(object sender, EventArgs e)
  251. {
  252.  
  253. companyDataListBox.Items.Clear();
  254.  
  255.  
  256. Company searchedCompany = new Company(searchTextBox.Text);
  257.  
  258. LinkedList<Company> allMatchedCompanies = avltree.Matches(searchedCompany);
  259.  
  260. if (!string.IsNullOrEmpty(searchTextBox.Text))
  261. {
  262. foreach (var company in allMatchedCompanies)
  263. {
  264. companyDataListBox.Items.Add(company.ToString());
  265. }
  266. }
  267.  
  268. else
  269. {
  270. SyncListBox();
  271. }
  272.  
  273.  
  274. }
  275.  
  276. private void partnersTextBox_TextChanged(object sender, EventArgs e)
  277. {
  278.  
  279.  
  280.  
  281. /*private void companyDataListBox_SelectedValueChanged(object sender, EventArgs e)
  282. {
  283. //int index = this.listBox1.IndexFromPoint(e.Location);
  284.  
  285. }*/
  286. }
  287.  
  288. private void insertPartnerButton_Click(object sender, EventArgs e)
  289. {
  290. sa.AddLast(partnersTextBox.Text);
  291. partnersListbox.Items.Add(partnersTextBox.Text);
  292. Console.WriteLine(String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
  293. }
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement