Advertisement
shady_obeyd

03.ShoppingSpree

Jul 20th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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 _03.ShoppingSpree
  8. {
  9.     class ShoppingSpree
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, double> products = new Dictionary<string, double>();
  14.             double budget = double.Parse(Console.ReadLine());
  15.             string input = Console.ReadLine();
  16.             double sum = 0;
  17.  
  18.             while (input != "end")
  19.             {
  20.                 string[] tokens = input.Split(' ');
  21.                 string productName = tokens[0];
  22.                 double productPrice = double.Parse(tokens[1]);
  23.  
  24.                 if (products.ContainsKey(productName))
  25.                 {
  26.                     if (products[productName] > productPrice)
  27.                     {
  28.                         products[productName] = productPrice;
  29.                     }
  30.                 }
  31.                 else
  32.                 {
  33.                     products[productName] = productPrice;
  34.                 }
  35.  
  36.                 input = Console.ReadLine();
  37.             }
  38.  
  39.             Dictionary<string, double> sortedProducts =
  40.                 products
  41.                 .OrderByDescending(p => p.Value)
  42.                 .ThenBy(n => n.Key.Length)
  43.                 .ToDictionary(k => k.Key, v => v.Value);
  44.  
  45.             foreach (double price in sortedProducts.Values)
  46.             {
  47.                 sum += price;
  48.             }
  49.  
  50.             if (sum > budget)
  51.             {
  52.                 Console.WriteLine("Need more money... Just buy banichka");
  53.             }
  54.             else
  55.             {
  56.                 foreach (KeyValuePair<string, double> item in sortedProducts)
  57.                 {
  58.                     string productName = item.Key;
  59.                     double productPrice = item.Value;
  60.  
  61.                     Console.WriteLine($"{productName} costs {productPrice:f2}");
  62.                 }
  63.             }
  64.  
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement