示例#1
0
        public void AddPerfume(Perfume m)
        {
            //if(listProducts.Count == 0)
            //{
            //    listProducts.Add(m);
            //    m.Quantity++;
            //    return;
            //}


            //foreach (var product in listProducts)
            //{
            //    if (product.Name.Equals(m.Name))
            //    {
            //        m.Quantity++;
            //    }
            //    else
            //    {
            //        listProducts.Add(m);
            //        m.Quantity++;
            //    }
            //}

            var perfumeToBeFound = listProducts.FirstOrDefault(x => x.Name.Equals(m.Name));

            if (perfumeToBeFound != null)
            {
                perfumeToBeFound.Quantity++;
            }
            else
            {
                listProducts.Add(m);
                m.Quantity++;
            }
        }
示例#2
0
        public void DeletePerfume(Perfume m)
        {
            var perfumeToBeFound = listProducts.FirstOrDefault(x => x.Name.Equals(m.Name));

            if (perfumeToBeFound != null && perfumeToBeFound.Quantity > 1)
            {
                perfumeToBeFound.Quantity--;
            }
            else if (perfumeToBeFound != null && perfumeToBeFound.Quantity == 1)
            {
                listProducts.Remove(perfumeToBeFound);
            }
            else
            {
                throw new Exception("Perfume not found.");
            }
        }