Advertisement
zhivko1985

using System; using System.Collections.Generic; using System

Apr 23rd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _7.Andrey_and_billiard
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             var productPrice = new Dictionary<string, decimal>();
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 var currentText = Console.ReadLine().Split('-').ToArray();
  18.                 productPrice[currentText[0]] = decimal.Parse(currentText[1]);
  19.             }
  20.  
  21.             string input = Console.ReadLine();
  22.             var listCustemers = new List<Customer>();
  23.             while (!input.Equals("end of clients"))
  24.             {
  25.                 var splitedInput = input.Split('-', ',').ToArray();
  26.                 string custemerName = splitedInput[0];
  27.                 string product = splitedInput[1];
  28.                 int quantity = int.Parse(splitedInput[2]);
  29.  
  30.                 if (productPrice.ContainsKey(product))
  31.                 {
  32.                     if (listCustemers.Any(x => x.name == custemerName))
  33.                     {
  34.                         Customer customer = listCustemers.First(x => x.name == custemerName);
  35.                         if (listCustemers.Any(x=>x.productQnty.ContainsKey(product)))
  36.                         {
  37.                             customer.productQnty[product] += quantity;
  38.                             customer.personalBill += quantity * productPrice[product];
  39.                         }
  40.                         else
  41.                         {
  42.                             customer.productQnty.Add(product,quantity);
  43.                             customer.personalBill += quantity * productPrice[product];
  44.                         }
  45.  
  46.                     }
  47.                     else
  48.                     {
  49.                         Customer currentCustomer = new Customer();
  50.                         currentCustomer.name = custemerName;
  51.                         currentCustomer.productQnty = new Dictionary<string, int>();
  52.                         currentCustomer.productQnty.Add(product, quantity);
  53.                         currentCustomer.personalBill= quantity * productPrice[product];
  54.                         listCustemers.Add(currentCustomer);
  55.                     }                
  56.                 }
  57.                 input = Console.ReadLine();
  58.             }
  59.             foreach (var item in listCustemers.OrderBy(x=>x.name))
  60.             {
  61.                 Console.WriteLine(item.name);
  62.                 foreach (var customerProductQuantity in item.productQnty)
  63.                 {
  64.                     Console.WriteLine("-- {0} - {1}",customerProductQuantity.Key,customerProductQuantity.Value);
  65.                 }
  66.                 Console.WriteLine("Bill: {0:f2}",item.personalBill);
  67.             }
  68.             decimal counter = 0;
  69.             foreach (var item in listCustemers)
  70.             {
  71.                 counter += item.personalBill;
  72.             }
  73.             Console.WriteLine("Total bill: {0}",counter);
  74.         }
  75.     }
  76.     public class Customer
  77.     {
  78.         public string name { get; set; }
  79.         public Dictionary<string, int> productQnty { get; set; }
  80.         public decimal personalBill { get; set; }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement