Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _4.Most_Valued_Customer
- {
- class Program
- {
- static void Main(string[] args)
- {
- var products = new Dictionary<string, decimal>();
- var customers = new Dictionary<string, Dictionary<string, decimal>>();
- string input = Console.ReadLine();
- var a = new List<string>();
- decimal sum = 0;
- while (input != "Shop is open")
- {
- string[] data = input.Split(' ');
- products[data[0]] = decimal.Parse(data[1]);
- input = Console.ReadLine();
- }
- input = Console.ReadLine();
- a.Add(input);
- while (input != "Print")
- {
- var sorteredProducts = products.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
- int count = 0;
- if (input == "Discount")
- {
- foreach (var product in sorteredProducts)
- {
- decimal price = product.Value;
- price *= 0.9m;
- products[product.Key] = price;
- count++;
- if (count == 3) break;
- }
- }
- input = Console.ReadLine();
- a.Add(input);
- }
- input = Console.ReadLine();
- for (int i = 0; i < a.Count; i++)
- {
- string[] data = a[i].Split(new string[] { ": ", ", " }, StringSplitOptions.RemoveEmptyEntries);
- if (data[0] == "Print")
- {
- break;
- }
- if (!(data[0] == "Discount"))
- {
- if (!customers.ContainsKey(data[0]))
- {
- customers.Add(data[0], new Dictionary<string, decimal>());
- for (int j = 1; j < data.Length; j++)
- {
- if (!customers[data[0]].ContainsKey(data[j]))
- {
- customers[data[0]][data[j]] = 0m;
- }
- }
- foreach (var product in products)
- {
- if (data.Contains(product.Key))
- {
- int count = 0;
- foreach (var item in data)
- {
- if (item == product.Key) count++;
- }
- customers[data[0]][product.Key] = product.Value * count;
- }
- }
- }
- }
- }
- string customerName = string.Empty;
- decimal max = decimal.MinValue;
- foreach (var record in customers)
- {
- string name = record.Key;
- sum = 0;
- foreach (var item in record.Value)
- {
- sum += item.Value;
- }
- if (max < sum)
- {
- max = sum;
- customerName = name;
- }
- }
- Console.WriteLine($"Biggest spender: {customerName}");
- Console.WriteLine("^Products bought:");
- var tempDictionary = new Dictionary<string, decimal>();
- var result = new Dictionary<string, decimal>();
- foreach (var item in customers[customerName])
- {
- tempDictionary[item.Key] = products[item.Key];
- }
- result = tempDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
- foreach (var item in result)
- {
- Console.WriteLine($"^^^{item.Key}: {products[item.Key]:F2}");
- }
- Console.WriteLine($"Total: {max:F2}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement