JulianJulianov

09.AsociativeArrays-Orders

Mar 14th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. 09.Orders
  2. Write a program that keeps information about products and their prices. Each product has a name, a price and a quantity. If the product doesn’t exist yet, add it with its starting quantity.
  3. If you receive a product, which already exists, increase its quantity by the input quantity and if its price is different, replace the price as well.
  4. You will receive products’ names, prices and quantities on new lines. Until you receive the command "buy", keep adding items. When you do receive the command "buy", print the items with their names and total price of all the products with that name.
  5. Input
  6. • Until you receive "buy", the products will be coming in the format: "{name} {price} {quantity}".
  7. • The product data is always delimited by a single space.
  8. Output
  9. • Print information about each product in the following format:
  10. "{productName} -> {totalPrice}"
  11. • Format the average grade to the 2nd digit after the decimal separator.
  12. Examples
  13. Input                                                                Output
  14. Beer 2.20 100                                                        Beer -> 220.00
  15. IceTea 1.50 50                                                       IceTea -> 75.00
  16. NukaCola 3.30 80                                                     NukaCola -> 264.00
  17. Water 1.00 500                                                       Water -> 500.00
  18. buy                                                                    
  19.                                                                      
  20.  
  21. Beer 1.20 200                                                        Beer -> 660.00
  22. IceTea 0.50 120                                                      Water -> 250.00
  23. Water 1.25 200                                                       IceTea -> 110.00
  24. IceTea 5.20 100                                                      
  25. Beer 2.40 350                                                        
  26. buy
  27.    
  28. Beer 1.35 350                                                        CesarSalad -> 255.00
  29. IceCream 1.50 25                                                     SuperEnergy -> 320.00
  30. CesarSalad 10.20 25                                                  Beer -> 472.50  
  31. SuperEnergy 0.80 400                                                 IceCream -> 37.50                                                            
  32. buy
  33.  
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Linq;
  37.  
  38. namespace _04Orders
  39. {
  40.     class Program
  41.     {
  42.         static void Main(string[] args)
  43.         {
  44.             var command = Console.ReadLine().Split();
  45.             var price = new Dictionary<string, double>();
  46.             var quantity = new Dictionary<string, double>();
  47.             while (command[0] != "buy")
  48.             {
  49.                 if (price.ContainsKey(command[0]) == false)
  50.                 {
  51.                     price.Add(command[0], double.Parse(command[1]));
  52.                     quantity.Add(command[0], double.Parse(command[2]));
  53.                 }
  54.                 else
  55.                 {
  56.                     if (price[command[0]] > double.Parse(command[1]))//Чрез "command[0]", който е всъщност "Key" се достъпва до
  57.                     {                                                //стойността на "Value"!
  58.                         price[command[0]] = double.Parse(command[1]);//Така "Value" приема нова стойност!
  59.                     }
  60.                     quantity[command[0]] += double.Parse(command[2]);//Тук "Value" се увеличава!
  61.                 }
  62.                 command = Console.ReadLine().Split();
  63.             }
  64.             foreach (var item in price)
  65.             {
  66.                 double sum = item.Value * quantity[item.Key];//Чрез "item.Key", който е общ за двата речника "price" и "quantity" се
  67.                                                              //достъпва стойността "Value"!
  68.                 Console.WriteLine($"{item.Key} -> {sum:F2}");
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment