Advertisement
Guest User

Andrey Billiard

a guest
Oct 22nd, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. namespace AndreyAndBilliard
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             List<Product> products = new List<Product> { };
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 Product currentProduct = ReadProduct();
  18.  
  19.                 if (products.Any(p => p.Name.Equals(currentProduct.Name)))
  20.                 {
  21.                     int index = products.IndexOf(products.FirstOrDefault(p => p.Name.Equals(currentProduct.Name)));
  22.  
  23.                     products.RemoveAt(index);
  24.                 }
  25.  
  26.                 products.Add(currentProduct);
  27.             }
  28.  
  29.             List<Buyer> orders = ReadBuyersOrders(products);
  30.  
  31.             PrintBill(orders);
  32.         }
  33.  
  34.         private static void PrintBill(List<Buyer> orders)
  35.         {
  36.             foreach (var buyer in orders.OrderBy(n => n.Name))
  37.             {
  38.                 Console.WriteLine($"{buyer.Name}");
  39.  
  40.                 foreach (var product in buyer.Order)
  41.                 {
  42.                     Console.WriteLine($"-- {product.Key.Name} - {product.Value}");
  43.                 }
  44.  
  45.                 Console.WriteLine($"Bill: {buyer.Bill():f2}");
  46.             }
  47.  
  48.             Console.WriteLine($"Total bill: {orders.Sum(t => t.Bill()):f2}");
  49.         }
  50.  
  51.         private static List<Buyer> ReadBuyersOrders(List<Product> products)
  52.         {
  53.             string input = Console.ReadLine();
  54.  
  55.             List<Buyer> buyersOrders = new List<Buyer> { };
  56.  
  57.             while (input != "end of clients")
  58.             {
  59.                 string[] buyerProductQuantity = input
  60.                         .Trim()
  61.                         .Split(new string[] { "-", "," }, StringSplitOptions
  62.                         .RemoveEmptyEntries)
  63.                         .ToArray();
  64.  
  65.                 Product orderedProduct = new Product();
  66.  
  67.                 orderedProduct.Name = buyerProductQuantity[1];
  68.  
  69.                 if (products.Any(n => n.Name.Equals(orderedProduct.Name)))
  70.                 {
  71.                     int indexPrice = products.IndexOf(products.FirstOrDefault(n => n.Name.Equals(orderedProduct.Name)));
  72.  
  73.                     orderedProduct.Price = products[indexPrice].Price;
  74.  
  75.                     Buyer currentBuyer = new Buyer();
  76.  
  77.                     currentBuyer.Name = buyerProductQuantity[0];
  78.                     currentBuyer.Order = new Dictionary<Product, decimal> { };
  79.  
  80.                     if (!buyersOrders.Any(n => n.Name.Equals(currentBuyer.Name)))
  81.                     {
  82.                         buyersOrders.Add(currentBuyer);
  83.                     }
  84.  
  85.                     int indexBuyer = buyersOrders.IndexOf(buyersOrders.FirstOrDefault(n => n.Name.Equals(currentBuyer.Name)));
  86.  
  87.                     //if (!buyersOrders[indexBuyer].Order.Any(p => p.Key.Equals(orderedProduct)))
  88.  
  89. //Here's the problem....
  90.  
  91.                     if (!buyersOrders[indexBuyer].Order.ContainsKey(orderedProduct))
  92.                     {
  93.                         buyersOrders[indexBuyer].Order.Add(orderedProduct, 0);
  94.                     }
  95.  
  96.                     buyersOrders[indexBuyer].Order[orderedProduct] += decimal.Parse(buyerProductQuantity[2]);
  97.                 }
  98.  
  99.                 input = Console.ReadLine();
  100.             }
  101.  
  102.             return buyersOrders;
  103.         }
  104.  
  105.         private static Product ReadProduct()
  106.         {
  107.             string[] input = Console.ReadLine()
  108.                         .Trim()
  109.                         .Split(new string[] { "-" }, StringSplitOptions
  110.                         .RemoveEmptyEntries)
  111.                         .ToArray();
  112.  
  113.             Product currentProduct = new Product();
  114.  
  115.             currentProduct.Name = input[0];
  116.             currentProduct.Price = decimal.Parse(input[1]);
  117.  
  118.             return currentProduct;
  119.         }
  120.     }
  121.  
  122.     internal class Buyer
  123.     {
  124.         public string Name { get; set; }
  125.         public Dictionary<Product, decimal> Order { get; set; }
  126.  
  127.         public decimal Bill()
  128.         {
  129.             decimal totalBill = 0;
  130.  
  131.             foreach (var item in Order)
  132.             {
  133.                 totalBill += item.Key.Price * item.Value;
  134.             }
  135.  
  136.             return totalBill;
  137.         }
  138.     }
  139.  
  140.     internal class Product
  141.     {
  142.         public string Name { get; set; }
  143.         public decimal Price { get; set; }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement