Advertisement
Spocoman

03. Orders

Apr 8th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Orders
  6. {
  7.     class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             Dictionary<string, double> productsAndPrices = new Dictionary<string, double>();
  12.             Dictionary<string, int> ordersQuantity = new Dictionary<string, int>();
  13.  
  14.             string command;
  15.  
  16.             while ((command = Console.ReadLine()) != "buy")
  17.             {
  18.                 string[] currentOrder = command.Split(" ");
  19.  
  20.                 string product = currentOrder[0];
  21.                 double price = double.Parse(currentOrder[1]);
  22.                 int quantity = int.Parse(currentOrder[2]);
  23.  
  24.                 if (!productsAndPrices.ContainsKey(product))
  25.                 {
  26.                     productsAndPrices.Add(product, 0);
  27.                     ordersQuantity.Add(product, 0);
  28.                 }
  29.  
  30.                 productsAndPrices[product] = price;
  31.                 ordersQuantity[product] += quantity;
  32.             }
  33.  
  34.             foreach (var order in ordersQuantity)
  35.             {
  36.                 double orderPrice = productsAndPrices[order.Key] * order.Value;
  37.                 Console.WriteLine($"{order.Key} -> {orderPrice:f2}");
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement