Advertisement
ati90

7. Andrey and Billiard

Jun 25th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 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 _7.Andrey_and_Billiard
  8. {
  9.     class Customer
  10.     {
  11.         public string Name { get; set; }
  12.  
  13.         public Dictionary<string, int> ShopingList { get; set; }
  14.  
  15.         public decimal Bill { get; set; }
  16.     }
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             int n = int.Parse(Console.ReadLine());
  22.             Dictionary<string, decimal> products = new Dictionary<string, decimal>();
  23.             List<Customer> customerList = new List<Customer>();
  24.  
  25.             for (int i = 0; i < n; i++)
  26.             {
  27.  
  28.                 var tokens = Console.ReadLine().Split('-').ToArray();
  29.  
  30.                 var item = tokens[0];
  31.  
  32.                 var price = decimal.Parse(tokens[1]);
  33.  
  34.                 if (!products.ContainsKey(item))
  35.                 {
  36.                     products.Add(item, price);
  37.                 }
  38.                 else
  39.                 {
  40.                     products[item] = price;
  41.                 }
  42.             }
  43.  
  44.             var orders = string.Empty;
  45.             while (true)
  46.             {
  47.                 orders = Console.ReadLine();
  48.  
  49.                 if (orders == "end of clients")
  50.                     break;
  51.  
  52.                 var tokens = orders.Split('-', ',').ToArray();
  53.                 string name = tokens[0];
  54.                 string order = tokens[1];
  55.                 int orderQuantity = int.Parse(tokens[2]);
  56.  
  57.  
  58.                 if (!products.ContainsKey(order))
  59.                 {
  60.                     continue;
  61.                 }
  62.  
  63.                 Customer customer = new Customer()
  64.                 {
  65.                     Name = name,
  66.                     ShopingList = new Dictionary<string, int>
  67.                     {
  68.                         [order] = orderQuantity
  69.                     },
  70.                     Bill = orderQuantity * products[order]
  71.  
  72.                 };
  73.  
  74.                 if (!customerList.Exists(c => c.Name == customer.Name))
  75.                 {
  76.                     customerList.Add(customer);
  77.                 }
  78.                 else
  79.                 {
  80.                     Customer existingCustomer = customerList.First(c => c.Name == name);
  81.                     if (!(existingCustomer.ShopingList.ContainsKey(order)))
  82.                     {
  83.                         existingCustomer.ShopingList.Add(order, orderQuantity);
  84.                         existingCustomer.Bill += customer.Bill;
  85.                     }
  86.                     else
  87.                     {
  88.                         existingCustomer.ShopingList[order] += orderQuantity;
  89.                         existingCustomer.Bill += customer.Bill;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             decimal totalPrice = 0;
  95.             foreach (var customer in customerList.OrderBy(a => a.Name).ThenBy(b=>b.Bill))
  96.             {
  97.                 Console.WriteLine(customer.Name);
  98.                 foreach (KeyValuePair<string, int> item in customer.ShopingList)
  99.                 {
  100.                     Console.WriteLine($"-- {item.Key} - {item.Value}");
  101.                 }
  102.  
  103.                 Console.WriteLine("Bill: {0:F2}", customer.Bill);
  104.                 totalPrice += customer.Bill;
  105.             }
  106.             Console.WriteLine($"Total bill: {customerList.Sum(a => a.Bill):F2}");
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement