Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Dictionary <string, PriceAndQuantity> productDictionary = new Dictionary <string, PriceAndQuantity>();

            string input = Console.ReadLine();

            while (input != "buy")
            {
                PriceAndQuantity newProduct = new PriceAndQuantity();
                newProduct.Price    = double.Parse(input.Split()[1]);
                newProduct.Quantity = int.Parse(input.Split()[2]);
                string productName = input.Split()[0];

                if (productDictionary.ContainsKey(productName))
                {
                    productDictionary[productName].Price     = newProduct.Price;
                    productDictionary[productName].Quantity += newProduct.Quantity;
                }
                else
                {
                    productDictionary.Add(productName, newProduct);
                }
                input = Console.ReadLine();
            }
            foreach (var product in productDictionary)
            {
                Console.WriteLine($"{product.Key} -> {product.Value.Price*product.Value.Quantity:f2}");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string input = Console.ReadLine();

            var products = new Dictionary <string, PriceAndQuantity>();

            while (input != "buy")
            {
                string[] product           = input.Split(' ');
                string   nameOfProduct     = product[0];
                double   priceOfProduct    = double.Parse(product[1]);
                int      quantityOfProduct = int.Parse(product[2]);

                if (products.ContainsKey(nameOfProduct))
                {
                    products[nameOfProduct].Quantity += quantityOfProduct;
                    products[nameOfProduct].Price     = priceOfProduct;
                }
                else
                {
                    PriceAndQuantity priceAndQuantity = new PriceAndQuantity
                    {
                        Price    = priceOfProduct,
                        Quantity = quantityOfProduct
                    };

                    products.Add(nameOfProduct, priceAndQuantity);
                }

                input = Console.ReadLine();
            }

            foreach (var product in products)
            {
                double totalPrice = product.Value.Price * product.Value.Quantity;

                Console.WriteLine($"{product.Key} -> {totalPrice:F2}");
            }
        }