コード例 #1
0
        /// <summary>
        /// Makes it possible for the user to update an address from the database and includes a TryCatch - in case the input does not work - and a failsafe in case the user has chosen the wrong address
        /// </summary>
        public static void UpdateAddress()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    DisplayAddresses();
                    Console.WriteLine("\nType the Guid of the adress you want to update:\n");
                    try
                    {
                        var addressId = Guid.Parse(Console.ReadLine());

                        var address = context.Addresses.Find(addressId);

                        if (address != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nThe adress you have selected is:\n" +
                                              $"{address.StreetName}\n" +
                                              $"{address.PostalCode} " +
                                              $"{address.City}\n");


                            Console.WriteLine("\nDo you really want to update this address?\n");
                            Console.WriteLine("\nType Y for yes and N for no\n");

                            var yesNo = Console.ReadLine();
                            if (yesNo == "y" || yesNo == "Y")
                            {
                                try
                                {
                                    Console.WriteLine("\nInput the new info\n");

                                    Console.WriteLine("Street name: ");
                                    var streetName = Console.ReadLine();

                                    Console.WriteLine("\nPostal code: ");
                                    var postalCode = Console.ReadLine();

                                    Console.WriteLine("\nCity: ");
                                    var city = Console.ReadLine();


                                    address.StreetName = streetName;
                                    address.PostalCode = postalCode;
                                    address.City       = city;

                                    context.Update(address);
                                    context.SaveChanges();
                                    Console.WriteLine("\nThe address has been updated\n");
                                    Console.WriteLine("\n----------------------------------------\n");

                                    Console.WriteLine("\nWhat would you like to do next?\n" +
                                                      "\n1. Choose another address to update" +
                                                      "\n2. Go back to the main menu\n");

                                    var choice = Console.ReadLine();
                                    Console.Clear();
                                    if (choice == "2")
                                    {
                                        MainMenuAddresses();
                                    }
                                }
                                catch (Exception)
                                {
                                    Console.Clear();
                                    Console.WriteLine("\nSomething went wrong. The info you have inserted has not been saved.\n");
                                    Console.WriteLine("\n----------------------------------------\n");
                                    MainMenuAddresses();
                                }
                            }

                            else if (yesNo == "n" || yesNo == "N")
                            {
                                Console.Clear();
                                Console.WriteLine("\nOk then, What would you like to do next?\n" +
                                                  "\n1. Choose another address to update" +
                                                  "\n2. Go back to the main menu\n");

                                var choice = Console.ReadLine();
                                Console.Clear();

                                if (choice == "2")
                                {
                                    MainMenuAddresses();
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("\nSomething went wrong. Try again!\n");
                        Console.WriteLine("\n----------------------------------------\n");
                        MainMenuAddresses();
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Makes it possible for the user to update an address from the database and includes a TryCatch - in case the input does not work - and a failsafe in case the user has chosen the wrong address
        /// </summary>
        public static void UpdateCustomer()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    DisplayCustomers();
                    Console.WriteLine("\nType the GUID of the customer you want to update:\n");
                    try
                    {
                        var input = Guid.Parse(Console.ReadLine());

                        Console.Clear();

                        var a = context.Customers.Find(input);

                        if (a != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nThe customer you have selected is:\n" +
                                              $"{a.FirstName} " +
                                              $"{a.LastName}\n" +
                                              $"{a.Email}\n");

                            Console.WriteLine("\nDo you really want to update this customer?\n");
                            Console.WriteLine("\nType Y for yes and N for no\n");

                            var yesNo = Console.ReadLine();
                            if (yesNo == "y" || yesNo == "Y")
                            {
                                Console.Clear();
                                Console.WriteLine("\nInput the new info\n");

                                Console.WriteLine("First name: ");
                                var firstName = Console.ReadLine();

                                Console.WriteLine("Last name: ");
                                var lastName = Console.ReadLine();

                                Console.WriteLine("\nPostal code: ");
                                var postalCode = Console.ReadLine();

                                Console.WriteLine("\nEmail: ");
                                var email = Console.ReadLine();

                                Console.WriteLine("\nPhonenumber: ");
                                var phoneNr = Console.ReadLine();


                                a.FirstName = firstName;
                                a.LastName  = lastName;
                                a.Email     = email;
                                a.PhoneNr   = phoneNr;

                                context.Update(a);
                                context.SaveChanges();
                                Console.WriteLine("\nThe customer has been updated\n");
                                Console.WriteLine("\n----------------------------------------\n");

                                Console.WriteLine("\nWhat would you like to do next?\n" +
                                                  "\n1. Choose another customer to update" +
                                                  "\n2. Go back to the cusomers menu\n");

                                var choice = Console.ReadLine();

                                Console.Clear();

                                if (choice == "2")
                                {
                                    MainMenuCustomers();
                                }
                            }

                            else if (yesNo == "n" || yesNo == "N")
                            {
                                Console.Clear();
                                Console.WriteLine("\nOk then, What would you like to do next?\n" +
                                                  "\n1. Choose another customer to update" +
                                                  "\n2. Go back to the cusomers menu\n");

                                var choice = Console.ReadLine();
                                Console.Clear();
                                if (choice == "2")
                                {
                                    MainMenuCustomers();
                                }
                            }

                            else // In case of unknown input
                            {
                                Console.Clear();
                                Console.WriteLine($"\n{input} is not recognized. Try again!\n");
                                Console.WriteLine("\n----------------------------------------\n");
                                MainMenuCustomers();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("\nSomething went wrong. Try again!\n");
                        Console.WriteLine("\n----------------------------------------\n");
                        MainMenuCustomers();
                    }
                }
            }
        }