示例#1
0
        static void Main(string[] args)
        {
            List <Food> groceries = new List <Food>
            {
                new Food("banana", 0.65),
                new Food("orange", 0.9),
                new Food("cucumber", 0.98),
                new Food("apple", 0.55),
                new Food("donut", 1.7),
                new Food("juice", 1.5),
                new Food("avocado", 2.5)
            };


            using (var db = new ShopDbContext())
            {
                List <Food> foods = db.Foods.ToList();
                foreach (var food in groceries)
                {
                    bool exists = false;
                    if (foods.FirstOrDefault(x => x.Name == food.Name) != null)
                    {
                        exists = true;
                    }
                    if (!exists)
                    {
                        db.Foods.Add(food);
                    }
                }
                ;
                db.SaveChanges();

                Console.WriteLine("Please enter your first name!");
                string firstname = Console.ReadLine();

                Console.WriteLine($"Hi, {firstname}! Please enter your last name!");
                string lastname = Console.ReadLine();

                Person client = db.Persons.FirstOrDefault(x => x.FirstName.ToLower() == firstname.ToLower() && x.LastName.ToLower() == lastname.ToLower());
                if (client == null)
                {
                    Console.WriteLine("Hello! You are our new client");
                    client = new Person(firstname, lastname);
                    db.Persons.Add(client);
                }
                else
                {
                    Console.WriteLine("Hello! You are existing client");
                }



                Console.WriteLine($"Welcome to our shop, {client}!");
                ShoppingCart shoppingCart = new ShoppingCart(client);

                while (true)
                {
                    Console.WriteLine($"What do you want to buy?");

                    ChooseFood(groceries, shoppingCart);


                    Console.WriteLine("Something else? Y/N");

                    if (Console.ReadLine().ToLower() != "Y")
                    {
                        break;
                    }
                }
                db.ShoppingCarts.Add(shoppingCart);
                db.SaveChanges();

                var people = db.Persons;
                foreach (var person in people)
                {
                    person.PrintItems(db);
                    Console.WriteLine("*********************************************************");
                }
                Console.WriteLine($"Thanks for visiting our shop, {client}!");
                Console.ReadKey();
            }
        }
