Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ExamPreparation
- {
- public sealed class Product
- {
- public Product(string name, decimal price)
- {
- this.Name = name;
- this.Price = price;
- }
- public string Name { get; private set; }
- public decimal Price { get; set; }
- public int Quantity { get; set; }
- }
- public sealed class Customer
- {
- public Customer(string name)
- {
- this.Name = name;
- this.Products = new Dictionary<string, Product>();
- }
- public string Name { get; private set; }
- public Dictionary<string, Product> Products { get; set; }
- public decimal Bill
- {
- get
- {
- return this.Products.Values.Sum(x => x.Quantity * x.Price);
- }
- }
- public override string ToString()
- {
- StringBuilder output = new StringBuilder();
- output.AppendLine(this.Name);
- foreach (var product in this.Products)
- {
- output.AppendLine($"-- {product.Key} - {product.Value.Quantity}");
- }
- output.AppendLine($"Bill: {this.Bill:F2}");
- return output.ToString().Trim();
- }
- }
- public sealed class Startup
- {
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Dictionary<string, Product> products = new Dictionary<string, Product>();
- FillProducts(n, products);
- string input = Console.ReadLine();
- SortedDictionary<string, Customer> customers = new SortedDictionary<string, Customer>();
- while (input != "end of clients")
- {
- string[] tokens = input.Split(new[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
- string personName = tokens[0];
- string productName = tokens[1];
- int quantity = int.Parse(tokens[2]);
- Customer customer = new Customer(personName);
- if (products.ContainsKey(productName))
- {
- if (customers.ContainsKey(personName))
- {
- if (customers[personName].Products.ContainsKey(productName))
- {
- customers[personName].Products[productName].Quantity += quantity;
- }
- else
- {
- customers[personName].Products.Add(productName, new Product(productName, products[productName].Price));
- customers[personName].Products[productName].Quantity += quantity;
- }
- }
- else
- {
- customers.Add(personName, customer);
- customers[personName].Products.Add(productName, new Product(productName, products[productName].Price));
- customers[personName].Products[productName].Quantity += quantity;
- }
- }
- input = Console.ReadLine();
- }
- foreach (var customer in customers)
- {
- Console.WriteLine(customer.Value);
- }
- decimal totalBill = customers.Sum(c => c.Value.Bill);
- Console.WriteLine($"Total bill: {totalBill:F2}");
- }
- private static void FillProducts(int n, Dictionary<string, Product> products)
- {
- for (int i = 0; i < n; i++)
- {
- string[] tokens = Console.ReadLine().Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
- string productName = tokens[0];
- decimal cost = decimal.Parse(tokens[1]);
- if (products.ContainsKey(productName))
- {
- products[productName].Price = cost;
- }
- else
- {
- products.Add(productName, new Product(productName, cost));
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment