public void Add_Event() { int key = rnd.Next(1, 4); Product p = null; switch (key) { case 1: p = new Food_product(); break; case 2: p = new Library_Product(); break; case 3: p = new Industrial_product(); break; } Add_element(p); OnCollectionCountChanged(this, new CollectionHandlerEventArgs(this.Name, "Добавлено", p.ToString())); }
private void OpenFileToolStripMenuItem_Click(object sender, EventArgs e) { try { MainField.Items.Clear(); string[] Shop_file = File.ReadAllLines("Shop.txt"); shop.Shop.Clear(); for (int i = 0; i < Shop_file.Length; i++) { string[] s = Shop_file[i].Split(); Product obj = null; if (s[10] == "Коллории:") { if (s[12] == "Свежее") { obj = new Food_product(s[1], s[3], double.Parse(s[5]), int.Parse(s[7]), int.Parse(s[9]), double.Parse(s[11]), true); } else { obj = new Food_product(s[1], s[3], double.Parse(s[5]), int.Parse(s[7]), int.Parse(s[9]), double.Parse(s[11]), false); } } else { if (s[10] == "Год_публикации:") { obj = new Library_Product(s[1], s[3], double.Parse(s[5]), int.Parse(s[7]), double.Parse(s[9]), int.Parse(s[11])); } else { obj = new Industrial_product(s[1], s[3], double.Parse(s[5]), int.Parse(s[7]), double.Parse(s[9]), double.Parse(s[11])); } } shop.Shop.Add(i, obj); } foreach (KeyValuePair <int, Product> p1 in shop.Shop) { MainField.Items.Add(p1.Value.ToString()); } } catch (FileNotFoundException) { MessageBox.Show("Файл со всей информацией удалён"); } }
public MyCollection(int capacity) { for (int i = 0; i < capacity; i++) { Product p = null; switch (rnd.Next(0, 4)) { case 1: p = new Food_product(); break; case 2: p = new Industrial_product(); break; default: p = new Library_Product(); break; } Shop.Add(i, p); } }