Advertisement
Aborigenius

MostValuedCustomer

Aug 16th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 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 MostValuedCustomer
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.             Dictionary<string, double> products = new Dictionary<string, double>();
  15.  
  16.             while (input != "Shop is open")
  17.             {
  18.                 string[] inputTokens = input.Split(' ');
  19.                 string productName = inputTokens[0];
  20.                 double productPrice = double.Parse(inputTokens[1]);
  21.  
  22.                 products[productName] = productPrice;
  23.                 input = Console.ReadLine();
  24.             }
  25.             string orders = Console.ReadLine();
  26.  
  27.             var buyersData = new Dictionary<string, List<string>>();
  28.             while (orders != "Print")
  29.             {
  30.                 string[] inputOrders = orders.Split(new string[] { ": ", ", " }, StringSplitOptions.RemoveEmptyEntries);
  31.                 string buyer = inputOrders[0];
  32.      
  33.                 if (orders == "Discount")
  34.                 {
  35.                    var discountProducts = products.OrderByDescending(x => x.Value)
  36.                         .Take(3).Select(x => new KeyValuePair<string, double>
  37.                         (x.Key, x.Value * 0.9));
  38.                     foreach (var dp in discountProducts)
  39.                     {
  40.                         products[dp.Key] = dp.Value;
  41.                     }
  42.              
  43.                 }
  44.                 else
  45.                 {
  46.                     if (!buyersData.ContainsKey(buyer))
  47.                     {
  48.                         buyersData.Add(buyer, new List<string>());
  49.                         for (int i = 1; i < inputOrders.Length; i++)
  50.                         {
  51.                             string boughtProduct = inputOrders[i];
  52.                             if (products.ContainsKey(boughtProduct))
  53.                             {
  54.                                 buyersData[buyer].Add(boughtProduct);
  55.                             }
  56.                         }
  57.                     }
  58.                 }
  59.  
  60.                 orders = Console.ReadLine();
  61.             }
  62.  
  63.  
  64.             var topCustomer = buyersData
  65.                 .OrderByDescending(d => d.Value.Sum(product => products[product]))
  66.                 .First();
  67.  
  68.             string name = topCustomer.Key;
  69.             List<string> prb = topCustomer.Value;
  70.  
  71.             Console.WriteLine("Biggest spender: {0}", name);
  72.             Console.WriteLine("^Products bought:");
  73.             double totalSpent = prb.Sum(pr => products[pr]);
  74.             var orderedProducts = prb.Distinct().OrderByDescending(p => products[p]);
  75.             foreach (var item in orderedProducts)
  76.             {
  77.                 Console.WriteLine("^^^{0}: {1:F2}", item, products[item]);
  78.             }
  79.             Console.WriteLine("Total: {0:F2}", totalSpent);
  80.         }
  81.     }
  82. }
  83. //da e jiv i zdrav Preslav
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement