Esempio n. 1
0
 public bool DeleteProduct(string id, double quantity = 0)
 {
     if (quantity < 0)
     {
         return(false);
     }
     if (_list.ContainsKey(id))
     {
         if (quantity == 0)
         {
             _list.Remove(id);
             return(true);
         }
         else
         {
             if (GetProduct(id).ProductQuantity - quantity > 0)
             {
                 GetProduct(id).ProductQuantity           -= quantity;
                 StoreList.GetProduct(id).ProductQuantity += quantity;
             }
             else if (GetProduct(id).ProductQuantity - quantity == 0)
             {
                 //StoreList.GetProduct(id).ProductQuantity += quantity;
                 DeleteProduct(id);
             }
             else
             {
                 return(false);
             }
             return(true);
         }
     }
     return(false);
 }
Esempio n. 2
0
        private bool RemoveProduct()
        {
            if (Basket.isEmpty())
            {
                Console.WriteLine("Basktet is empty, functionality disabled!");
                return(false);
            }
            Console.Write("Enter id of product to remove/remove from: ");
            string id = Console.ReadLine();

            if (Basket.GetProduct(id) == null)//check if product exists in basket
            {
                Console.WriteLine("Item entered is not in basket!");
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                return(false);
            }
            try
            {
                Console.Write("Enter quantity to remove: ");
                double quan = Convert.ToDouble(Console.ReadLine());
                if (quan < 0)
                {
                    throw new FormatException();
                }
                if (quan != 0)//dont delete if quantity is 0
                {
                    if (Basket.DeleteProduct(id, quan))
                    {
                        StoreList.AddProduct(id, quan);
                        SaveUser();
                        if (Basket.isEmpty())
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong value entered!");
                Console.Write("Press any key to continue...");
                Console.ReadKey();
            }
            return(true);
        }
Esempio n. 3
0
 public void Add(Product P, double quantity = 0)
 {
     P.TimeOfPurchase = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", null);
     try
     {
         _list.Add(P.ProductId, P);
         //StoreList.GetProduct(P.ProductId).ProductQuantity -= quantity;
         StoreList.SaveStoreList();
     }
     catch (ArgumentException)
     {
         GetProduct(P.ProductId).ProductQuantity += quantity;
     }
 }
Esempio n. 4
0
 public override void ViewStoreProducts(double threshold = 0)
 {
     if (!StoreList.isEmpty())
     {
         if (threshold >= 0)
         {
             StoreList.ViewStoreList(false);
         }
     }
     else
     {
         Console.WriteLine("Store is empty!");
     }
 }
Esempio n. 5
0
        private void Search()
        {
            if (StoreList.isEmpty())
            {
                Console.WriteLine("Store is empty, nothing to search for!");
                return;
            }
            Dictionary <int, string> categories = new Dictionary <int, string>();
            int idx = 1;

            foreach (Product p in StoreList.List.Values)
            {
                if (!categories.ContainsValue(p.ProductCategory) && p.ProductQuantity > 0)
                {
                    categories.Add(idx++, p.ProductCategory);
                }
            }
            if (categories.Count == 0)
            {
                Console.WriteLine("Store is empty, nothing to search for!");
                return;
            }
            foreach (KeyValuePair <int, string> d in categories)
            {
                Console.WriteLine(d.Key + ". " + d.Value);
            }

            try
            {
                Console.Write("Enter your choice(1-{0}): ", categories.Count);
                int choice = Convert.ToInt32(Console.ReadLine());
                if (!(choice >= 1 && choice <= categories.Count))
                {
                    throw new FormatException();
                }
                Console.WriteLine("{0,-25} {1,-25} {2,-25} {3,-10} {4,-10}", "ID", "Name", "Category", "Price", "Quantity");
                foreach (Product p in StoreList.List.Values)
                {
                    if (p.ProductCategory == categories[choice] && p.ProductQuantity > 0)
                    {
                        p.Print();
                    }
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong value entered or category doesn\'t exist!");
            }
        }
Esempio n. 6
0
        private void Search()
        {
            Console.Write("Enter ProductId to look for: ");
            string id = Console.ReadLine();

            if (StoreList.GetProduct(id) != null)
            {
                Console.WriteLine("{0,-25} {1,-25} {2,-25} {3,-10} {4,-10}", "ID", "Name", "Category", "Price", "Quantity");
                StoreList.GetProduct(id).Print();
            }
            else
            {
                Console.WriteLine("Poduct with Id ({0}) does not exist!", id);
            }
        }
Esempio n. 7
0
        public override void AddProduct()
        {
            if (StoreList.isEmpty())
            {
                Console.WriteLine("Store is empty!");
                return;
            }
            Console.WriteLine("Available products");
            StoreList.ViewStoreList(false);
            Console.WriteLine("Products in basket");
            Basket.ViewBasket();
            Console.Write("Enter product ID to add to basket: ");
            string tempID = Console.ReadLine();

            try
            {
                Product temp = StoreList.GetProduct(tempID).Clone();
                if (temp.ProductQuantity == 0)
                {
                    throw new NullReferenceException();
                }
                Console.Write("Enter quantity: ");
                double uquan = Convert.ToDouble(Console.ReadLine());
                if (uquan < 0)
                {
                    throw new FormatException();
                }
                if (temp.ProductQuantity - uquan >= 0)
                {
                    temp.ProductQuantity = uquan;
                    StoreList.GetProduct(tempID).ProductQuantity -= uquan;
                    Basket.Add(temp, uquan);
                    SaveUser();
                }
                else
                {
                    Console.WriteLine("Desired quantity is unavailable!");
                }
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("Product doesn\'t exist!");
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong value entered!");
            }
        }
Esempio n. 8
0
        private void DeleteProductFromStore()
        {
            ViewList();
            Console.Write("Enter ID of product to remove: ");
            string uchoice = Console.ReadLine();
            double quan    = 0;

            if (StoreList.GetProduct(uchoice) != null)
            {
                if (StoreList.GetProduct(uchoice).ProductQuantity != 0)
                {
                    Console.Write("Enter quantity: ");
                    try
                    {
                        quan = Convert.ToDouble(Console.ReadLine());
                        if (quan > StoreList.GetProduct(uchoice).ProductQuantity)
                        {
                            Console.WriteLine("Can\'t remove more than available quantity");
                            throw new FormatException();
                        }
                        if (quan < 0)
                        {
                            throw new FormatException();
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Wrong value entered!");
                        return;
                    }
                }
                if (!StoreList.DeleteProduct(uchoice, quan))
                {
                    Console.WriteLine("Wrong choice!");
                }
            }
            else
            {
                Console.WriteLine("Product does not exist!");
            }
        }
Esempio n. 9
0
        public override void AddProduct()
        {
            string id;

            Console.Write("Please enter product ID: ");
            id = Console.ReadLine();
            if (StoreList.GetProduct(id) != null)
            {
                Console.Write("Product already exists, would you like to add by a certain quantity?(Y/N): ");
                string uchoice = Console.ReadLine().ToUpper();
                try
                {
                    if (uchoice == "Y")
                    {
                        while (true)
                        {
                            Console.Write("Enter quantity to increase by: ");
                            double quan = Convert.ToDouble(Console.ReadLine());
                            if (quan < 0)
                            {
                                throw new FormatException();
                            }
                            StoreList.AddProduct(id, quan);
                            break;
                        }
                    }
                    else if (uchoice != "N")
                    {
                        throw new FormatException();
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Wrong value entered!");
                }
            }
            else
            {
                StoreList.AddProduct(id);
            }
        }
Esempio n. 10
0
 public override void ViewStoreProducts(double threshold)
 {
     if (StoreList.isEmpty())
     {
         Console.WriteLine("Store is empty!");
         return;
     }
     if (threshold > 0)
     {
         Console.WriteLine("{0,-25} {1,-25} {2,-25} {3,-10} {4,-10}", "ID", "Name", "Category", "Price", "Quantity");
         foreach (Product p in StoreList.List.Values)
         {
             if (p.ProductQuantity >= threshold)
             {
                 p.Print();
             }
         }
     }
     else
     {
         Console.WriteLine("Wrong value entered!");
     }
 }