Advertisement
allsi89

Andrei and Billiard

Feb 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Andrey_and_billiard
  6. {
  7.     class Customer
  8.     {
  9.         public string Name { get; set; }
  10.         public Dictionary<string, double> ShopList { get; set; }
  11.         public double Bill { get; set; }
  12.  
  13.     }
  14.     class Program
  15.     {
  16.         static void Main()
  17.         {
  18.  
  19.             Dictionary<string, double> menu = new Dictionary<string, double>();
  20.             int numberOfItems = int.Parse(Console.ReadLine());
  21.             for (int i = 0; i < numberOfItems; i++)
  22.             {
  23.                 string[] input = Console.ReadLine().Split('-');
  24.                 if (!menu.ContainsKey(input[0]))
  25.                 {
  26.                     menu.Add(input[0], double.Parse(input[1]));
  27.                 }
  28.                 else
  29.                 {
  30.                     menu[input[0]] = double.Parse(input[1]);
  31.                 }
  32.  
  33.             }
  34.             List<Customer> allCustomers = new List<Customer>();
  35.             while (true)
  36.             {
  37.                 string info = Console.ReadLine();
  38.                 if (info == "end of clients")
  39.                 {
  40.                     break;
  41.                 }
  42.                 string[] clientInfo = info.Split('-', ',');
  43.                 string customerName = clientInfo[0];
  44.                 string product = clientInfo[1];
  45.                 double quantity = double.Parse(clientInfo[2]);
  46.  
  47.                 if (!menu.ContainsKey(product)) { continue; }
  48.  
  49.                 Customer client = new Customer();
  50.                 client.ShopList = new Dictionary<string, double>();
  51.                 client.Name = clientInfo[0];
  52.                 client.ShopList.Add(product, quantity);
  53.  
  54.                 if (allCustomers.Any(c => c.Name == customerName))
  55.                 {
  56.                     Customer existingCustomer = allCustomers.First(c => c.Name == customerName);
  57.                     if (existingCustomer.ShopList.ContainsKey(product))
  58.                     {
  59.                         existingCustomer.ShopList[product] += quantity;
  60.                     }
  61.                     else
  62.                     {
  63.                         existingCustomer.ShopList[product] = quantity;
  64.                     }
  65.                 }
  66.                 else
  67.                 {
  68.                     allCustomers.Add(client);
  69.                 }
  70.             }
  71.  
  72.             foreach (var customer in allCustomers)
  73.             {
  74.                 foreach (var item in customer.ShopList)
  75.                 {
  76.                  
  77.                     foreach (var product in menu)
  78.                     {
  79.                         if (item.Key == product.Key) { customer.Bill += item.Value * product.Value; }
  80.                     }
  81.                 }
  82.             }
  83.          
  84.             var ordered = allCustomers
  85.                             .OrderBy(x => x.Name)
  86.                             .ThenBy(x => x.Bill)
  87.                             .ToList();
  88.             foreach (var customer in ordered)
  89.             {
  90.                 Console.WriteLine(customer.Name);
  91.                 foreach (KeyValuePair<string, double> item in customer.ShopList)
  92.                 {
  93.                     Console.WriteLine($"-- {item.Key} - {item.Value}");
  94.                 }
  95.                 Console.WriteLine("Bill: {0:f2}", customer.Bill);
  96.              
  97.             }
  98.             Console.WriteLine("Total bill: {0:F2}", allCustomers.Sum(c => c.Bill));
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement