private void SearchRateInFile()//this method will get rate from the data file and store it to a specific global Dictionary. { try { StreamReader ReadFile = new StreamReader("PersonnelFile.txt"); Waga_Tax Rate_Data = new Waga_Tax(); while (!ReadFile.EndOfStream) { string x = ReadFile.ReadLine(); string index = Rate_Data.getIndexFromString(x); Rate_Data.AddRateToDictionary(index, Rate_Data.GetRateFromString(index, x)); } ReadFile.Close(); } catch (Exception e) { MessageBox.Show($"Sorry,seems something wrong:{Environment.NewLine}{e}", "Error"); } }
private void OpenSavedFile() { try { StreamReader ReadFile = new StreamReader("PersonnelFile.txt"); PersonList list_data = new PersonList(); employee_list.ItemsSource = list_data.getEmptyArray(); //empty the list for new data list List <string> NewDataList = new List <string>(); //This List is used to store new data come from the data file. //Do not use Array because we do not know how many data it needs to store. while (!ReadFile.EndOfStream) { string data_string = ReadFile.ReadLine(); NewDataList.Add(data_string); } string[] NewData = NewDataList.ToArray(); //convert List to Array, my class method based on Array as a parameter. Waga_Tax Rate_Data = new Waga_Tax(); //As usual,the rate of every person will be stored into a Dictionary of Waga_Tax Class. if (list_data.getCountFromDictionary() > 0) //when the number of the list item is not 0, we need to keep the data which user has already added to list //and add other existing data which is in the data file to list { foreach (string n in list_data.getArrayForList()) // Firstly,Traverse existing data of the list. // { string Index_New = list_data.getIndexFromString(n); foreach (string o in NewData) //Secondly, compare existing data with new data which came from the data file. { string Index_Old = list_data.getIndexFromString(o); //analyze and get the index of existing data through its string. if (Index_New != Index_Old) //when it is not an existing data, add it to Person List. //if it is an existing data,we do not do anything,just keep what user enters. { list_data.Add_NewData(Index_Old, o); Rate_Data.AddRateToDictionary(Index_Old, Rate_Data.GetRateFromString(Index_Old, o)); } } } } else // when the list is empty,we do not have to check data,just add new data to the Dictionary { foreach (string d in NewDataList) { string index = Rate_Data.getIndexFromString(d); list_data.Add_NewData(list_data.getIndexFromString(d), d); Rate_Data.AddRateToDictionary(index, Rate_Data.GetRateFromString(index, d)); } } employee_list.ItemsSource = list_data.getArrayForList(); //updata ItemSource. ReadFile.Close(); MessageBox.Show("Success!"); } catch (Exception e) { MessageBox.Show($"Sorry,seems something wrong:{Environment.NewLine}{e}", "Error"); } }