예제 #1
0
 public void AddPersist(PersistModel persist)
 {
     MyModel.Add(persist);
 }
예제 #2
0
        /// <summary>
        /// The method implements all options for adding goods to the warehouse.
        /// </summary>
        /// <param name="storageMethod"></param>
        /// <param name="typeOfProduct"></param>
        /// <param name="countItems"></param>
        /// <param name="yesNo"></param>
        /// <param name="idProduct"></param>
        #region CaseNumberAddMenu
        public void CaseNumberAddMenu(string storageMethod, string typeOfProduct, ref int countItems, ref string yesNo, ref byte idProduct)
        {
            Console.Clear();

            // Name, count and price of the goods added to the warehouse.
            string nameProduct = "No name";

            int countProduct = 0;

            decimal priceProduct = 0m;

            Console.Write($"Enter the name of the {typeOfProduct}: ");
            nameProduct = Console.ReadLine();

            string.Intern(nameProduct);

            // Checking whether there is already an item with the same name in stock.
            for (int index = 0; index < _products.Products.Count; index++)
            {
                if (nameProduct == _products.Products[index].Name)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("There is already a product with this name.\n");
                    Console.ForegroundColor = ConsoleColor.Gray;

                    return;
                }
            }

            // All properties of the added product, except for the name, must be entered
            // taking into account the check for compliance of types and values.
            Console.Write($"Enter the number of {storageMethod}: ");
            while (true)
            {
                try
                {
                    countProduct = int.Parse(Console.ReadLine());

                    var condition = countProduct > 0 && countProduct < 101;

                    if (condition)
                    {
                        break;
                    }
                    else
                    {
                        throw new Exception($"The warehouse can store no more than 100 {storageMethod}.\nEnter a number from 1 to 100: ");
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        if (ex.GetType().ToString() == "System.FormatException")
                        {
                            throw new FormatException($"The warehouse can store no more than 100 boxes {storageMethod}.\nEnter a number from 1 to 100: ");
                        }
                        else if (ex.GetType().ToString() == "System.OverflowException")
                        {
                            throw new OverflowException($"The warehouse can store no more than 100 {storageMethod}.\nEnter a number from 1 to 100:  ");
                        }
                        else
                        {
                            Console.Clear();

                            Console.WriteLine($"Enter the number of {storageMethod} {nameProduct}.");

                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write(ex.Message);
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                    }
                    catch (OverflowException exc)
                    {
                        Console.Clear();

                        Console.WriteLine($"Enter the number of {storageMethod} {nameProduct}.");

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(exc.Message);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    catch (FormatException exc)
                    {
                        Console.Clear();

                        Console.WriteLine($"Enter the number of {storageMethod} {nameProduct}.");

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(exc.Message);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
            }

            Console.Write($"Enter the price for {nameProduct}: ");
            while (true)
            {
                try
                {
                    priceProduct = decimal.Parse(Console.ReadLine());

                    break;
                }
                catch (Exception ex)
                {
                    try
                    {
                        if (ex.GetType().ToString() == "System.FormatException")
                        {
                            throw new FormatException("Enter a price value, for example 2,99: ");
                        }
                        else if (ex.GetType().ToString() == "System.OverflowException")
                        {
                            throw new OverflowException("Enter the real price for this product: ");
                        }
                        else
                        {
                            Console.Clear();

                            Console.WriteLine($"Enter the number of {storageMethod} {nameProduct}.");

                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write(ex.Message);
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                    }
                    catch (OverflowException exc)
                    {
                        Console.Clear();

                        Console.WriteLine($"Enter the price {nameProduct}.");

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(exc.Message);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    catch (FormatException exc)
                    {
                        Console.Clear();

                        Console.WriteLine($"Enter the price {nameProduct}.");

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(exc.Message);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
            }

            // Increase in the number of items of goods stored in the warehouse.
            countItems++;

            // Zeroing the string yes/no for further correct use.
            yesNo = "";

            // Adding a product with the entered field values.
            _products.Add(new Product(nameProduct, countProduct, priceProduct, idProduct));

            Console.Clear();
        }