Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace AndreyAndBilliard
- {
- class Customer
- {
- public string Name { get; set; }
- public Dictionary<string, int> Products { get; set; }
- public void AddProduct(Dictionary<string, int> productToAdd)
- {
- string productName = productToAdd.ElementAt(0).Key;
- int quantity = productToAdd.ElementAt(0).Value;
- if (!Products.ContainsKey(productName))
- {
- Products.Add(productName, quantity);
- }
- else
- {
- Products[productName] += quantity;
- }
- }
- public decimal GetCustomerBill(Dictionary<string, decimal> priceList)
- {
- decimal bill = 0.0m;
- foreach (var product in Products)
- {
- bill += product.Value * priceList[product.Key];
- }
- return bill;
- }
- }
- class Program
- {
- static void Main()
- {
- var priceList = GeneratePriceList(int.Parse(Console.ReadLine()));
- var customerList = new List<Customer>();
- AddClients(priceList, customerList);
- decimal totalBill = 0.0m;
- foreach (var customer in customerList.OrderBy(s => s.Name))
- {
- Console.WriteLine(customer.Name);
- foreach (var product in customer.Products)
- {
- Console.WriteLine($"-- {product.Key} - {product.Value}");
- };
- decimal customerBill = customer.GetCustomerBill(priceList);
- totalBill += customerBill;
- Console.WriteLine($"Bill: {customerBill:F2}");
- }
- Console.WriteLine($"Total bill: {totalBill:F2}");
- }
- private static void AddClients(Dictionary<string, decimal> priceList, List<Customer> customerList)
- {
- string input = Console.ReadLine();
- while (input != "end of clients")
- {
- var splitted = ReadInput(input);
- if (priceList.ContainsKey(splitted[1]))
- {
- var newCustomer = ReadCustomer(splitted);
- if (!customerList.Any(s => s.Name == newCustomer.Name))
- {
- customerList.Add(newCustomer);
- }
- else
- {
- var productToAdd = newCustomer.Products;
- foreach (var customer in customerList)
- {
- if (customer.Name == newCustomer.Name)
- {
- customer.AddProduct(newCustomer.Products);
- break;
- }
- }
- }
- }
- input = Console.ReadLine();
- }
- }
- private static Customer ReadCustomer(string[] splitted)
- {
- return new Customer
- {
- Name = splitted[0],
- Products = new Dictionary<string, int>() { { splitted[1], int.Parse(splitted[2]) } }
- };
- }
- private static string[] ReadInput(string input)
- {
- return input
- .Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
- }
- private static Dictionary<string, decimal> GeneratePriceList(int n)
- {
- var priceList = new Dictionary<string, decimal>();
- for (int i = 0; i < n; i++)
- {
- var input = Console.ReadLine()
- .Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
- priceList[input[0]] = decimal.Parse(input[1]);
- }
- return priceList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement