Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.         Dictionary<string, decimal> productsPrices = ReadProductsPrices(n);
  11.         List<Client> clients = ReadClientsBills(productsPrices);
  12.         clients = clients.OrderBy(c => c.Name).ToList();
  13.         PrintClientsBills(clients, productsPrices);
  14.     }
  15.  
  16.     static Dictionary<string, decimal> ReadProductsPrices(int count)
  17.     {
  18.         Dictionary<string, decimal> productsPrices = new Dictionary<string, decimal>();
  19.         for (int a = 0; a < count; a++)
  20.         {
  21.             string[] input = Console.ReadLine().Split('-');
  22.             string product = input[0];
  23.             decimal price = decimal.Parse(input[1]);
  24.             productsPrices[product] = price;
  25.         }
  26.         return productsPrices;
  27.     }
  28.  
  29.  
  30.     static List<Client> ReadClientsBills(Dictionary<string, decimal> productsPrices)
  31.     {
  32.         List<Client> clients = new List<Client>();
  33.         while (true)
  34.         {
  35.             string input = Console.ReadLine();
  36.             if (input == "end of clients")
  37.             {
  38.                 break;
  39.             }
  40.             string[] splitInput = input.Split('-', ',');
  41.             string name = splitInput[0];
  42.             string product = splitInput[1];
  43.             int quantity = int.Parse(splitInput[2]);
  44.             if (!productsPrices.ContainsKey(product))
  45.             {
  46.                 continue;
  47.             }
  48.             Client client = new Client()
  49.             {
  50.                 Name = name,
  51.                 ProductsQuantitys = new Dictionary<string, int>
  52.                 {
  53.                     [product] = quantity
  54.                 }
  55.             };
  56.             if (clients.Any(c => c.Name == name))
  57.             {
  58.                 Client exitingClient = clients.First(c => c.Name == name);
  59.                 if (exitingClient.ProductsQuantitys.ContainsKey(product))
  60.                 {
  61.                     exitingClient.ProductsQuantitys[product] += quantity;
  62.                 }
  63.                 else
  64.                 {
  65.                     exitingClient.ProductsQuantitys[product] = quantity;
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 clients.Add(client);
  71.             }
  72.         }
  73.         return clients;
  74.     }
  75.  
  76.     static void PrintClientsBills(List<Client> clients, Dictionary<string, decimal> productsPrices)
  77.     {
  78.         decimal totalBills = 0;
  79.         foreach (Client client in clients)
  80.         {
  81.             Console.WriteLine(client.Name);
  82.             foreach (KeyValuePair<string, int> productQuantity in client.ProductsQuantitys)
  83.             {
  84.                 Console.WriteLine($"-- {productQuantity.Key} - {productQuantity.Value}");
  85.                 Console.WriteLine($"Bill: {productQuantity.Value * productsPrices[productQuantity.Key]:f2}");
  86.                 totalBills += productQuantity.Value * productsPrices[productQuantity.Key];
  87.             }
  88.         }
  89.         Console.WriteLine($"Total bill: {totalBills:f2}");
  90.     }
  91. }
  92.  
  93. class Client
  94. {
  95.     public string Name { get; set; }
  96.     public Dictionary<string, int> ProductsQuantitys { get; set; }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement