Advertisement
Vladimir76

Untitled

Feb 9th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 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 Andrey_and_billiard
  8. {
  9.     class Customer
  10.     {
  11.         public string Name { get; set; }
  12.         public Dictionary<string, int> order { get; set; }
  13.         public decimal Bill { get; set; }
  14.     }
  15.  
  16.     class Program
  17.     {
  18.         static void InsertProductAndPrice(Dictionary<string, decimal> menu, string product, decimal price)
  19.         {
  20.             if (!menu.ContainsKey(product))
  21.             {
  22.                 menu.Add(product, price);
  23.             }
  24.             else
  25.             {
  26.                 menu[product] = price;
  27.             }
  28.         }
  29.  
  30.         static void Main()
  31.         {
  32.             Dictionary<string, decimal> menu = new Dictionary<string, decimal>();
  33.             List<Customer> customers = new List<Customer>();
  34.  
  35.             int number = int.Parse(Console.ReadLine());
  36.             for (int i = 0; i < number; i++)
  37.             {
  38.                 string input = Console.ReadLine();
  39.                 string[] inputArr = input.Trim().Split('-');
  40.                 string product = inputArr[0];
  41.                 decimal price = decimal.Parse(inputArr[1]);
  42.                 InsertProductAndPrice(menu, product, price);
  43.  
  44.             }
  45.             string inputOrder = null;
  46.             do
  47.             {
  48.                 inputOrder = Console.ReadLine();
  49.                 if (inputOrder=="end of clients")
  50.                 {
  51.                     break;
  52.                 }
  53.                 string[] inputOrderArr = inputOrder
  54.                     .Trim()
  55.                     .Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
  56.                 string namePerson = inputOrderArr[0];
  57.                 string productOrder = inputOrderArr[1];
  58.                 int quantity = int.Parse(inputOrderArr[2]);
  59.                 if (!menu.ContainsKey(productOrder))
  60.                 {
  61.                     continue;
  62.                 }
  63.                 if (!customers.Any(name => name.Name == namePerson))
  64.                 {
  65.                     Customer cust = new Customer();
  66.                     cust.order = new Dictionary<string, int>();
  67.                     cust.Name = namePerson;
  68.                     cust.order.Add(productOrder, quantity);
  69.                     cust.Bill += quantity * menu[productOrder];
  70.                     customers.Add(cust);
  71.                 }
  72.                 else
  73.                 {
  74.                     int index = customers.FindIndex(x => x.Name == namePerson);
  75.  
  76.                     if (!customers[index].order.ContainsKey(productOrder))
  77.                     {
  78.                         customers[index].order.Add(productOrder, 0);
  79.                     }
  80.                     customers[index].order[productOrder] += quantity;
  81.                     customers[index].Bill += quantity * menu[productOrder];
  82.                 }
  83.             } while (inputOrder != "end of clients");
  84.             var sortCustomers = customers.OrderBy(x => x.Name).ToList();
  85.             foreach (var custS in sortCustomers)
  86.             {
  87.                 Console.WriteLine(custS.Name);
  88.                 foreach (KeyValuePair<string,int> kvp in custS.order)
  89.                 {
  90.                     Console.WriteLine("-- {0} - {1}",kvp.Key,kvp.Value);
  91.                 }
  92.                 Console.WriteLine("Bill: {0:F2}",custS.Bill);
  93.             }
  94.             Console.WriteLine("Total bill: {0}",sortCustomers.Sum(x => x.Bill));
  95.         }
  96.  
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement