Advertisement
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
- {
- class Program
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- List<Product> products = new List<Product>();
- for (int i = 0; i < n; i++)
- {
- string[] tokens = Console.ReadLine().Split("-");
- string productName = tokens[0];
- decimal price = decimal.Parse(tokens[1]);
- Product product = products.FirstOrDefault(p => p.ProductName == productName);
- if (product == null)
- {
- Product newProduct = new Product(productName, price);
- products.Add(newProduct);
- }
- else
- {
- product.Price = price;
- }
- }
- List<Customer> customers = new List<Customer>();
- while (true)
- {
- string line = Console.ReadLine();
- if (line == "end of clients")
- {
- break;
- }
- string[] tokens = line.Split(new[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
- string customerName = tokens[0];
- string productName = tokens[1];
- int quantity = int.Parse(tokens[2]);
- if ((products.Any(p => p.ProductName == productName)) == false)
- {
- continue;
- }
- Customer customer = customers.FirstOrDefault(c => c.Name == customerName);
- if (customer == null)
- {
- Customer newCustomer = new Customer(customerName);
- newCustomer.OrderedProducts.Add(productName, quantity);
- customers.Add(newCustomer);
- }
- else
- {
- if (customer.OrderedProducts.ContainsKey(productName) == false)
- {
- customer.OrderedProducts.Add(productName, quantity);
- }
- else
- {
- customer.OrderedProducts[productName] += quantity;
- }
- }
- }
- decimal totalBill = 0;
- foreach (var customer in customers.OrderBy(c => c.Name))
- {
- decimal bill = 0;
- Console.WriteLine(customer.Name);
- foreach (var orderedProduct in customer.OrderedProducts)
- {
- Console.WriteLine($"-- {orderedProduct.Key} - {orderedProduct.Value}");
- string productName = orderedProduct.Key;
- int quantity = orderedProduct.Value;
- decimal price = products.First(p => p.ProductName == productName).Price;
- bill += price * quantity;
- }
- totalBill += bill;
- Console.WriteLine($"Bill: {bill:f2}");
- }
- Console.WriteLine($"Total bill: {totalBill:f2}");
- }
- }
- class Product
- {
- public Product(string productName, decimal price)
- {
- ProductName = productName;
- Price = price;
- }
- public string ProductName { get; set; }
- public decimal Price { get; set; }
- }
- class Customer
- {
- public Customer(string name)
- {
- Name = name;
- OrderedProducts = new Dictionary<string, int>();
- }
- public string Name { get; set; }
- public Dictionary<string, int> OrderedProducts { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement