Example #1
0
        public static void RunShoppingScreen(User user)
        {
            ILogger Trace         = new GenericLoggerAdapter();
            int     userSelection = 0;
            bool    wantsToExit   = false;
            var     viewModel     = new ShoppingScreenViewModel();
            Dictionary <int, Product> indexedProducts  = new Dictionary <int, Product>();
            ProductServices           productServices  = new ProductServices();
            ShoppingServices          shoppingServices = new ShoppingServices();

            while (!wantsToExit)
            {
                Console.Clear();
                Console.WriteLine("Welcome {0} to the rare and custom items's shop!\nBelow is a list of all products currently available.", user.UserName);

                indexedProducts.Clear();

                var allProducts = viewModel.AllProdcuts;
                for (int productCount = 0; productCount < allProducts.Count; productCount++)
                {
                    var currenProduct = allProducts[productCount];
                    indexedProducts.Add(productCount, currenProduct);
                }

                Console.WriteLine("Available items: \n");
                foreach (var indexedProduct in indexedProducts)
                {
                    if (!indexedProduct.Value.IsPurchased)
                    {
                        Console.WriteLine($"{indexedProduct.Key} - {indexedProduct.Value.ProductName}");
                    }

                    continue;
                }

                Console.WriteLine("Sold items: \n");
                foreach (var indexedProduct in indexedProducts)
                {
                    if (indexedProduct.Value.IsPurchased)
                    {
                        Console.WriteLine($"{indexedProduct.Key} - {indexedProduct.Value.ProductName} - {indexedProduct.Value.Owner.UserName}");
                    }

                    continue;
                }

                Console.WriteLine("\n\nPlease select from the above list which item you would like to buy or press e and enter to go back a page: ");

                Product selectedProduct;

                try{
                    var input = Console.ReadLine();
                    if (input[0] == 'e')
                    {
                        wantsToExit = true;
                        break;
                    }
                    userSelection   = int.Parse(input);
                    selectedProduct = indexedProducts[userSelection];
                }
                catch (Exception e) {
                    Console.WriteLine("Sorry could not read input either as it is an invalid input or the product does not exist: " + e.Message);
                    Trace.TraceError("Incorrect input format", e.Message + e.StackTrace, SeverityLevel.Error);
                    continue;
                }

                Console.Clear();
                Console.WriteLine("Name: {0}\nPrice: £{1}\nIs it on sale: {2}\n\n", selectedProduct.ProductName, selectedProduct.ProductPrice, selectedProduct.IsSalesProduct ? "Yes" : "No");
                Console.WriteLine("Would you like to buy? (y, n)");

                var doesBuy = Console.ReadLine()[0];
                if (doesBuy != 'y' && doesBuy != 'n')
                {
                    Console.WriteLine("Sorry, invalid input");
                    Console.ReadLine();
                    continue;
                }

                if (doesBuy == 'n')
                {
                    continue;
                }

                if (viewModel.PurchaseProduct(user, selectedProduct))
                {
                    Console.WriteLine("Purchase successful");
                    Console.ReadLine();
                    continue;
                }
            }
        }
