示例#1
0
        public void ChangeProductQuantity()
        {
            Console.Clear();

            using (var context = new ShopsContext())
            {
                Console.WriteLine("Select a product to change its quantity:");
                foreach (var product in context.Products)
                {
                    Console.WriteLine($" {product.ID}. {product.Name},\tquantity - {product.Quantity}");
                }
                Console.WriteLine("0. Go to main menu");

                var productID = ConsoleReadToInt();

                var productToChange = context.Products.Find(productID);
                if (productToChange != null)
                {
                    Console.WriteLine($"Enter changed quantity of {productToChange.Name}");
                    var quantity = ConsoleReadToInt();
                    productToChange.Quantity = quantity;
                    context.SaveChanges();
                    Console.WriteLine("Product quantity has changed.\nPress any key to go to the main menu");
                    Console.ReadKey();
                }
                else if (productID != 0)
                {
                    Console.WriteLine("No such product.\nPress any key to go to the main menu");
                    Console.ReadKey();
                }
            }
        }
示例#2
0
        public void BuyProduct()
        {
            Console.Clear();

            using (var context = new ShopsContext())
            {
                Console.WriteLine("Select a shop to buy the product:");
                Console.WriteLine(" 0. All products");

                foreach (var shop in context.Shops)
                {
                    Console.WriteLine($" {shop.ID}. {shop.Name}");
                }
                var shopID = ConsoleReadToInt();

                var products = new List<Product>();
                if (shopID != 0)
                {
                    var shop = context.Shops.Find(shopID);
                    if (shop != null)
                    {
                        products = shop.Products.Where(x => x.Quantity > 0).ToList();
                    }
                    else
                    {
                        Console.WriteLine("No such shop.\nPress any key to go to the main menu");
                        Console.ReadKey();
                    }
                }
                else
                {
                    products = context.Products.Where(x => x.Quantity > 0).ToList();
                }

                if (products.Count > 0)
                {
                    BuyProductFromList(products);
                    context.SaveChanges();
                }
                else
                {
                    Console.WriteLine("No products to buy in this shop.\nPress any key to go to the main menu");
                    Console.ReadKey();
                }
            }
        }
示例#3
0
        public void AddProduct()
        {
            Console.Clear();
            Console.WriteLine("Enter product name:");
            var name = Console.ReadLine();
            Console.WriteLine("Enter product quantity:");
            var quantity = ConsoleReadToInt();
            Console.WriteLine("Enter product price:");
            var price = ConsoleReadToDec();

            var product = new Product
            {
                Name = name,
                Quantity = quantity,
                Price = price
            };

            using (var context = new ShopsContext())
            {
                Console.WriteLine("Select a shop to add the product:");
                Console.WriteLine(" 0. No shop");
                foreach (var shop in context.Shops)
                {
                    Console.WriteLine($" {shop.ID}. {shop.Name}");
                }
                var shopID = ConsoleReadToInt();

                if (shopID != 0)
                {
                    var shop = context.Shops.Find(shopID);
                    if (shop != null) shop.Products.Add(product);
                    context.SaveChanges();
                }
                else
                {
                    context.Products.Add(product);
                    context.SaveChanges();
                }
            }
        }
示例#4
0
        public void AddShop()
        {
            Console.Clear();
            Console.WriteLine("Enter shop name:");
            var name = Console.ReadLine();
            Console.WriteLine("Enter shop address:");
            Console.WriteLine("Enter country:");
            var country = Console.ReadLine();
            Console.WriteLine("Enter city:");
            var city = Console.ReadLine();
            Console.WriteLine("Enter street:");
            var street = Console.ReadLine();
            Console.WriteLine("Enter building number:");
            var buildingNumber = ConsoleReadToInt();
            Console.WriteLine("Enter postal code:");
            var postalCode = Console.ReadLine();

            var shop = new Shop
            {
                Name = name,
                Address = new Address
                {
                    Country = country,
                    City = city,
                    Street = street,
                    BuildingNumber = buildingNumber,
                    PostalCode = postalCode
                }
            };

            using (var context = new ShopsContext())
            {
                context.Shops.Add(shop);
                context.SaveChanges();
            }
        }