private void SearchforPayRate() { try { if (Check_TextBox(wages_Employement_Hours.Text, 1, "Hours") && Check_TextBox(wages_Employement_Index.Text, 3, "Employee ID")) //call Check_TextBox to check whether context which user enters is valid. { Waga_Tax Rate_Data = new Waga_Tax(); SearchRateInFile(); //loading from Data File string em_id = wages_Employement_Index.Text; //get what index user enters. if (Rate_Data.CheckID(em_id)) //if the index has existed in the data file. Get the data from the file to finish it. { decimal rate_num = Convert.ToDecimal(Rate_Data.GetRateFromDictionary(em_id)); decimal hours_num = Convert.ToDecimal(wages_Employement_Hours.Text); decimal waga_num = Convert.ToDecimal(wages_Employement_Hours.Text) * rate_num; showResult_Text.Text = ""; showResult_Text.AppendText($"Employee {em_id} Wage Details:" + Environment.NewLine); showResult_Text.AppendText($"Pay rate is ${rate_num}" + Environment.NewLine); showResult_Text.AppendText($"Working hours is {hours_num}" + Environment.NewLine); showResult_Text.AppendText($"Wage is ${waga_num}"); } else { MessageBox.Show($"Sorry,Can not find the ID."); } } } catch (Exception e) { MessageBox.Show($"Sorry,seems something wrong:{Environment.NewLine}{e}", "Error"); } }
private void AddToList() { try { PersonData person = new PersonData(); //Declare an object from PersonData, in terms of PersonData Class, it could be store what user enters into TextBox Control and offer some methods. person.setPersonDetails(getUI_PersonDetails()); //Get what user enters from UI and store it into properties of Class. if (Check_TextBox(person.index, 3, "Employee ID") && Check_TextBox(person.name, 0, "Name") && Check_TextBox(person.payrate, 1, "Pay rate") && Check_TextBox(person.email, 2, "Email")) //Check context what user enters is vaild through Check_TextBox method. { string[] person_desc = { person.index, person.name, person.payrate, person.email }; //Declare an array as a value for add or change data to List . PersonList list = new PersonList(); //Declare an object from PersonList Class,this class offer some methods to control List in UI and store some global variables Waga_Tax Rate_Data = new Waga_Tax(); //Declare an object from Wage_Tax Class,this class offer some methods to set and get value in its properties and store some global variables. if (!list.Check_Key(person.index)) //According to Employment ID to decide whether to add new data to list or only change an existing data { list.Add_NewDataArrary(person.index, person_desc); // Add new data to List array. employee_list.ItemsSource = list.getArrayForList(); //Update data for the list through set the ItemSource of the list. //******Part 2 solution****** Rate_Data.AddRateToDictionary(person.index, person.payrate); //Add new rate into Dictionary. } else { list.Change_Data(person.index, person_desc); employee_list.ItemsSource = list.getArrayForList(); //******Part 2 solution****** Rate_Data.ChangeRateToDictionary(person.index, person.payrate); //Change new rate into Dictionary. } MessageBox.Show("Success!", "Great!"); } } catch (Exception e) { MessageBox.Show($"Sorry,seems something wrong:{Environment.NewLine}{e}", "Error"); } }
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"); } }
public void SaveDataFile() { try { StreamReader ReadFile = new StreamReader("PersonnelFile.txt"); // Declare an object for read text file. List <string> Index_List = new List <string>(); List <string> Value_List = new List <string>(); //Declare two lists to store data. //Index_List is used to get whether the data which user want to store has existed in the file. //Value_List is used to store the data of specific Index. PersonList list = new PersonList(); while (!ReadFile.EndOfStream)//read every single line to get data and store them into Index_List and Value_List. { string x = ReadFile.ReadLine(); Index_List.Add(list.getIndexFromString(x)); //list.getIndexFromString(x) is a method which PersonList offerd,it could analysis and return the Index according to a whole string data. Value_List.Add(x); } ReadFile.Close(); foreach (string s in list.getArrayForList()) // Traverse existing data of the list. { string indexList_String = list.getIndexFromString(s); //get its index string of the single line if (!Index_List.Contains(indexList_String)) //if this a new data,just add it to the file. { StreamWriter WriteFile = new StreamWriter("PersonnelFile.txt", true); WriteFile.AutoFlush = true; WriteFile.WriteLine(s); WriteFile.Close(); } else //if this an old data, we need to replace it by new data.. { string story = File.ReadAllText("PersonnelFile.txt"); //Declare a variable("story") to store the whole existing data. string OldStr = Value_List[Index_List.IndexOf(indexList_String)]; //pick Oldstr up the to prepare to replace it. story = story.Replace(OldStr, s); //replace OldStr by s(new data) System.IO.File.WriteAllText(@"PersonnelFile.txt", string.Empty); //Empty the whole old data in the file. StreamWriter WriteFile = new StreamWriter("PersonnelFile.txt", true); WriteFile.AutoFlush = true; WriteFile.Write(story); //Write the whole new data in the file WriteFile.Close(); } Waga_Tax Rate_Data = new Waga_Tax(); //Store the new data to Waga_Tax class since it needs these data to Check exist data. //The benefit is does not have to read Data File every time when we want to get every person rate. Rate_Data.AddRateToDictionary(s, Rate_Data.GetRateFromString(indexList_String, s)); } MessageBox.Show("Success!"); } catch (Exception e) { MessageBox.Show($"Sorry,seems something wrong:{Environment.NewLine}{e}", "Error"); } }