public static ProductAndGrams ExtendedTextToProductFunction(string s) { var listOfWords = (s.Split(new char[] { ';' } ) ).ToList(); if (listOfWords.Count != 7) { Product product = new Product(); ProductAndGrams pag = new ProductAndGrams(product); return(pag); } else { int grams; Product product = new Product(); product.Name = listOfWords[0]; grams = int.Parse(listOfWords[1]); product.B = float.Parse(listOfWords[2]); product.T = float.Parse(listOfWords[3]); product.W = float.Parse(listOfWords[4]); product.Kcal = (int)float.Parse(listOfWords[5]); product.Barcode = int.Parse(listOfWords[6]); ProductAndGrams pag = new ProductAndGrams(product); pag.grams = grams; return(pag); } }
private void odczytToolStripMenuItem_Click(object sender, EventArgs e) { var fileContent = string.Empty; var filePath = string.Empty; OpenFileDialog openFileDialog = new OpenFileDialog(); string directory = AppDomain.CurrentDomain.BaseDirectory; openFileDialog.InitialDirectory = directory + "odczyt"; openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { listView1.Items.Clear(); listView2.Items.Clear(); listView3.Items.Clear(); listView4.Items.Clear(); listView5.Items.Clear(); CalculateMacros(); filePath = openFileDialog.FileName; var fileStream = openFileDialog.OpenFile(); StreamReader reader = new StreamReader(fileStream); fileContent = reader.ReadToEnd(); var ListOfWords = TextToList.method(fileContent); int i = 1; foreach (var item in ListOfWords) { if (item == "###") { i++; } else { ProductAndGrams ProductAndGrams = ExtendedTextToProduct.ExtendedTextToProductFunction(item); Product product = ProductAndGrams.product; string ConvertedProduct = ProductToText.ConvertProductIntoText(product); AddProductToEspeciallyList(ProductAndGrams.grams, ConvertedProduct, i); CalculateMacros(); } } } else { MessageBox.Show("Wystąpił błąd otwierania pliku"); } }