public Gift Read() { Gift G = new Gift(); string str = null; while ((str = reader.ReadLine()) != null) { string[] ops = str.Split(' '); Sweet item = null; switch (ops[0]) { case "Конфета": { item = new Candy(ops[1], double.Parse(ops[2]), double.Parse(ops[3]), double.Parse(ops[4]), double.Parse(ops[5]), ops[6]); } break; case "Вафля": { item = new Waffle(ops[1], double.Parse(ops[2]), double.Parse(ops[3]), double.Parse(ops[4]), ops[5], bool.Parse(ops[6])); } break; case "Фрукт": { item = new Fruit(ops[1], double.Parse(ops[2]), double.Parse(ops[3]), double.Parse(ops[4]), double.Parse(ops[5]), double.Parse(ops[6])); } break; } G.AddItem(item); } return G; }
public void Write(Gift gift) { writer.Write(@gift.ToString()); writer.Close(); }
static void Main(string[] args) { ICollection <Sweet> sweets = new List <Sweet> { new ChocolateCandy("", 23, 45, 55), new ChocolateCandy("", 22, 40, 555), new BrittleСandy("", 10, 42, 11), new BrittleСandy("", 8, 80, 5), new LiquorCandy("", 7, 5, 555), new CurdCookie("", 111, "", 2), new ChocolateCookie("", 75, "", 10), new OatCookie("", 30, ""), new Chocolate("", 200, 25, 50), new Marshmallow("", 1, "", 10) }; Gift gift = new Gift(); IGiftService giftService = new GiftService(); giftService.AddSweetsToGift(gift, sweets); int choose; bool flag = true; while (flag) { Console.WriteLine("1-print all sweets in gift\n" + "2-Search by sugar value\n" + "3-Sort by weight\n" + "4-Get total gift weight\n" + "5-Sort sweets which have sugar\n" + "6-Sort sweets which have chocolate\n"); choose = Convert.ToInt32(Console.ReadLine()); Console.Clear(); switch (choose) { case 1: { Console.WriteLine("All sweets"); giftService.PrintAll(gift.Sweets); break; } case 2: { Console.Write("Min value"); int min = int.Parse(Console.ReadLine()); Console.Write("Max value"); int max = int.Parse(Console.ReadLine()); Console.WriteLine(); giftService.PrintAll(giftService.SearchSugar(gift.Sweets, min, max)); break; } case 3: { Console.WriteLine("All sweets sorted by weight"); giftService.PrintAll(giftService.SortByWeight(gift.Sweets)); Console.WriteLine(); break; } case 4: { Console.WriteLine("Gift weight"); Console.WriteLine($"{giftService.CalculateWeight(gift.Sweets)}g"); Console.WriteLine(); break; } case 5: { Console.WriteLine("Sort sweets which have sugar"); giftService.PrintAll(giftService.SortOnlySugarable(gift.Sweets)); Console.WriteLine(); break; } case 6: { Console.WriteLine("Sort sweets which have chocolate"); giftService.PrintAll(giftService.SortOnlyChocolable(gift.Sweets)); Console.WriteLine(); break; } case 0: { flag = false; break; } default: { break; } } } Console.WriteLine(); }