Advertisement
alexbancheva

4. Orders

Aug 4th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Orders_ExercisesAssArrays
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, decimal> productPrices = new Dictionary<string, decimal>();
  12.             Dictionary<string, long> productQuantity = new Dictionary<string, long>();
  13.  
  14.             string input;
  15.  
  16.             while ((input = Console.ReadLine()) != "buy")
  17.             {
  18.                 string[] productArgs = input
  19.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  20.                     .ToArray();
  21.  
  22.                 string name = productArgs[0];
  23.                 decimal price = decimal.Parse(productArgs[1]);
  24.                 int quantity = int.Parse(productArgs[2]);
  25.  
  26.                 if (!productQuantity.ContainsKey(name))
  27.                 {
  28.                     productQuantity[name] = 0;
  29.                     productPrices[name] = 0;
  30.                 }
  31.  
  32.                 productQuantity[name] += quantity;
  33.                 productPrices[name] = price;
  34.  
  35.             }
  36.  
  37.             foreach (var kvp in productPrices)
  38.             {
  39.                 string name = kvp.Key;
  40.                 decimal price = kvp.Value;
  41.                 long quantity = productQuantity[name];
  42.                 decimal totalProducts = price * quantity;
  43.  
  44.                 Console.WriteLine($"{name} -> {totalProducts:f2}");
  45.             }
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement