Advertisement
AlexTasev

07_AndreyAndBilliard

Jun 16th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07_AndreyAndBilliard
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             List<Product> products = new List<Product>();
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string[] tokens = Console.ReadLine().Split("-");
  17.                 string productName = tokens[0];
  18.                 decimal price = decimal.Parse(tokens[1]);
  19.  
  20.                 Product product = products.FirstOrDefault(p => p.ProductName == productName);
  21.  
  22.                 if (product == null)
  23.                 {
  24.                     Product newProduct = new Product(productName, price);
  25.                     products.Add(newProduct);
  26.                 }
  27.                 else
  28.                 {
  29.                     product.Price = price;
  30.                 }
  31.             }
  32.  
  33.             List<Customer> customers = new List<Customer>();
  34.  
  35.             while (true)
  36.             {
  37.                 string line = Console.ReadLine();
  38.                 if (line == "end of clients")
  39.                 {
  40.                     break;
  41.                 }
  42.  
  43.                 string[] tokens = line.Split(new[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
  44.  
  45.                 string customerName = tokens[0];
  46.                 string productName = tokens[1];
  47.                 int quantity = int.Parse(tokens[2]);
  48.  
  49.                 if ((products.Any(p => p.ProductName == productName)) == false)
  50.                 {
  51.                     continue;
  52.                 }
  53.  
  54.                 Customer customer = customers.FirstOrDefault(c => c.Name == customerName);
  55.  
  56.                 if (customer == null)
  57.                 {
  58.                     Customer newCustomer = new Customer(customerName);
  59.                     newCustomer.OrderedProducts.Add(productName, quantity);
  60.                     customers.Add(newCustomer);
  61.                 }
  62.                 else
  63.                 {
  64.                     if (customer.OrderedProducts.ContainsKey(productName) == false)
  65.                     {
  66.                         customer.OrderedProducts.Add(productName, quantity);
  67.                     }
  68.  
  69.                     else
  70.                     {
  71.                         customer.OrderedProducts[productName] += quantity;
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             decimal totalBill = 0;
  77.  
  78.             foreach (var customer in customers.OrderBy(c => c.Name))
  79.             {
  80.                 decimal bill = 0;
  81.  
  82.                 Console.WriteLine(customer.Name);
  83.  
  84.                 foreach (var orderedProduct in customer.OrderedProducts)
  85.                 {
  86.                     Console.WriteLine($"-- {orderedProduct.Key} - {orderedProduct.Value}");
  87.  
  88.                     string productName = orderedProduct.Key;
  89.                     int quantity = orderedProduct.Value;
  90.  
  91.                     decimal price = products.First(p => p.ProductName == productName).Price;
  92.  
  93.                     bill += price * quantity;
  94.                 }
  95.  
  96.                 totalBill += bill;
  97.  
  98.                 Console.WriteLine($"Bill: {bill:f2}");
  99.             }
  100.  
  101.             Console.WriteLine($"Total bill: {totalBill:f2}");
  102.         }
  103.     }
  104.  
  105.     class Product
  106.     {
  107.         public Product(string productName, decimal price)
  108.         {
  109.             ProductName = productName;
  110.             Price = price;
  111.         }
  112.         public string ProductName { get; set; }
  113.         public decimal Price { get; set; }
  114.     }
  115.     class Customer
  116.     {
  117.         public Customer(string name)
  118.         {
  119.             Name = name;
  120.             OrderedProducts = new Dictionary<string, int>();
  121.         }
  122.         public string Name { get; set; }
  123.         public Dictionary<string, int> OrderedProducts { get; set; }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement