示例#1
0
        public void ExecuteCommand(string[] cmd, ProductINFO product = null)
        {
            if (Equals(cmd[0], "all"))
            {
                ShowProducts();
            }

            else if (Equals(cmd[0], "clear"))
            {
                if (ProductINFO.Products.Count() != 0)
                {
                    ProductINFO.Products.RemoveAll(x => string.Equals(x.Date.ToString("dd-MM-yyyy"), Convert.ToDateTime(cmd[1]).ToString("dd-MM-yyyy")));
                }
                ShowProducts();
            }

            else if (Equals(cmd[0], "purchase"))
            {
                double   price;
                DateTime date;
                DateTime.TryParseExact(cmd[1], "dd-MM-yyyy", CultureInfo.InvariantCulture,
                                       DateTimeStyles.None, out date); // cmd[1] == date
                Double.TryParse(cmd[2], out price);                    // cmd[2] == Price
                product.Currency = cmd[3];                             // cmd[3] == currency
                product.Name     = cmd[4];                             // cmd[4] == name
                product.Price    = price;
                product.Date     = date;

                ProductINFO.Products.Add(product);
                ShowProducts();
            }

            else if (Equals(cmd[0], "report"))
            {
                int year = Int32.Parse(cmd[1]);
                ProductINFO.ProfitPerYear[year] = 0;
                if (ProductINFO.Products.Count() != 0)
                {
                    Fixer  fixer = new Fixer();
                    string str   = string.Empty;
                    var    prods = from p in ProductINFO.Products
                                   where p.Date.Year == year
                                   select p; // taking products bought during input year
                    foreach (ProductINFO p in prods)
                    {
                        if (string.Equals(p.Currency, cmd[2]))
                        {
                            ProductINFO.ProfitPerYear[year] += p.Price; // adding products with input currency
                        }
                        else
                        {
                            str = fixer.FormString(p.Price, p.Currency, cmd[2]);
                            ProductINFO.ProfitPerYear[year] += fixer.GetPrice(str, p.Price, cmd[2]); // converting price to input currency
                        }
                    }
                }
                Console.WriteLine($"Earnings in {cmd[1]}: {ProductINFO.ProfitPerYear[year]} {cmd[2]}");
            }
        }
示例#2
0
        public static void Menu()
        {
            string command;    // command to write

            string[]    words; // command splitted into separated instructions
            Executer    executer = new Executer();
            ProductINFO product  = null;
            bool        stop     = false; // indicates the stop of program
            string      Answer;           // "Yes" to stop the program | "No" to continue

            do
            {
                Console.WriteLine("Enter a command to execute: ");
                Console.Write("-> ");
                command = Console.ReadLine();
                words   = command.Split(' ');
                if (words.Length == 5)
                {
                    product = new ProductINFO();  // [CMD] purchase requires 5 arguments
                }
                if (!executer.CheckParams(words)) // checking the parameters
                {
                    Console.WriteLine("[Error] Uncorrect command");
                    continue;
                }
                executer.ExecuteCommand(words, product); // Execute the command
                Console.WriteLine("Would you like to enter one more command?(Print: Yes or No)");
                Console.Write("-> ");
                Answer = Console.ReadLine();
                while (!string.Equals(Answer, "Yes") && !string.Equals(Answer, "No")) // Uncorrect answer
                {
                    Console.WriteLine("Enter correct answer:");
                    Console.Write("-> ");
                    Answer = Console.ReadLine();
                }
                if (string.Equals(Answer, "Yes"))
                {
                    stop = false;
                }
                else if (string.Equals(Answer, "No"))
                {
                    stop = true;
                }
            } while (!stop);
        }