Example #2
0
        public static void RunAdminScreen(string currentUserName)
        {
            AdminScreenViewModel viewModel = new AdminScreenViewModel();
            ILogger Trace = new GenericLoggerAdapter();

            int userSelection = 0;

            while (userSelection != 9)
            {
                Console.Clear();

                Console.WriteLine("\n\nPlease select from the options below: " +
                                  "\n\n1. View Users in Db\n2. Update a user's name\n3. Add new User\n" +
                                  "4. Delete User\n5. View Products\n6. Update Product\n7. Add new product\n8. Delete Product\n9. Exit");
                try
                {
                    userSelection = int.Parse(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Could not read answer as: " + e.Message + "\nPlease try again");
                    Trace.TraceError("Incorrect input format", e.Message + e.StackTrace, SeverityLevel.Error);
                }

                switch (userSelection)
                {
                case 1:
                    foreach (var user in viewModel.AllUsers)
                    {
                        Console.WriteLine($"{user.UserId}\n{user.UserName}\n{user.NickName}\n{user.IsAdmin}\n\n");
                    }
                    Console.ReadLine();
                    break;

                case 2:
                    Console.WriteLine("Please give me the name of the user: "******"Sorry, user does not exist in db" : "What would you like to change the name to: ");
                    if (user2update != null)
                    {
                        var newProdName = Console.ReadLine();

                        if (viewModel.UpdateUserName(user2update.UserId, newProdName))
                        {
                            Console.WriteLine("User's name is succesfully updated.");
                        }
                        ;
                        break;
                    }
                    Console.WriteLine("Sorry, something went wrong with the update");
                    Console.ReadLine();
                    break;

                case 3:
                    var newUser = new User();
                    Console.WriteLine("Please enter the new user's Name: ");
                    newUser.UserName = Console.ReadLine();
                    Console.WriteLine("Please enter a nick name: ");
                    newUser.NickName = Console.ReadLine();
                    Console.WriteLine("Please enter y or n to indicate if the user is an admin: ");
                    if (string.Equals(Console.ReadLine().ToLower(), "y"))
                    {
                        newUser.IsAdmin = true;
                    }
                    else
                    {
                        newUser.IsAdmin = false;
                    };
                    if (viewModel.SaveUser(newUser))
                    {
                        Console.WriteLine("New User saved successfully");
                    }
                    else
                    {
                        Console.WriteLine("Could not save new user");
                    }
                    break;

                case 4:
                    Console.WriteLine("Please enter the user id for the user to be deleted: ");
                    int id = 0;

                    try
                    {
                        id = int.Parse(Console.ReadLine());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Sorry, incorrect input: " + e.Message);
                        Trace.TraceError("Incorrect input format", e.Message + e.StackTrace, SeverityLevel.Error);
                        Console.ReadLine();
                        break;
                    }

                    if (viewModel.DeleteUser(id))
                    {
                        Console.WriteLine("User successfully deleted");
                        Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("Could not delete user");
                        Console.ReadLine();
                    }
                    break;

                case 5:
                    foreach (var product in viewModel.AllProducts)
                    {
                        Console.WriteLine($"{product.ProductId}\n{product.ProductName}\n{product.ProductPrice}\n{product.IsSalesProduct}\n\n");
                    }
                    Console.WriteLine("Please press any keys to continue");
                    Console.ReadLine();
                    break;

                case 6:
                    double newPrice      = -1;
                    bool   isSaleProduct = false;

                    Console.WriteLine("Please give me the name of the product: ");
                    string productName = Console.ReadLine();
                    Console.WriteLine("Plese enter new name: ");
                    string newProductName = Console.ReadLine();
                    Console.WriteLine("Please enter new price: ");
                    try
                    {
                        newPrice = double.Parse(Console.ReadLine());
                    }catch (Exception e) {
                        Console.WriteLine("Sorry could not read new price");
                        Trace.TraceError("Incorrect price format", e.Message + e.StackTrace, SeverityLevel.Error);
                        Console.ReadLine();
                        break;
                    }

                    if (newPrice < 0)
                    {
                        Console.WriteLine("Sorry price has to be 0 or greater");
                        Console.ReadLine();
                        break;
                    }

                    Console.WriteLine("Please enter y or n to indicate if product is on sale: ");
                    var isOnSale = Console.ReadLine();

                    if (string.Equals(isOnSale.ToLower(), "y"))
                    {
                        isSaleProduct = true;
                    }

                    if (viewModel.TryUpdateProduct(productName, newProductName, newPrice, isSaleProduct))
                    {
                        Console.WriteLine("Product is succesfully updated.");
                        break;
                    }

                    Console.WriteLine("Sorry, something went wrong with the update");
                    Console.ReadLine();
                    break;

                case 7:
                    isSaleProduct = false;

                    Console.WriteLine("Plese enter new name: ");
                    newProductName = Console.ReadLine();
                    Console.WriteLine("Please enter new price: ");
                    try
                    {
                        newPrice = double.Parse(Console.ReadLine());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Sorry could not read new price");
                        Trace.TraceError("Incorrect price format", e.Message + e.StackTrace, SeverityLevel.Error);
                        Console.ReadLine();
                        break;
                    }

                    if (newPrice < 0)
                    {
                        Console.WriteLine("Sorry price has to be 0 or greater");
                        break;
                    }

                    Console.WriteLine("Please enter y or n to indicate if product is on sale: ");
                    isOnSale = Console.ReadLine();

                    if (string.Equals(isOnSale.ToLower(), "y"))
                    {
                        isSaleProduct = true;
                    }

                    var newProduct = new Product {
                        ProductName    = newProductName,
                        ProductPrice   = newPrice,
                        IsSalesProduct = isSaleProduct,
                        OwnerId        = null,
                    };

                    if (viewModel.AddNewProduct(newProduct))
                    {
                        Console.WriteLine("Product successfully added.");
                        Console.ReadLine();
                        break;
                    }
                    Console.WriteLine("Could not add product");
                    Console.ReadLine();
                    break;

                case 8:
                    Console.WriteLine("Please enter the id of the product to be deleted: ");
                    var productId = 0;

                    try
                    {
                        productId = int.Parse(Console.ReadLine());
                    }catch (Exception e) {
                        Console.WriteLine("Sorry could not read input as : " + e.Message + "Please press any key");
                        Trace.TraceError("Incorrect Product Id format", e.Message + e.StackTrace, SeverityLevel.Error);
                        break;
                    }
                    if (viewModel.DeleteProduct(productId))
                    {
                        Console.WriteLine("Product successfully deleted. Please a key to continue.");
                        Console.ReadLine();
                        break;
                    }
                    Console.WriteLine("Error trying to delete product. Please press any keys to continue.");
                    Console.ReadLine();
                    break;

                case 9:
                    break;

                default:
                    Console.WriteLine("Sorry, incorrect option");
                    break;
                }
            }
        }
Example #3
0
        public static void RunMainScreen()
        {
            var     viewModel = new MainScreenViewModel();
            ILogger Trace     = new GenericLoggerAdapter();

            var userServices  = new UserServices();
            int userSelection = 0;

            while (userSelection != 4)
            {
                Console.Clear();
                Console.WriteLine(string.Format("{0}",
                                                viewModel.CurrentUserName == null ?
                                                "Please Change user to enjoy the most of the app!" :
                                                $"Welcome{viewModel.CurrentUserName}!\n\n"));
                Console.WriteLine("\n\nPlease select an option:\n1.Change User\n2.Admin\n3.Shop\n4.Exit\n ");
                try
                {
                    userSelection = int.Parse(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Sorry, could not understand selection as : " + e.Message);
                    Trace.TraceError("Incorrect User Selection", e.Message + e.StackTrace, SeverityLevel.Error);
                }

                Console.Clear();

                switch (userSelection)
                {
                case 1:
                    Console.WriteLine("Please enter your name: ");
                    string currentUserName = Console.ReadLine();
                    try
                    {
                        viewModel.AssignUserName(currentUserName);
                    }catch (ArgumentNullException e) {
                        Console.WriteLine(e.Message);
                        Trace.TraceError("Could not find user", e.Message + e.StackTrace, SeverityLevel.Warning);
                        Console.ReadLine();
                    }
                    continue;

                case 2:
                    Console.Clear();
                    if (!viewModel.IsAdmin)
                    {
                        Console.WriteLine("Sorry this section is for admmins only!");
                        Console.ReadLine();
                        continue;
                    }
                    AdminScreen.RunAdminScreen(viewModel.CurrentUserName);
                    continue;

                case 3:
                    if (viewModel.CurrentUserName == null || viewModel.CurrentUser == null)
                    {
                        Console.WriteLine("Please Change to an existing User First!");
                        Console.ReadLine();
                        continue;
                    }
                    ShoppingScreen.RunShoppingScreen(viewModel.CurrentUser);
                    continue;

                case 4:
                    break;

                default:
                    Console.WriteLine("Sorry, invalid selection.");
                    Console.ReadLine();
                    continue;
                }
            }
        }