Esempio n. 1
0
        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");
            }
        }
Esempio n. 2
0
        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");
            }
        }
Esempio n. 3
0
        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");
            }
        }