Exemplo n.º 1
0
        public int CompareTo(object objOther)
        {
            ShoplistItem other = objOther as ShoplistItem;

            if (other == null)
            {
                throw new ArgumentException("Cannot compare shoplist item to a non shoplist item");
            }

            int mySort    = this.Product.Category.CategorySorts.SingleOrDefault(cs => cs.SupermarketId == this.ShopList.SuperMarketId).SortValue;
            int otherSort = other.Product.Category.CategorySorts.SingleOrDefault(cs => cs.SupermarketId == other.ShopList.SuperMarketId).SortValue;

            return(mySort.CompareTo(otherSort));
        }
Exemplo n.º 2
0
        public void SortedGivenShopListTest()
        {
            //Assign
            Supermarket sm;
            List<Category> categories;
            List<Product> products;
            SmartShopLogicsTests.CreateDataBaseObjects(out sm, out categories, out products);

            ShopList list = new ShopList();
            #region Populating shopping list
            list.Supermarket = sm;
            list.SuperMarketId = sm.Id;
            for (int i = 0; i < products.Count; i++)
            {
                Product currProduct = products[i];
                ShoplistItem newItem = new ShoplistItem() { Product = currProduct, Quantity = i, ShopList = list };
                list.ShoplistItems.Add(newItem);
            }
            #endregion

            //act
            DataBase dataBase = Substitute.For<DataBase>();
            SmartShopLogics bs = new SmartShopLogics(dataBase);
            ShopList sorted = bs.GetSortedList(list);

            //assert
            int lastCategoryId = -1;
            for (int i = 0; i < sorted.ShoplistItems.Count; i++)
            {
                List<ShoplistItem> items = sorted.ShoplistItems.ToList();
                int currCategory = items[i].Product.Category.CategorySorts.Where(cat => cat.Supermarket == sm).SingleOrDefault().SortValue;
                //If list is sorted, the sort value should always increase
                Assert.IsTrue(currCategory >= lastCategoryId, "Shopping list was not sorted properly ({0} sort value came before {1})",lastCategoryId, currCategory);
                lastCategoryId = currCategory;

            }
        }
Exemplo n.º 3
0
        public ShopList GetShoppingList(Dictionary<Product, int> quantityByProduct, Supermarket market, Customer customer)
        {
            ShopList list = new ShopList();

            foreach (var pair in quantityByProduct)
            {
                var product = pair.Key;
                var quantity = pair.Value;
                ShoplistItem item = new ShoplistItem();
                item.Product = product;
                item.ProductId = product.Id;
                item.Quantity = quantity;
                item.ShopList = list;
                list.ShoplistItems.Add(item);

            }

            if (market == null)
            {
                var markets = this.GetAllSuperMarkets();
                market = markets[0];
            }
                list.Supermarket = market;
                list.SuperMarketId = market.Id;

            return this.GetSortedList(list,customer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a shopping based on data in GUI.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        private ShopList GetShoppingListFromGui()
        {
            Logger.Log("Getting shopping list from gui");
            ShopList list = new ShopList();

            foreach (DataGridViewRow currRow in this.gvProducts.Rows)
            {
                bool toBuy = Convert.ToBoolean( currRow.Cells[this.clmToBuy.Index].Value);
                DataGridViewCell cllQuant = currRow.Cells[this.clmQuantity.Index];
                int quantity;
                if (toBuy && cllQuant.Value != null && int.TryParse(cllQuant.Value.ToString(),out quantity))
                {
                    ShoplistItem item = new ShoplistItem();
                    item.Product = (Product)currRow.DataBoundItem;
                    item.Quantity = quantity;
                    item.ShopList = list;
                    list.ShoplistItems.Add(item);
                }
            }

            Supermarket market = this.cmbSuperMarkets.SelectedItem as Supermarket;
            list.Supermarket = market;
            list.SuperMarketId = market.Id;
            Logger.Log(String.Format("got shopping list from gui: {0}", list.ToString()));
            return list;
        }