示例#2
0
 public UnitOfWork(ShopDbContext db)
 {
     _db = db;
 }
        static void Main(string[] args)
        {
            List <Food> groceries = new List <Food>
            {
                new Food("sandwich", 0.5),
                new Food("grapes", 1.7),
                new Food("milk", 2.5),
                new Food("apple", 3),
                new Food("cheese", 2)
            };

            ShoppingCart newCart = new ShoppingCart();

            //ChooseFood(groceries, newCart);

            /*foreach (var food in groceries)
             *          {
             *                  newCart.Items.Add(food);
             *          }*/

            ChooseFood(groceries, newCart);

            while (Console.ReadLine() == "Yes")
            {
                ChooseFood(groceries, newCart);
            }

            using (var db = new ShopDbContext())


            {
                var cartWithZeroSum = db.ShoppingCarts.Where(x => x.Sum == 0);
                foreach (var cart in cartWithZeroSum)
                {
                    db.ShoppingCarts.Remove(cart);
                }

                db.SaveChanges();
                db.ShoppingCarts.Add(newCart);
                db.SaveChanges();

                var carts = db.ShoppingCarts.Include("Items").OrderByDescending(c => c.DateCreated).ToList();



                foreach (var cart in carts)
                {
                    Console.WriteLine("");
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                    Console.WriteLine("Items in cart:");

                    foreach (var food in cart.Items)
                    {
                        Console.WriteLine($"Name: {food.Name}  Price: {food.Price}");
                    }
                    Console.WriteLine($"Total: {cart.Sum}");
                    Console.WriteLine("****");
                }

                //show only the last(latest created) shopping cart with all its items

                /*   var carts1 = db.ShoppingCarts.OrderByDescending(c => c.DateCreated).First();
                 *
                 * Console.WriteLine($"Last shopping cart created on {carts1.DateCreated}");
                 * Console.WriteLine("*****");
                 * Console.WriteLine("Items in cart:");
                 *
                 * foreach (var cart in carts1.Items)
                 * {
                 *
                 *     Console.WriteLine($"{cart.Name}");
                 *
                 * }
                 * Console.ReadLine();*/


                //show only carts with sum > 5

                /*  var carts2 = db.ShoppingCarts.Include("Items").Where(cart => cart.Sum > 5);
                 *    foreach (var cart in carts2)
                 * {
                 *    Console.WriteLine("");
                 *    Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                 *    Console.WriteLine("Total sum of shopping cart over 5 euros");
                 *
                 *    foreach(var food in cart.Items)
                 *    {
                 *        Console.WriteLine($"Name: {food.Name}  Price: {food.Price}");
                 *
                 *    }
                 *    Console.WriteLine($"Total sum is: {cart.Sum}");
                 *    Console.WriteLine("****");
                 *     }*/

                //show only the carts with  more than one item in it(and how many items in it)

                /* var carts3 = db.ShoppingCarts.Include("Items").Where(cart => cart.Items.Count > 1);
                 * foreach (var cart in carts3)
                 * {
                 *   Console.WriteLine("");
                 *   Console.WriteLine("");
                 *   Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                 *
                 *   foreach (var food in cart.Items)
                 *   {
                 *       Console.WriteLine($"Name: {food.Name}  Price: {food.Price}");
                 *
                 *   }
                 *   Console.WriteLine($"Cart has {cart.Items.Count()} items in it");
                 *   Console.WriteLine($"Total sum is: {cart.Sum}");
                 *   Console.WriteLine("****");
                 * }
                 * Console.ReadLine();*/

                //show only the carts that contain apple TEHHA

                /* var carts4 = db.ShoppingCarts.Where(x => x.Items.Any(y => y.Name == "apple"));
                 *
                 * foreach (var cart in carts4)
                 * {
                 *   Console.WriteLine("******");
                 *   Console.WriteLine(" ");
                 *   Console.WriteLine("Shopping carts that contain apple");
                 *   Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                 *
                 *
                 *   foreach (var food in cart.Items)
                 *   {
                 *       Console.WriteLine($"Name: {food.Name}  Price: {food.Price}");
                 *
                 *   }
                 *   Console.WriteLine($"Total: {cart.Sum}");
                 * }
                 *
                 *  Console.ReadLine();*/

                //show the total number of shopping carts

                /* var carts5 = db.ShoppingCarts.Count();
                 *
                 *
                 *   Console.WriteLine($"Total number on shopping carts: {carts5}");*/

                //show the cart with maxium sum

                /* var cart6 = db.ShoppingCarts.Max(z => z.Sum);
                 *   {
                 *   Console.WriteLine($"Cart's maxium sum: {cart6}");
                 * }*/
                //show the cheapest food

                /* double cart7 = db.Foods.Min(z => z.Price);
                 * {
                 *   Console.WriteLine($"Cheapest food costs {cart7}" );
                 * }*/
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            //list kus kõik tooted on
            List <Food> groceries = new List <Food>
            {
                new Food("apple", 1.7),
                new Food("bread", 1.2),
                new Food("cheese", 2)
            };

            //Food apple = new Food("apple", 1.7);


            //poekorv
            ShoppingCart newCart = new ShoppingCart();

            //do
            //{
            //    chooseFood(groceries, newCart);
            //}
            //while (Console.ReadLine().ToLower() == "y");



            //poekorvi newCart lisatakse listist groceries toidud
            //foreach (Food food in groceries)
            //{
            //    newCart.Items.Add(food);
            //}


            //db loomine
            using (ShopDbContext db = new ShopDbContext())
            {
                IQueryable <ShoppingCart> cartWithZeroSum = db.ShoppingCarts.Where(x => x.Sum == 0);
                foreach (ShoppingCart cart in cartWithZeroSum)
                {
                    db.ShoppingCarts.Remove(cart);
                }

                //db kõik shopping cartid listakse database
                db.ShoppingCarts.Add(newCart);
                //salvesta muutused
                db.SaveChanges();

                List <ShoppingCart> carts = db.ShoppingCarts.Include("Items").OrderByDescending(x => x.DateCreated).ToList();
                foreach (ShoppingCart cart in carts)
                {
                    Console.WriteLine($"Created on {cart.DateCreated}");
                    foreach (Food food in cart.Items)
                    {
                        Console.WriteLine($"Name: {food.Name} Price: {food.Price}");
                    }
                }



                //    Console.WriteLine("-----------------------------------");
                //carts = db.ShoppingCarts.Include("Items").OrderByDescending(x => x.DateCreated).ToList();
                //Console.WriteLine($"Created on {carts[0].DateCreated}");
                //Console.WriteLine(carts[0].Items);

                //foreach (Food food in carts[0].Items)
                //{
                //    Console.WriteLine("test");
                //    Console.WriteLine($"Name: {food.Name} Price: {food.Price}");
                //}

                System.Data.Entity.Infrastructure.DbQuery <ShoppingCart> shcarts        = db.ShoppingCarts;
                System.Data.Entity.Infrastructure.DbQuery <ShoppingCart> cartsWithItems = db.ShoppingCarts.Include("Items");
                System.Data.Entity.Infrastructure.DbQuery <Food>         foods          = db.Items;

                //1.show only the last(latest created) shopping cart with all its items
                ShoppingCart latest = cartsWithItems.OrderBy(cart => cart.DateCreated).ToList().First(); //lamda expression
                //or
                ShoppingCart latest2 = cartsWithItems.OrderBy(cart => cart.DateCreated).ToList().Last();

                //2.show only the carts with sum > 5
                List <ShoppingCart> largerThanFive = shcarts.Where(cart => cart.Sum > 5).ToList();
                foreach (var cart in largerThanFive)
                {
                    Console.WriteLine($"ShoppingCart {cart.DateCreated} {cart.Sum}");
                }

                //3.show only the carts with more than one item in it(and how many items is in cart)
                IQueryable <ShoppingCart> cartsMoreThanOne = cartsWithItems.Where(cart => cart.Items.Count() > 1);
                foreach (var cart in cartsMoreThanOne)
                {
                    Console.WriteLine(cart.DateCreated);
                }

                // LINQ
                cartsMoreThanOne = from cart in cartsWithItems where cart.Items.Count() > 1 select cart;

                //4.show only the carts that contain apples
                IQueryable <ShoppingCart> cartsWithApples = cartsWithItems.Where(cart => cart.Items.Any(y => y.Name == "apple"));

                //5.show the total number of shopping carts
                int totalShCatrs = shcarts.Count();

                //6.show the cart with maximum sum
                ShoppingCart maxSumShoppingcart = shcarts.OrderByDescending(cart => cart.Sum).FirstOrDefault();

                //7.show the cheapest food
                Food cheapestFood = foods.OrderBy(food => food.Price).FirstOrDefault();

                Console.ReadLine();
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            List <Food> groceries = new List <Food>
            {
                new Food("banana", 2.4),
                new Food("onion", 0.4),
                new Food("pear", 1),
                new Food("apple", 0.9),
                new Food("butter", 0.6)
            };


            using (var db = new ShopDbContext())
            {
                //db.Database.Delete();
                List <Food> foods = db.Foods.ToList();
                foreach (var food in groceries)
                {
                    bool exists = false;
                    if (foods.FirstOrDefault(x => x.Name == food.Name) != null)
                    {
                        exists = true;
                    }
                    if (!exists)
                    {
                        db.Foods.Add(food);
                    }
                }
                ;
                db.SaveChanges();

                Console.WriteLine("Please enter your first name!");
                string firstname = Console.ReadLine();

                Console.WriteLine($"Hi, {firstname}! Please enter your last name!");
                string lastname = Console.ReadLine();

                Person client = db.Persons.FirstOrDefault(x => x.FirstName.ToLower() == firstname.ToLower() && x.LastName.ToLower() == lastname.ToLower());
                if (client == null)
                {
                    client = new Person(firstname, lastname);
                    db.Persons.Add(client);
                }


                Console.WriteLine($"Welcome to our shop, {client}!");
                ShoppingCart shoppingCart = new ShoppingCart(client);

                while (true)
                {
                    Console.WriteLine($"What do you want to buy?");

                    ChooseFood(groceries, shoppingCart);

                    Console.WriteLine("Something else?");

                    if (Console.ReadLine().ToLower() != "y")
                    {
                        break;
                    }
                }
                db.ShoppingCarts.Add(shoppingCart);
                db.SaveChanges();

                var people = db.Persons;
                foreach (var person in people)
                {
                    person.PrintItems(db);
                    Console.WriteLine("__________________________________");
                }
                Console.ReadKey();
            }
        }
示例#6
0
        static void Main(string[] args)
        {
            List <Food> groceries = new List <Food>
            {
                new Food("apple", 1.7),
                new Food("bread", 1.2),
                new Food("cheese", 2)
            };
            ShoppingCart newCart = new ShoppingCart();

            foreach (var food in groceries)
            {
                newCart.Items.Add(food);
            }

            using (var db = new ShopDbContext())
            {
                db.ShoppingCarts.Add(newCart);
                db.SaveChanges();

                var carts = db.ShoppingCarts;
                foreach (var cart in carts)
                {
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                    foreach (var food in cart.Items)
                    {
                        Console.WriteLine($"Name: {food.Name} Price: {food:Price}");
                    }
                }
            }

            Console.WriteLine("Choose food to select...");
            string itemFood = Console.ReadLine();

            Food chosenFood = groceries.FirstOrDefault(x => x.Name == itemFood);

            if (chosenFood == null)
            {
                Console.WriteLine("Sorry, no food " + itemFood + " in our shop");
            }
            else
            {
                Console.WriteLine("How much do you want? ");
                string amount = Console.ReadLine();
                int    a;
                bool   success = int.TryParse(amount, out a); //
                while (!success)
                {
                    Console.WriteLine("Sorry, amount should be integer value: ");
                    amount  = Console.ReadLine();
                    success = int.TryParse(amount, out a);
                }
                chosenFood.InsertCommand = new OleDbCommand("Insert into");
                chosenFood.InsertCommand.Parameters.Add("@FIO", OleDbType.VarChar, 255).Value = itemFood;

                //Console.WriteLine("Anything else? Y/N");
            }



            //////////////////////////////////
        }
示例#7
0
        static void Main(string[] args)
        {
            List <Food> groceries = new List <Food>
            {
                new Food("apple", 1.7),
                new Food("bread", 1.2),
                new Food("cheese", 2)
            };

            ShoppingCart newCart = new ShoppingCart();

            //ChooseFood(groceries, newCart);
            //while (Console.ReadLine() == "Yes")
            //{
            //	ChooseFood(groceries, newCart);
            //}

            //foreach (var food in groceries)
            //{
            //	newCart.Items.Add(food);
            //}

            using (var db = new ShopDbContext())
            {
                IQueryable <ShoppingCart> cartsWithZeroSum = db.ShoppingCarts.Where(x => x.Sum == 0);
                foreach (var cart in cartsWithZeroSum)
                {
                    db.ShoppingCarts.Remove(cart);
                }
                db.SaveChanges();

                db.ShoppingCarts.Add(newCart);
                db.SaveChanges();

                //var carts = db.ShoppingCarts.Include("Items").OrderByDescending(x => x.DateCreated).ToList();
                //foreach (var cart in carts)
                //{
                //	Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                //	foreach (var food in cart.Items)
                //	{
                //		Console.WriteLine($"Name: {food.Name}  Price: {food.Price}");
                //	}
                //	Console.WriteLine($"Total: {cart.Sum}");
                //}

                var carts          = db.ShoppingCarts;
                var cartsWithItems = db.ShoppingCarts.Include("Items");
                var foods          = db.Foods;

                //1. Last created cart
                var latest  = cartsWithItems.OrderByDescending(cart => cart.DateCreated).ToList().First();
                var latest2 = cartsWithItems.OrderBy(cart => cart.DateCreated).ToList().Last();

                Console.WriteLine($"Shopping cart created on {latest.DateCreated}");
                Console.WriteLine($"Shopping cart created on {latest2.DateCreated}");
                Console.WriteLine("");

                //2. Carts with Sum > 5
                var carts5 = carts.Where(x => x.Sum > 5).ToList();
                foreach (var cart in carts5)
                {
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated} Sum: {cart.Sum} ");
                }

                // 3. Carts with more than 1 item (and show how many items are there)
                var cartsMoreThan1 = cartsWithItems.Where(x => x.Items.Count() > 1).ToList();
                foreach (var cart in cartsMoreThan1)
                {
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated} Items count: {cart.Items.Count()}");
                }

                var cartsMoreThan1_query = from cart in cartsWithItems where cart.Items.Count() > 1 select cart;
                foreach (var cart in cartsMoreThan1_query)
                {
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated} Items count: {cart.Items.Count()}");
                }
                Console.WriteLine("");

                // 4. Only carts that contain apples
                var cartsWithApples = cartsWithItems.Where(cart => cart.Items.Any(y => y.Name == "apple"));
                foreach (var cart in cartsWithApples)
                {
                    Console.WriteLine($"Shopping cart created on {cart.DateCreated}");
                    foreach (var food in cart.Items)
                    {
                        Console.WriteLine($"{food.Name}");
                    }
                }

                // 5. Show the total number of shopping carts
                var count = carts.Count();
                Console.WriteLine($"Total number of carts is {count}");

                // 6. Show th cart with maximum sum
                var cartWithMaxSum = carts.OrderByDescending(x => x.Sum).FirstOrDefault();
                Console.WriteLine($"Cart created on {cartWithMaxSum.DateCreated} Sum: {cartWithMaxSum.Sum}");

                // 7. Show the cheapest food
                var cheapestFood = foods.OrderByDescending(food => food.Price).ToList().Last();
                Console.WriteLine($"Cheapest food is {cheapestFood.Name} Price {cheapestFood.Price}");
            }
        }