Advertisement
Aliendreamer

7. Andrey and Billiard

Aug 10th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class Client
  7. {
  8.     public Client()
  9.     {
  10.         this.Order=new Dictionary<Product,int>();
  11.     }
  12.    
  13.    
  14.     public string Name{get;set;}
  15.    
  16.     public Dictionary<Product,int>Order{get;set;}
  17.    
  18.     public double Bill=>Order.Sum(x=>x.Key.Price*x.Value);
  19.    
  20.     public override string ToString()
  21.     {
  22.         StringBuilder sb=new StringBuilder();
  23.         sb.AppendLine(this.Name);
  24.         foreach(var p in this.Order)
  25.         {
  26.             string current=$"-- {p.Key.Type} - {p.Value}";
  27.             sb.AppendLine(current);
  28.         }
  29.        
  30.         string bill=$"Bill: {this.Bill:f2}";
  31.         sb.AppendLine(bill);
  32.         return sb.ToString().TrimEnd();
  33.     }
  34. }
  35.  
  36. public class Product
  37. {
  38.     public string Type{get;set;}
  39.    
  40.     public double Price{get;set;}
  41.    
  42. }
  43.  
  44.  
  45. public class Program
  46. {
  47.    
  48.    
  49.     public static void Main()
  50.     {
  51.         List<Product>offerList= new List<Product>();
  52.         List<Client>clients=new List<Client>();
  53.        
  54.         int n=int.Parse(Console.ReadLine());
  55.        
  56.         for(int i=0;i<n;i++)
  57.         {
  58.        
  59.             string[]inputTokens=Console.ReadLine().Split(new []{'-'},StringSplitOptions.RemoveEmptyEntries).ToArray();
  60.            
  61.             string name=inputTokens[0];
  62.             double price=double.Parse(inputTokens[1]);
  63.            
  64.             if(offerList.Any(x=>x.Type==name))
  65.             {
  66.                 var existingProduct=offerList.First(x=>x.Type==name);
  67.                 existingProduct.Price=price;
  68.                 continue;
  69.             }
  70.            
  71.             var product=new Product
  72.             {
  73.              Type=name,
  74.              Price=price              
  75.             };
  76.            
  77.             offerList.Add(product);
  78.         }
  79.        
  80.         string input;
  81.        
  82.         while((input=Console.ReadLine())!="end of clients")
  83.         {
  84.             string[] tokens=input.Split(new []{'-',','},StringSplitOptions.RemoveEmptyEntries).ToArray();
  85.        
  86.             string name=tokens[0];
  87.             string productName=tokens[1];
  88.             int quantity=int.Parse(tokens[2]);
  89.            
  90.            
  91.             if(!offerList.Any(x=>x.Type==productName))
  92.                {
  93.                 continue;
  94.                }
  95.            
  96.             var client=clients.FirstOrDefault(x=>x.Name==name);
  97.             var product=offerList.FirstOrDefault(p=>p.Type==productName);
  98.            
  99.             if(client==null)
  100.             {
  101.            
  102.                 client=new Client
  103.                 {
  104.                     Name=name
  105.                 };
  106.                 client.Order.Add(product,quantity);
  107.                
  108.                 clients.Add(client);
  109.                 continue;
  110.             }
  111.            
  112.             bool alreadyOrderedOnce=client.Order.Any(x=>x.Key.Type==productName);
  113.             if(alreadyOrderedOnce)
  114.             {
  115.            
  116.              client.Order[product]+=quantity;
  117.  
  118.             }
  119.             else
  120.             {
  121.               client.Order.Add(product,quantity);
  122.  
  123.             }
  124.            
  125.         }
  126.         var ordered = clients.OrderBy(x => x.Name).ToList();
  127.         foreach(var c in ordered)
  128.         {
  129.            
  130.             Console.WriteLine(c.ToString());
  131.         }      
  132.         double totalBill=clients.Sum(x=>x.Bill);
  133.         Console.WriteLine($"Total bill: {totalBill:f2}");
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement