Exemple #1
0
        internal static void AddNewCandy(CandyStorage db)
        {
            // Get candy type
            var candyTypes   = db.GetCandyTypes();
            var newCandyMenu = new View()
                               .AddMenuText("What type of candy did you get?")
                               .AddMenuOptions(candyTypes);

            Console.Write(newCandyMenu.GetFullMenu());

            var selectedCandyType = Console.ReadKey().KeyChar.ToString();
            var candyTypeIndex    = int.Parse(selectedCandyType);
            var candyType         = candyTypes[candyTypeIndex - 1];

            // Get candy name

            var candyNames    = db.GetCandyNames(candyType);
            var candyNameMenu = new View()
                                .AddMenuText("What is the name of the candy?")
                                .AddMenuOptions(candyNames)
                                .AddMenuText("Or enter a new name");

            Console.Write(candyNameMenu.GetFullMenu());

            int candyNameIndex;
            var candyName = Console.ReadLine();

            int.TryParse(candyName, out candyNameIndex);

            if (candyNameIndex > 0)
            {
                candyName = candyNames[candyNameIndex - 1];
            }

            // Get candy manufacturer
            var candyMakes    = db.GetCandyMakes();
            var candyMakeMenu = new View()
                                .AddMenuText("Who makes the candy?")
                                .AddMenuOptions(candyMakes)
                                .AddMenuText("Or enter a new manufacturer");

            Console.Write(candyMakeMenu.GetFullMenu());

            int candyMakeIndex;
            var candyMake = Console.ReadLine();

            int.TryParse(candyMake, out candyMakeIndex);

            if (candyMakeIndex > 0)
            {
                candyMake = candyMakes[candyMakeIndex - 1];
            }

            // Make new candy from inputs and save
            var candyToAdd = new Candy {
                Name = candyName, Flavor = candyType, Manufacturer = candyMake, ReceivedOn = DateTime.Now
            };

            db.SaveNewCandy(candyToAdd);
        }