示例#1
0
        public static void editSnack()
        {
            //This function edits a snack
            string valprice, snackId, replace = "";
            double priceDouble = 0.0;
            int    idSnack     = 0;

            string       snackDetails = File.ReadAllText("Snacks.Json");
            List <Snack> snackDetail  = JsonSerializer.Deserialize <List <Snack> >(snackDetails);

            for (int i = 0; i < snackDetail.Count; i++)
            {
                Console.WriteLine("Snack Id: " + snackDetail[i].Id);
                Console.WriteLine("Snack name: " + snackDetail[i].Name);
                Console.WriteLine("Snack price: " + snackDetail[i].Price);
                Console.WriteLine("Snack contains nuts:" + snackDetail[i].Nuts);
                Console.WriteLine("Snack type: " + snackDetail[i].Type);
                Console.WriteLine("\n===================================================================================\n");
            }

            Snack snack = new Snack();

            Console.WriteLine("Enter the ID of the snack you want to edit: ");
            snackId = Console.ReadLine();
            idSnack = Convert.ToInt32(snackId);
            var searchedSnack = snackDetail.FirstOrDefault(s => s.Id == idSnack);

            for (int i = 0; i < snackDetail.Count; i++)
            {
                Console.WriteLine("Snack Id: " + snackDetail[i].Id);
                Console.WriteLine("Snack name: " + snackDetail[i].Name);
                Console.WriteLine("Snack price: " + snackDetail[i].Price);
                Console.WriteLine("Snack contains nuts:" + snackDetail[i].Nuts);
                Console.WriteLine("Snack type: " + snackDetail[i].Type);
                Console.WriteLine("\n===================================================================================\n");
            }
            Console.WriteLine("Enter the name of the snacc: ");
            searchedSnack.Name = Console.ReadLine();
            Console.WriteLine("Enter the price of the snack: ");
            valprice    = Console.ReadLine();
            replace     = valprice.Replace(".", ".");
            priceDouble = Convert.ToDouble(replace);
            snack.Price = priceDouble;
            Console.WriteLine("Enter enter if the snack contains nuts (Enter Yes or No): ");
            snack.Nuts = Console.ReadLine();
            Console.WriteLine("Enter the type of the snacc: ");
            snack.Type = Console.ReadLine();

            string resultJson = JsonSerializer.Serialize <List <Snack> >(snackDetail);

            File.WriteAllText("snacks.json", resultJson);
            Console.WriteLine("Snack eddited succesfully! :)");
        }