Advertisement
_CodeBehind

ShopDatabase

Jun 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class ShopDatabase
  6. {
  7.     public static void Main()
  8.     {
  9.         string input;
  10.  
  11.         var products = new Dictionary<string, Dictionary<double, int>>();
  12.  
  13.         while ((input = Console.ReadLine()) != "stocked")
  14.         {
  15.             var tokens = input
  16.                 .Split()
  17.                 .ToArray();
  18.  
  19.             var productName = tokens[0];
  20.             var productPrice = double.Parse(tokens[1]);
  21.             var quantity = int.Parse(tokens[2]);
  22.  
  23.             if (!products.ContainsKey(productName))
  24.             {
  25.                 products[productName] = new Dictionary<double, int>();
  26.                 products[productName].Add(productPrice, quantity);
  27.             }
  28.             else
  29.             {
  30.                 products[productName].Add(productPrice, quantity);
  31.             }
  32.         }
  33.         double totalPrice = 0d;
  34.         foreach (var currentProduct in products)
  35.         {
  36.             var productName = currentProduct.Key;
  37.             var lastProductPrice = currentProduct.Value.Keys.Last();
  38.             var sumOfQuantities = currentProduct.Value.Values.Sum();
  39.             var finalPricesForTheCurrentProduct = lastProductPrice * sumOfQuantities;
  40.             Console.WriteLine($"{productName}: ${lastProductPrice:f2} * {sumOfQuantities} = ${finalPricesForTheCurrentProduct:f2}");
  41.             totalPrice += finalPricesForTheCurrentProduct;
  42.         }
  43.         Console.WriteLine("------------------------------");
  44.         Console.WriteLine($"Grand Total: ${totalPrice:f2}");
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement