Advertisement
hrimar

More04SupermarketDatabase of Dictionaries - ProgFund

Jun 17th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace More04SupermarketDatabase
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             // Judge decide 40/100 because row 40 is .Min() instead of .Last()!
  12.             var products = Console.ReadLine();
  13.             var result = new Dictionary<string, Dictionary<double, double>>();
  14.             var totalAmount = 0.0;
  15.  
  16.                         while (products != "stocked")
  17.             {
  18.                 var product = products.Split().ToArray();
  19.                 var name = product[0];
  20.                 var price = double.Parse(product[1]);
  21.                 var quantity = double.Parse(product[2]);
  22.  
  23.                 if (!result.ContainsKey(name))
  24.                 {
  25.                     result[name] = new Dictionary<double, double>();
  26.                  
  27.                 }
  28.                 if (!result[name].ContainsKey(price))
  29.                 {
  30.                     result[name][price] = 0;        
  31.                 }
  32.  
  33.                 result[name][price]+= quantity;
  34.  
  35.                 products = Console.ReadLine();
  36.             }
  37.             foreach (var namePriceQuantity in result)
  38.             {
  39.                 var name = namePriceQuantity.Key;
  40.                 var price = namePriceQuantity.Value.Keys.Min(); //var price = namePriceQuantity.Value.Keys.Last();
  41.                 var quantity = namePriceQuantity.Value.Values.Sum();
  42.                 totalAmount += (quantity * price);
  43.                
  44.                 Console.WriteLine($"{name}: ${price:f2} * {quantity} = ${quantity * price:f2}");
  45.  
  46.             }
  47.             Console.WriteLine("------------------------------");
  48.             Console.WriteLine($"Grand Total: ${totalAmount:f2}");
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement