static void Main(string[] args) { try { SweetGift sweetGift = new SweetGift(); sweetGift.AddSweets(new Marshmallow(KindMarshmallow.Premium)); sweetGift.AddSweets(new Candy(KindCandy.Snickers), 2); sweetGift.AddSweets(new Marshmallow(KindMarshmallow.Bobruisk)); sweetGift.AddSweets(new Candy(KindCandy.Alenka), 2); sweetGift.AddSweets(new Waffle(KindWaffle.Victoria)); sweetGift.AddSweets(new Waffle(KindWaffle.Yashkino), 3); SweetGiftConsole.StartWorking(sweetGift); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Program has finished"); } }
public static void StartWorking(SweetGift sweetGift) { if (sweetGift == null) { return; } do { Console.Clear(); Console.WriteLine(header); switch (Console.ReadLine()) { case "1": Console.WriteLine(sweetGift); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; case "2": sweetGift.Sort(SortCriterion.NameSweet); Console.WriteLine(sweetGift); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; case "3": sweetGift.Sort(SortCriterion.WeightSweet); Console.WriteLine(sweetGift); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; case "4": sweetGift.Sort(SortCriterion.SugarSweet); Console.WriteLine(sweetGift); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; case "5": Console.WriteLine("Enter the left limit: "); if (!(double.TryParse(Console.ReadLine(), out double leftLimit)) || leftLimit < 0) { Console.WriteLine("Incorrect input of the left limit"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; } Console.WriteLine("Enter the right limit: "); if (!(double.TryParse(Console.ReadLine(), out double rightLimit)) || rightLimit < 0) { Console.WriteLine("Incorrect input of the right limit"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; } if (leftLimit > rightLimit) { Console.WriteLine("Incorrect input. Left limit must be less or equals than right limit"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; } Console.WriteLine(sweetGift.SearchSweetBySugar(leftLimit, rightLimit).Count == 0 ? "There are no such sweets in the gift" : new SweetGift(sweetGift.SearchSweetBySugar(leftLimit, rightLimit)).ToString()); Console.WriteLine("Press any key to continue"); Console.ReadKey(); break; default: Console.WriteLine("You have finished work"); return; } } while (true); }