Advertisement
momchilovv

04.Supermarket Database

Jun 19th, 2017
143
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. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SupermarketDatabase
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var dict = new Dictionary<string, List<decimal>>();
  14.             var input = Console.ReadLine().Split(' ').ToArray();
  15.  
  16.             while (input[0] != "stocked")
  17.             {
  18.                 var item = input[0].ToString();
  19.                 var price = decimal.Parse(input[1]);
  20.                
  21.                 var quantity = decimal.Parse(input[2]);
  22.  
  23.                 if (!dict.ContainsKey(item))
  24.                 {
  25.                     dict[item] = new List<decimal>();
  26.                     dict[item].Add(price);
  27.                     dict[item].Add(quantity);
  28.                 }
  29.  
  30.                 else
  31.                 {
  32.                     dict[item][0] = price;
  33.                     dict[item][1] += quantity;
  34.                 }
  35.  
  36.                 input = Console.ReadLine().Split(' ').ToArray();
  37.             }
  38.             var total = 0m;
  39.             foreach (var w in dict)
  40.             {
  41.                 total += w.Value[0] * w.Value[1];
  42.                 Console.WriteLine($"{w.Key}: ${w.Value[0]:f2} * {w.Value[1]} = ${(w.Value[0] * w.Value[1]):f2}");
  43.             }
  44.             Console.WriteLine(new string('-', 30));
  45.             Console.WriteLine($"Grand Total: ${total:F2}");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement