Ejemplo n.º 1
0
    private static void DeleteProducts(ShoppingCenter shoppingCenter, string[] args)
    {
        if (!args.Any())
        {
            return;
        }

        var count = 0;

        if (args.Length > 1)
        {
            var name     = args[0];
            var producer = args[1];
            count = shoppingCenter.DeleteProductsByNameAndProducer(name, producer);
        }
        else
        {
            count = shoppingCenter.DeleteProductsByProducer(args[0]);
        }

        Print(count);
    }
Ejemplo n.º 2
0
    static void Main()
    {
        int            commandsNumber = int.Parse(Console.ReadLine());
        ShoppingCenter center         = new ShoppingCenter();

        for (int i = 0; i < commandsNumber; i++)
        {
            string line       = Console.ReadLine();
            int    firstSpace = line.IndexOf(" ");

            string   command = line.Substring(0, firstSpace);
            string[] tokens  = line.Substring(firstSpace + 1).Split(';');


            switch (command)
            {
            case "AddProduct":
            {
                string name     = tokens[0];
                double price    = double.Parse(tokens[1]);
                string producer = tokens[2];

                Product p = new Product(name, price, producer);
                center.Add(p);
                Console.WriteLine("Product added");
                break;
            }

            case "DeleteProducts":
                if (tokens.Length == 1)
                {
                    int count = center.DeleteProductsByProducer(tokens[0]);

                    if (count == 0)
                    {
                        Console.WriteLine("No products found");
                    }
                    else
                    {
                        Console.WriteLine(count.ToString() + " products deleted");
                    }
                }
                else
                {
                    int count = center.DeleteProductsByProducerAndName(
                        tokens[0],
                        tokens[1]);
                    if (count == 0)
                    {
                        Console.WriteLine("No products found");
                    }
                    else
                    {
                        Console.WriteLine(count.ToString() + " products deleted");
                    }
                }
                break;

            case "FindProductsByName":

                List <Product> result =
                    center.FindProductsByName(tokens[0]).ToList();
                if (result.Count != 0)
                {
                    Console.WriteLine(string.Join("\n", result));
                }
                else
                {
                    Console.WriteLine("No products found");
                }
                break;

            case "FindProductsByProducer":
                List <Product> result2 =
                    center.FindProductsByProducer(tokens[0]).ToList();

                if (result2.Count != 0)
                {
                    Console.WriteLine(string.Join("\n", result2));
                }
                else
                {
                    Console.WriteLine("No products found");
                }

                break;

            case "FindProductsByPriceRange":
                IEnumerable <Product> result3 =
                    center.FindProductsByPriceRange(
                        double.Parse(tokens[0]),
                        double.Parse(tokens[1]))
                    .OrderBy(x => x);

                if (result3.Any())
                {
                    Console.WriteLine(string.Join("\n", result3));
                }
                else
                {
                    Console.WriteLine("No products found");
                }
                break;
            }
        }
    }
Ejemplo n.º 3
0
    static void Main()
    {
        int            commandsNumber = int.Parse(Console.ReadLine());
        ShoppingCenter shoppingCenter = new ShoppingCenter();

        for (int i = 0; i < commandsNumber; i++)
        {
            string line       = Console.ReadLine();
            int    firstSpace = line.IndexOf(" ");

            string   command = line.Substring(0, firstSpace);
            string[] tokens  = line.Substring(firstSpace + 1).Split(';');

            switch (command)
            {
            case "AddProduct":
                string name     = tokens[0];
                double price    = double.Parse(tokens[1]);
                string producer = tokens[2];

                Product p = new Product(name, price, producer);

                shoppingCenter.Add(p);
                Console.WriteLine("Product added");
                break;

            case "DeleteProducts":
                if (tokens.Length == 1)
                {
                    producer = tokens[0];
                    int countDeleted = shoppingCenter.DeleteProductsByProducer(producer);

                    if (countDeleted == 0)
                    {
                        Console.WriteLine("No products found");
                    }
                    else
                    {
                        Console.WriteLine(countDeleted.ToString() + " products deleted");
                    }
                }
                else
                {
                    name     = tokens[0];
                    producer = tokens[1];
                    int countDeleted = shoppingCenter.DeleteProductsByProducerAndName(name, producer);

                    if (countDeleted == 0)
                    {
                        Console.WriteLine("No products found");
                    }
                    else
                    {
                        Console.WriteLine(countDeleted.ToString() + " products deleted");
                    }
                }

                break;

            case "FindProductsByName":
                name = tokens[0];
                List <Product> result = shoppingCenter.FindProductsByName(name).ToList();
                if (result.Count != 0)
                {
                    Console.WriteLine(string.Join(Environment.NewLine, result));
                }
                else
                {
                    Console.WriteLine("No products found");
                }

                break;

            case "FindProductsByProducer":
                producer = tokens[0];
                result   = shoppingCenter.FindProductsByProducer(producer).ToList();
                if (result.Count != 0)
                {
                    Console.WriteLine(string.Join(Environment.NewLine, result));
                }
                else
                {
                    Console.WriteLine("No products found");
                }

                break;

            case "FindProductsByPriceRange":
                double priceLow  = double.Parse(tokens[0]);
                double priceHigh = double.Parse(tokens[1]);
                result = shoppingCenter.FindProductsByPriceRange(priceLow, priceHigh)
                         .ToList();

                if (result.Count != 0)
                {
                    Console.WriteLine(string.Join(Environment.NewLine, result));
                }
                else
                {
                    Console.WriteLine("No products found");
                }

                break;
            }
        }
    }
