Advertisement
YavorGrancharov

Shopping_Spree(lambda)

Jul 18th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Shopping_Spree
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, decimal> stock = new Dictionary<string, decimal>();
  12.  
  13.             decimal budget = decimal.Parse(Console.ReadLine());
  14.  
  15.             string[] input = Console.ReadLine().Split(' ');
  16.  
  17.             while (input[0] != "end")
  18.             {
  19.                 string productName = input[0];
  20.                 decimal productPrice = decimal.Parse(input[1]);
  21.  
  22.                 if (!stock.ContainsKey(productName))
  23.                 {
  24.                     stock[productName] = productPrice;
  25.                 }
  26.                 else
  27.                 {
  28.                     if (stock[productName] > productPrice)
  29.                     {
  30.                         stock[productName] = productPrice;
  31.                     }
  32.                 }
  33.                 input = Console.ReadLine().Split(' ');
  34.             }
  35.  
  36.             decimal sum = stock.Values.Sum();
  37.  
  38.             if (sum > budget)
  39.             {
  40.                 Console.WriteLine("Need more money... Just buy banichka");
  41.             }
  42.             else
  43.             {
  44.                 stock = stock
  45.                     .OrderByDescending(p => p.Value)
  46.                     .ThenBy(v => v.Key.Length)
  47.                     .ToDictionary(v => v.Key, p => p.Value);
  48.  
  49.                 foreach (KeyValuePair<string, decimal> kvp in stock)
  50.                 {
  51.                     Console.WriteLine("{0} costs {1:F2}", kvp.Key, kvp.Value);
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement