MiroslavKisov

AnrdeyAndBilliard

Nov 23rd, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07.AndreyAndBilliard
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             Dictionary<string, decimal> shop = new Dictionary<string, decimal>();
  12.             List<Customer> customers = new List<Customer>();
  13.             int numberOfProducts = int.Parse(Console.ReadLine());
  14.  
  15.             for (int i = 0; i < numberOfProducts; i++)
  16.             {
  17.                 string input = Console.ReadLine();
  18.                 var productAndPrice = input
  19.                     .Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
  20.                     .ToArray();
  21.                 string product = productAndPrice[0];
  22.                 decimal price = decimal.Parse(productAndPrice[1]);
  23.  
  24.                 if (!shop.ContainsKey(product))
  25.                 {
  26.                     shop.Add(product, price);
  27.                 }
  28.                 else
  29.                 {
  30.                     shop[product] = price;
  31.                 }
  32.             }
  33.             while (true)
  34.             {
  35.                 string orders = Console.ReadLine();
  36.  
  37.                 if (orders == "end of clients")
  38.                 {
  39.                     break;
  40.                 }
  41.  
  42.                 var clientOrders = orders
  43.                     .Split(new char[] { ',', '-' }, StringSplitOptions.RemoveEmptyEntries)
  44.                     .ToArray();
  45.                 string clientName = clientOrders[0];
  46.                 string clientsProduct = clientOrders[clientOrders.Length - 2];
  47.                 int productQuantity = int.Parse(clientOrders[clientOrders.Length - 1]);
  48.                 if (shop.ContainsKey(clientsProduct))
  49.                 {
  50.                     Customer customer = new Customer();
  51.                     customer.Name = clientName;
  52.                     customer.ShopList.Add(clientsProduct, productQuantity);
  53.                     customer.Bill = shop[clientsProduct] * productQuantity;
  54.                     if (customers.Any(x => x.Name == clientName))
  55.                     {
  56.                         var currentCustomer = customers.First(x => x.Name == clientName);
  57.  
  58.                         if (currentCustomer.ShopList.ContainsKey(clientsProduct))
  59.                         {
  60.                             currentCustomer.ShopList[clientsProduct] += productQuantity;
  61.                             currentCustomer.Bill += shop[clientsProduct] * productQuantity;
  62.                         }
  63.                         else
  64.                         {
  65.                             currentCustomer.ShopList.Add(clientsProduct, productQuantity);
  66.                             currentCustomer.Bill += shop[clientsProduct] * productQuantity;
  67.                         }
  68.                     }
  69.                     else
  70.                     {
  71.                         customers.Add(customer);
  72.                     }
  73.                 }
  74.             }
  75.             PrintResult(customers);
  76.         }
  77.  
  78.         private static void PrintResult(List<Customer> customers)
  79.         {
  80.             decimal totalBill = 0.0m;
  81.                 foreach (Customer customer in customers.OrderBy(x => x.Name))
  82.                 {
  83.                     Console.WriteLine(customer.Name);
  84.                     foreach (KeyValuePair<string, int> item in customer.ShopList)
  85.                     {
  86.                         Console.WriteLine($"-- {item.Key} - {item.Value}");
  87.                     }
  88.                     Console.WriteLine($"Bill: {customer.Bill:F2}");
  89.                     totalBill += customer.Bill;
  90.                 }
  91.                 Console.WriteLine($"Total bill: {totalBill:F2}");
  92.         }
  93.  
  94.         class Customer
  95.         {
  96.             public string Name { get; set; }
  97.             public Dictionary<string, int> ShopList { get; set; }
  98.             public Customer()
  99.             {
  100.                 ShopList = new Dictionary<string, int>();
  101.             }
  102.             public decimal Bill { get; set; }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment