Guest User

Untitled

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