Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class Client
- {
- public Client()
- {
- this.Order=new Dictionary<Product,int>();
- }
- public string Name{get;set;}
- public Dictionary<Product,int>Order{get;set;}
- public double Bill=>Order.Sum(x=>x.Key.Price*x.Value);
- public override string ToString()
- {
- StringBuilder sb=new StringBuilder();
- sb.AppendLine(this.Name);
- foreach(var p in this.Order)
- {
- string current=$"-- {p.Key.Type} - {p.Value}";
- sb.AppendLine(current);
- }
- string bill=$"Bill: {this.Bill:f2}";
- sb.AppendLine(bill);
- return sb.ToString().TrimEnd();
- }
- }
- public class Product
- {
- public string Type{get;set;}
- public double Price{get;set;}
- }
- public class Program
- {
- public static void Main()
- {
- List<Product>offerList= new List<Product>();
- List<Client>clients=new List<Client>();
- int n=int.Parse(Console.ReadLine());
- for(int i=0;i<n;i++)
- {
- string[]inputTokens=Console.ReadLine().Split(new []{'-'},StringSplitOptions.RemoveEmptyEntries).ToArray();
- string name=inputTokens[0];
- double price=double.Parse(inputTokens[1]);
- if(offerList.Any(x=>x.Type==name))
- {
- var existingProduct=offerList.First(x=>x.Type==name);
- existingProduct.Price=price;
- continue;
- }
- var product=new Product
- {
- Type=name,
- Price=price
- };
- offerList.Add(product);
- }
- string input;
- while((input=Console.ReadLine())!="end of clients")
- {
- string[] tokens=input.Split(new []{'-',','},StringSplitOptions.RemoveEmptyEntries).ToArray();
- string name=tokens[0];
- string productName=tokens[1];
- int quantity=int.Parse(tokens[2]);
- if(!offerList.Any(x=>x.Type==productName))
- {
- continue;
- }
- var client=clients.FirstOrDefault(x=>x.Name==name);
- var product=offerList.FirstOrDefault(p=>p.Type==productName);
- if(client==null)
- {
- client=new Client
- {
- Name=name
- };
- client.Order.Add(product,quantity);
- clients.Add(client);
- continue;
- }
- bool alreadyOrderedOnce=client.Order.Any(x=>x.Key.Type==productName);
- if(alreadyOrderedOnce)
- {
- client.Order[product]+=quantity;
- }
- else
- {
- client.Order.Add(product,quantity);
- }
- }
- var ordered = clients.OrderBy(x => x.Name).ToList();
- foreach(var c in ordered)
- {
- Console.WriteLine(c.ToString());
- }
- double totalBill=clients.Sum(x=>x.Bill);
- Console.WriteLine($"Total bill: {totalBill:f2}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement