Advertisement
Guest User

Andrey An Billiard

a guest
Oct 13th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07.AndreyAndBilliard
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             Dictionary<string, decimal> menuItems = AddMenuItems();
  12.  
  13.            
  14.             List<Client> clients = ReadClients(menuItems);
  15.            
  16.  
  17.             PrintResults(clients, menuItems);
  18.  
  19.         }
  20.  
  21.         private static List<Client> ReadClients(Dictionary<string, decimal> menuItems)
  22.         {
  23.             string command = Console.ReadLine();
  24.             List < Client > clients = new List<Client>();
  25.             while (!command.Equals("end of clients"))
  26.             {
  27.                 string[] ordersData = command.Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
  28.                 string item = ordersData[1];
  29.                 string clientName = ordersData[0];
  30.                 int quantity = int.Parse(ordersData[2]);
  31.                 Dictionary<string, int> clientOrders = new Dictionary<string, int>();
  32.  
  33.                 if (!menuItems.ContainsKey(item))
  34.                 {
  35.                     command = Console.ReadLine();
  36.                     continue;
  37.                 }
  38.                 Client client = new Client();
  39.                 if (clients.Any(cl => cl.Name == clientName))
  40.                 {
  41.                     client = clients.First(cl => cl.Name == clientName);
  42.                     if (!client.Order.ContainsKey(item))
  43.                     {
  44.                         client.Order.Add(item, 0);
  45.                     }
  46.  
  47.                     client.Order[item] += quantity;
  48.                 }
  49.                 else
  50.                 {
  51.                     client.Name = clientName;
  52.                     client.Order = new Dictionary<string, int>();
  53.                     client.Order.Add(item, 0);
  54.                     client.Order[item] += quantity;
  55.                     clients.Add(client);
  56.                 }
  57.  
  58.                 command = Console.ReadLine();
  59.             }
  60.  
  61.             return clients;
  62.         }
  63.  
  64.         static void PrintResults(List<Client> clients, Dictionary<string, decimal> menuItems)
  65.         {
  66.             List<decimal> bills = new List<decimal>();
  67.  
  68.             foreach (var client in clients.OrderBy(x => x.Name))
  69.             {
  70.                 Console.WriteLine("{0}", client.Name);
  71.  
  72.                 foreach (var order in client.Order)
  73.                 {
  74.                     client.Bill = order.Value * menuItems[order.Key];
  75.                     Console.WriteLine("-- {0} - {1}", order.Key, order.Value);
  76.                 }
  77.  
  78.                 bills.Add(client.Bill);
  79.  
  80.                 Console.WriteLine("Bill: {0:f2}", client.Bill);
  81.             }
  82.  
  83.             Console.WriteLine("Total bill: {0:f2}", bills.Sum());
  84.         }
  85.  
  86.         static Dictionary<string, decimal> AddMenuItems()
  87.         {
  88.             Dictionary<string, decimal> items = new Dictionary<string, decimal>();
  89.             int number = int.Parse(Console.ReadLine());
  90.  
  91.             for (int i = 0; i < number; i++)
  92.             {
  93.                 string[] itemProp = Console.ReadLine().Split('-').ToArray();
  94.                 string item = itemProp[0];
  95.                 decimal singlePrice = decimal.Parse(itemProp[1]);
  96.  
  97.                 if (!items.ContainsKey(item))
  98.                 {
  99.                     items.Add(item, 0);
  100.                 }
  101.  
  102.                 items[item] = singlePrice;
  103.             }
  104.  
  105.             return items;
  106.         }
  107.     }
  108.  
  109.     class Client
  110.     {
  111.         public string Name { get; set; }
  112.         public Dictionary<string, int> Order { get; set; }
  113.         public decimal Bill { get; set; }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement