Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Assignment
- {
- public partial class Form1 : Form
- {
- AVLTree<Company> avltree = new AVLTree<Company>();
- LinkedList<Company> allCompaniesList = new LinkedList<Company>();
- LinkedList<String> sa = new LinkedList<String>();
- public Form1()
- {
- InitializeComponent();
- const int MAX_LINES_FILE = 50000;
- /*partnersTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
- partnersTextBox.AutoCompleteMode = AutoCompleteMode.Append; // or whatever you need
- this.partnersTextBox.AutoCompleteCustomSource = collection;
- */
- string[] headers = new string[6]; //column headers
- {
- string[] AllLines = new string[MAX_LINES_FILE];
- //set a string variable with the location of the data file. Use ReadAllLines to read the data
- string path = @"C:\Users\farid\downloads\companies.csv";
- AllLines = File.ReadAllLines(path);
- foreach (var lineItem in AllLines.Where(l => !string.IsNullOrEmpty(l)).Select((v, i) => new { value = v, index = i }))
- {
- if (lineItem.value.StartsWith("Company")) //found first line - headers
- {
- headers = lineItem.value.Split(',');
- }
- else
- {
- //split data using commas
- string[] columns = lineItem.value.Split(',');
- //Console.Write(columns[0] + ","); //first string in line;
- //Console.Write(columns[1] + ","); //second string in line;
- //add similar instructions to read the other strings
- // read the full set of buyers
- string[] buyers = columns[5].Split(new string[] { ",", "[", "]" }, StringSplitOptions.RemoveEmptyEntries)
- .Where(s => !string.IsNullOrEmpty(s))
- .ToArray();
- LinkedList<string> ss = new LinkedList<string>(buyers);
- Company company = new Company(
- columns[0],
- Int32.Parse(columns[1]),
- Int32.Parse(columns[2]),
- Int32.Parse(columns[3]),
- Int32.Parse(columns[4]),
- ss
- );
- insertCompany(company);
- }
- }
- SyncListBox();
- }
- }
- void SyncListBox()
- {
- companyDataListBox.Items.Clear();
- avltree.GetAllNodesInOrder(ref allCompaniesList);
- foreach (var company in allCompaniesList.Where(c => c != null))
- {
- companyDataListBox.Items.Add(company.ToString());
- }
- heightLabelValue.Text = avltree.Height().ToString();
- numberOfCompaniesLabelValue.Text = avltree.Count().ToString();
- }
- void insertCompany(Company company)
- {
- avltree.InsertItem(company);
- SyncListBox();
- }
- void removeRecords(Company company)
- {
- avltree.RemoveItem(company);
- SyncListBox();
- }
- private void removeButton_Click(object sender, EventArgs e)
- {
- companyNameTextBox.Text = String.Empty;
- netIncomeTextBox.Text = String.Empty;
- operatingIncomeTextBox.Text = String.Empty;
- totalAssetsTextBox.Text = String.Empty;
- numberOfEmployeesTextBox.Text = String.Empty;
- partnersListbox.Items.Clear();
- if (companyDataListBox.SelectedIndex != -1)
- {
- int i = companyDataListBox.SelectedIndex;
- string companyString = companyDataListBox.Items[i].ToString();
- Company selectedCompany = Company.fromString(companyString);
- Console.WriteLine($"deleting this company:{selectedCompany.ToString()}");
- removeRecords(selectedCompany);
- }
- }
- private void insertButton_Click(object sender, EventArgs e)
- {
- //to do find a way to do the buyers insert
- if (companyNameTextBox.Text != "" && netIncomeTextBox.Text != "" && operatingIncomeTextBox.Text != "" && totalAssetsTextBox.Text != "" && numberOfEmployeesTextBox.Text != "")
- {
- Company insertedCompany = new Company(
- companyNameTextBox.Text,
- Int32.Parse(netIncomeTextBox.Text),
- Int32.Parse(operatingIncomeTextBox.Text),
- Int32.Parse(totalAssetsTextBox.Text),
- Int32.Parse(numberOfEmployeesTextBox.Text),
- sa
- );
- insertCompany(insertedCompany);
- }
- }
- private void companyDataListBox_MouseClick(object sender, MouseEventArgs e)
- {
- sa.Clear();
- partnersListbox.Items.Clear();
- int index = companyDataListBox.IndexFromPoint(e.Location);
- string companyString = companyDataListBox.Items[index].ToString();
- Company selectedCompany = Company.fromString(companyString);
- Console.WriteLine($"avl contains it: {avltree.Contains(selectedCompany)}");
- companyNameTextBox.Text = selectedCompany.Name;
- netIncomeTextBox.Text = selectedCompany.NetIncome.ToString();
- operatingIncomeTextBox.Text = selectedCompany.OperatingIncome.ToString();
- totalAssetsTextBox.Text = selectedCompany.TotalIncome.ToString();
- numberOfEmployeesTextBox.Text = selectedCompany.NoOfEmployees.ToString();
- foreach (string buyer in selectedCompany.Buyers.Where(s => !string.IsNullOrEmpty(s)))
- {
- sa.AddLast(buyer);
- partnersListbox.Items.Add(buyer);
- }
- Console.WriteLine("sa ************ after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
- }
- private void updateButton_Click(object sender, EventArgs e)
- {
- //to do find a way to do the buyers insert
- Console.WriteLine(companyDataListBox.SelectedIndex);
- if (companyDataListBox.SelectedIndex != -1)
- {
- Console.WriteLine("sa ************after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
- int index = companyDataListBox.SelectedIndex;
- string companyString = companyDataListBox.Items[index].ToString();
- Console.WriteLine(companyString);
- Company oldCompanyValue = Company.fromString(companyString);
- Console.WriteLine("NAME IS THIS: "+oldCompanyValue.Name);
- Company newCompanyvalue = new Company(
- companyNameTextBox.Text,
- Int32.Parse(netIncomeTextBox.Text),
- Int32.Parse(operatingIncomeTextBox.Text),
- Int32.Parse(totalAssetsTextBox.Text),
- Int32.Parse(numberOfEmployeesTextBox.Text),
- sa
- );
- if (avltree.Contains(newCompanyvalue))
- {
- avltree.RemoveItem(oldCompanyValue);
- avltree.InsertItem(newCompanyvalue);
- SyncListBox();
- Console.WriteLine("sa ************after" + String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
- }
- }
- companyNameTextBox.Text = String.Empty;
- netIncomeTextBox.Text = String.Empty;
- operatingIncomeTextBox.Text = String.Empty;
- totalAssetsTextBox.Text = String.Empty;
- numberOfEmployeesTextBox.Text = String.Empty;
- partnersListbox.Items.Clear();
- }
- private void buttonHome_Click(object sender, EventArgs e)
- {
- SyncListBox();
- }
- private void searchTextBox_TextChanged(object sender, EventArgs e)
- {
- companyDataListBox.Items.Clear();
- Company searchedCompany = new Company(searchTextBox.Text);
- LinkedList<Company> allMatchedCompanies = avltree.Matches(searchedCompany);
- if (!string.IsNullOrEmpty(searchTextBox.Text))
- {
- foreach (var company in allMatchedCompanies)
- {
- companyDataListBox.Items.Add(company.ToString());
- }
- }
- else
- {
- SyncListBox();
- }
- }
- private void partnersTextBox_TextChanged(object sender, EventArgs e)
- {
- /*private void companyDataListBox_SelectedValueChanged(object sender, EventArgs e)
- {
- //int index = this.listBox1.IndexFromPoint(e.Location);
- }*/
- }
- private void insertPartnerButton_Click(object sender, EventArgs e)
- {
- sa.AddLast(partnersTextBox.Text);
- partnersListbox.Items.Add(partnersTextBox.Text);
- Console.WriteLine(String.Join(";", sa.Where(s => !string.IsNullOrEmpty(s))));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement