// Function that modify article price public void ModifyPrice(FileArticle article, double newPrice) { List <FileArticle> articles = GetAllFileArticles(); for (int i = 0; i < articles.Count; i++) { if (articles.ElementAt(i).Name == article.Name) { articles.ElementAt(i).Price = newPrice; } } File.Delete(fileArticles); foreach (var item in articles) { AddArticleToFile(item); } }
// Function that read all articles from Articles.txt file public List <FileArticle> GetAllFileArticles() { List <FileArticle> listOfArticles = new List <FileArticle>(); if (!File.Exists(fileArticles)) { File.Create(fileArticles).Close(); } try { using (StreamReader sr = File.OpenText(fileArticles)) { string line = ""; while ((line = sr.ReadLine()) != null) { char delimiter = ';'; string[] data = line.Split(delimiter); FileArticle newArticle = new FileArticle { Name = data[0], Quantity = Convert.ToInt32(data[1]), Price = Convert.ToDouble(data[2]) }; listOfArticles.Add(newArticle); } } return(listOfArticles); } catch (FileNotFoundException e) { Debug.WriteLine($"The file was not found: '{e}'"); return(null); } catch (IOException e) { Debug.WriteLine($"The file could not be opened: '{e}'"); return(null); } }
// Function that add one article to Articles.txt file public void AddArticleToFile(FileArticle article) { if (!File.Exists(fileArticles)) { File.Create(fileArticles).Close(); } try { using (StreamWriter sw = File.AppendText(fileArticles)) { sw.WriteLine(article.ToString()); } } catch (FileNotFoundException e) { Debug.WriteLine($"The file was not found: '{e}'"); } catch (IOException e) { Debug.WriteLine($"The file could not be opened: '{e}'"); } }