Ejemplo n.º 4
0
    public static void Main()
    {
        var sc = new ShoppingCenter();

        var commands = int.Parse(Console.ReadLine());

        for (int i = 0; i < commands; i++)
        {
            string line = Console.ReadLine();

            int firstSpace = line.IndexOf(" ");

            string   command = line.Substring(0, firstSpace);
            string[] tokens  = line.Substring(firstSpace + 1).Split(';');

            switch (command)
            {
            case "AddProduct":
            {
                string addName     = tokens[0];
                double addPrice    = double.Parse(tokens[1]);
                string addProducer = tokens[2];
                sc.AddProduct(addName, addPrice, addProducer);
                Console.WriteLine("Product added");
                break;
            }

            case "DeleteProducts":
            {
                int count = 0;
                if (tokens.Length == 1)
                {
                    string currProducer = tokens[0];
                    count = sc.DeleteProductsByProducer(currProducer);
                }
                else
                {
                    string deleteName     = tokens[0];
                    string deleteProducer = tokens[1];
                    count = sc.DeleteProductsByProducerAndName(deleteName, deleteProducer);
                }
                PrintRemoveMessage(count);
                break;
            }

            case "FindProductsByName":
            {
                string findByName = tokens[0];
                PrintProducts(sc.FindProductsByName(findByName).ToList());
                break;
            }

            case "FindProductsByProducer":
            {
                string findByProd = tokens[0];
                PrintProducts(sc.FindProductsByProducer(findByProd).ToList());
                break;
            }

            case "FindProductsByPriceRange":
            {
                double lowerRange = double.Parse(tokens[0]);
                double upperRange = double.Parse(tokens[1]);

                PrintProducts(sc.FindProductsByPriceRange(lowerRange, upperRange).OrderBy(x => x).ToList());
                break;
            }
            }
        }
    }
Ejemplo n.º 5
0
    public static void Main()
    {
        int            commandsNumber = int.Parse(Console.ReadLine());
        ShoppingCenter center         = new ShoppingCenter();

        for (int i = 0; i < commandsNumber; i++)
        {
            string line = Console.ReadLine();

            int firstSpace = line.IndexOf(" ");

            string         command = line.Substring(0, firstSpace);
            string[]       args    = line.Substring(firstSpace + 1).Split(';');
            List <Product> result  = new List <Product>();
            switch (command)
            {
            case "AddProduct":
                string name     = args[0];
                double price    = double.Parse(args[1]);
                string producer = args[2];

                Product p = new Product(name, price, producer);
                center.Add(p);
                Console.WriteLine("Product added");
                break;

            case "DeleteProducts":
                int count = 0;
                if (args.Length == 1)      // delete producer
                {
                    count = center.DeleteProductsByProducer(args[0]);
                }
                else
                {
                    count = center.DeleteProductsByProducerAndName(
                        args[0],
                        args[1]);
                }
                if (count == 0)
                {
                    Console.WriteLine("No products found");
                }
                else
                {
                    Console.WriteLine(count + " products deleted");
                }
                break;

            case "FindProductsByName":
                result = center.FindProductsByName(args[0]).ToList();
                break;

            case "FindProductsByProducer":
                result = center.FindProductsByProducer(args[0]).ToList();
                break;

            case "FindProductsByPriceRange":
                result = center.FindProductsByPriceRange(
                    double.Parse(args[0]),
                    double.Parse(args[1]))
                         .OrderBy(x => x)
                         .ToList();
                break;
            }
            if (command.StartsWith("Find"))
            {
                if (result.Count != 0)
                {
                    Console.WriteLine(string.Join("\n", result));
                }
                else
                {
                    Console.WriteLine("No products found");
                }
            }
        }
    }