tanya_zheleva

7

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