Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _07.AndreyAndBilliard
- {
- public class StartUp
- {
- public static void Main()
- {
- Dictionary<string, decimal> shop = new Dictionary<string, decimal>();
- List<Customer> customers = new List<Customer>();
- int numberOfProducts = int.Parse(Console.ReadLine());
- for (int i = 0; i < numberOfProducts; i++)
- {
- string input = Console.ReadLine();
- var productAndPrice = input
- .Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- string product = productAndPrice[0];
- decimal price = decimal.Parse(productAndPrice[1]);
- if (!shop.ContainsKey(product))
- {
- shop.Add(product, price);
- }
- else
- {
- shop[product] = price;
- }
- }
- while (true)
- {
- string orders = Console.ReadLine();
- if (orders == "end of clients")
- {
- break;
- }
- var clientOrders = orders
- .Split(new char[] { ',', '-' }, StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- string clientName = clientOrders[0];
- string clientsProduct = clientOrders[clientOrders.Length - 2];
- int productQuantity = int.Parse(clientOrders[clientOrders.Length - 1]);
- if (shop.ContainsKey(clientsProduct))
- {
- Customer customer = new Customer();
- customer.Name = clientName;
- customer.ShopList.Add(clientsProduct, productQuantity);
- customer.Bill = shop[clientsProduct] * productQuantity;
- if (customers.Any(x => x.Name == clientName))
- {
- var currentCustomer = customers.First(x => x.Name == clientName);
- if (currentCustomer.ShopList.ContainsKey(clientsProduct))
- {
- currentCustomer.ShopList[clientsProduct] += productQuantity;
- currentCustomer.Bill += shop[clientsProduct] * productQuantity;
- }
- else
- {
- currentCustomer.ShopList.Add(clientsProduct, productQuantity);
- currentCustomer.Bill += shop[clientsProduct] * productQuantity;
- }
- }
- else
- {
- customers.Add(customer);
- }
- }
- }
- PrintResult(customers);
- }
- private static void PrintResult(List<Customer> customers)
- {
- decimal totalBill = 0.0m;
- foreach (Customer customer in customers.OrderBy(x => x.Name))
- {
- Console.WriteLine(customer.Name);
- foreach (KeyValuePair<string, int> item in customer.ShopList)
- {
- Console.WriteLine($"-- {item.Key} - {item.Value}");
- }
- Console.WriteLine($"Bill: {customer.Bill:F2}");
- totalBill += customer.Bill;
- }
- Console.WriteLine($"Total bill: {totalBill:F2}");
- }
- class Customer
- {
- public string Name { get; set; }
- public Dictionary<string, int> ShopList { get; set; }
- public Customer()
- {
- ShopList = new Dictionary<string, int>();
- }
- public decimal Bill { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment