Advertisement
Guest User

Andrey Billiard

a guest
Oct 22nd, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.43 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.                     //var test1 = buyersOrders[indexBuyer].Order.Any(p => p.Key.Equals(orderedProduct));
  88.                     //var test2 = buyersOrders[indexBuyer].Order.Any(p => p.Key.Name.Equals(orderedProduct));
  89.                     //var test3 = buyersOrders[indexBuyer].Order.Any(p => p.Key.Name.Equals(orderedProduct.Name));
  90.                     //var test4 = buyersOrders[indexBuyer].Order.Any(p => p.Key.Equals(orderedProduct.Name));
  91.                     //var test5 = buyersOrders[indexBuyer].Order.Any(p => p.Key == orderedProduct);
  92.                     //var test6 = buyersOrders[indexBuyer].Order.Any(p => p.Key.Name == orderedProduct.Name);
  93.                     //var test7 = buyersOrders[indexBuyer].Order.ContainsKey(orderedProduct);
  94.  
  95.                     //Console.WriteLine(test1 + " " + test2 + " " + test3 + " " + test4 + " " + test5 + " " + test6 + " " + test7);
  96.  
  97.                     if (!buyersOrders[indexBuyer].Order.Any(p => p.Key.Name.Equals(orderedProduct.Name)))
  98.                     {
  99.                         buyersOrders[indexBuyer].Order.Add(orderedProduct, 0);
  100.                     }
  101.  
  102.                     Product indexOrder = buyersOrders[indexBuyer].Order.FirstOrDefault(p => p.Key.Name.Equals(orderedProduct.Name)).Key;
  103.  
  104.                     //var key = buyersOrders[indexBuyer].Order.Keys(p => p.Key.Name.Equals(orderedProduct.Name));
  105.  
  106.                     buyersOrders[indexBuyer].Order[indexOrder] += decimal.Parse(buyerProductQuantity[2]);
  107.                 }
  108.  
  109.                 input = Console.ReadLine();
  110.             }
  111.  
  112.             return buyersOrders;
  113.         }
  114.  
  115.         private static Product ReadProduct()
  116.         {
  117.             string[] input = Console.ReadLine()
  118.                         .Trim()
  119.                         .Split(new string[] { "-" }, StringSplitOptions
  120.                         .RemoveEmptyEntries)
  121.                         .ToArray();
  122.  
  123.             Product currentProduct = new Product();
  124.  
  125.             currentProduct.Name = input[0];
  126.             currentProduct.Price = decimal.Parse(input[1]);
  127.  
  128.             return currentProduct;
  129.         }
  130.     }
  131.  
  132.     internal class Buyer
  133.     {
  134.         public string Name { get; set; }
  135.         public Dictionary<Product, decimal> Order { get; set; }
  136.  
  137.         public decimal Bill()
  138.         {
  139.             decimal totalBill = 0;
  140.  
  141.             foreach (var item in Order)
  142.             {
  143.                 totalBill += item.Key.Price * item.Value;
  144.             }
  145.  
  146.             return totalBill;
  147.         }
  148.     }
  149.  
  150.     internal class Product
  151.     {
  152.         public string Name { get; set; }
  153.         public decimal Price { get; set; }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement