Advertisement
Guest User

Most Valued Customer

a guest
Jul 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 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 _4.Most_Valued_Customer
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var products = new Dictionary<string, decimal>();
  14.             var customers = new Dictionary<string, Dictionary<string, decimal>>();
  15.             string input = Console.ReadLine();
  16.             var a = new List<string>();
  17.             decimal sum = 0;
  18.             while (input != "Shop is open")
  19.             {
  20.                 string[] data = input.Split(' ');
  21.                 products[data[0]] = decimal.Parse(data[1]);
  22.                 input = Console.ReadLine();
  23.             }
  24.  
  25.             input = Console.ReadLine();
  26.             a.Add(input);
  27.             while (input != "Print")
  28.             {
  29.                 var sorteredProducts = products.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  30.                 int count = 0;
  31.                 if (input == "Discount")
  32.                 {
  33.                     foreach (var product in sorteredProducts)
  34.                     {
  35.                         decimal price = product.Value;
  36.                         price *= 0.9m;
  37.                         products[product.Key] = price;
  38.                         count++;
  39.                         if (count == 3) break;
  40.                     }
  41.                 }
  42.                 input = Console.ReadLine();
  43.                 a.Add(input);
  44.             }
  45.  
  46.             input = Console.ReadLine();
  47.             for (int i = 0; i < a.Count; i++)
  48.             {
  49.                 string[] data = a[i].Split(new string[] { ": ", ", " }, StringSplitOptions.RemoveEmptyEntries);
  50.                 if (data[0] == "Print")
  51.                 {
  52.                     break;
  53.                 }
  54.                 if (!(data[0] == "Discount"))
  55.                 {
  56.                     if (!customers.ContainsKey(data[0]))
  57.                     {
  58.                         customers.Add(data[0], new Dictionary<string, decimal>());
  59.  
  60.                         for (int j = 1; j < data.Length; j++)
  61.                         {
  62.                             if (!customers[data[0]].ContainsKey(data[j]))
  63.                             {
  64.                                 customers[data[0]][data[j]] = 0m;
  65.                             }
  66.                         }
  67.  
  68.                         foreach (var product in products)
  69.                         {
  70.                             if (data.Contains(product.Key))
  71.                             {
  72.                                 int count = 0;
  73.                                 foreach (var item in data)
  74.                                 {
  75.                                     if (item == product.Key) count++;
  76.                                 }
  77.                                 customers[data[0]][product.Key] = product.Value * count;
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.  
  84.             string customerName = string.Empty;
  85.             decimal max = decimal.MinValue;
  86.             foreach (var record in customers)
  87.             {
  88.                 string name = record.Key;
  89.                 sum = 0;
  90.                 foreach (var item in record.Value)
  91.                 {
  92.                     sum += item.Value;
  93.                 }
  94.                 if (max < sum)
  95.                 {
  96.                     max = sum;
  97.                     customerName = name;
  98.                 }
  99.             }
  100.  
  101.             Console.WriteLine($"Biggest spender: {customerName}");
  102.             Console.WriteLine("^Products bought:");
  103.             var tempDictionary = new Dictionary<string, decimal>();
  104.             var result = new Dictionary<string, decimal>();
  105.             foreach (var item in customers[customerName])
  106.             {
  107.                 tempDictionary[item.Key] = products[item.Key];
  108.             }
  109.             result = tempDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  110.             foreach (var item in result)
  111.             {
  112.                 Console.WriteLine($"^^^{item.Key}: {products[item.Key]:F2}");
  113.             }
  114.             Console.WriteLine($"Total: {max:F2}");
  115.  
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement