valchak

Supermarket_Database

Jun 17th, 2017
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Supermarket_Database
  6. {
  7.     public static void Main()
  8.     {
  9.         var productNameQuantityDict = new Dictionary<string, long>();
  10.         var productNamePriceDict = new Dictionary<string, double>();
  11.  
  12.         var inputLine = Console.ReadLine();
  13.  
  14.         while (inputLine != "stocked")
  15.         {
  16.             var token = inputLine
  17.                 .Trim()
  18.                 .Split(' ')
  19.                 .ToArray();
  20.  
  21.             var productName = token[0];
  22.             var productPrice = double.Parse(token[1]);
  23.             var productQuantity= long.Parse(token[2]);
  24.  
  25.             if (!productNameQuantityDict.ContainsKey(productName))
  26.             {
  27.                 productNameQuantityDict[productName] = 0;
  28.                 productNamePriceDict[productName] = productPrice;
  29.             }
  30.  
  31.             productNameQuantityDict[productName] += productQuantity;
  32.             productNamePriceDict[productName] = productPrice;
  33.  
  34.             inputLine = Console.ReadLine();
  35.         }
  36.  
  37.         var grandTotal = 0d;
  38.         foreach (var product in productNamePriceDict)
  39.         {
  40.             foreach (var productQuantity in productNameQuantityDict)
  41.             {
  42.                 if (productQuantity.Key == product.Key)
  43.                 {
  44.                     var totalPrice = product.Value * productQuantity.Value;
  45.                     grandTotal += totalPrice;
  46.                     Console.WriteLine($"{product.Key}: " +
  47.                                       $"${product.Value:f2} * {productQuantity.Value} = ${totalPrice:f2}");
  48.                 }
  49.             }          
  50.         }
  51.  
  52.         Console.WriteLine(new string('-', 30));
  53.         Console.WriteLine($"Grand Total: ${grandTotal:f2}");
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment