Advertisement
DaniPasteBin

Untitled

Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07.AndreyAndBiliard
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int count = int.Parse(Console.ReadLine());
  12.             List<Product> products = new List<Product>();
  13.             for (int i = 0; i < count; i++)
  14.             {
  15.                 string[] tokens = Console.ReadLine().Split('-');
  16.                 string productName = tokens[0];
  17.                 decimal price = decimal.Parse(tokens[1]);
  18.  
  19.                 Product product = products.FirstOrDefault(p => p.Name == productName);
  20.  
  21.                 if (product == null)
  22.                 {
  23.                     Product newProduct = new Product(productName, price);
  24.                     products.Add(newProduct);
  25.                 }
  26.                 else
  27.                 {
  28.                     product.Price = price;
  29.                 }
  30.             }
  31.             List<Customer> customers = new List<Customer>();
  32.             while (true)
  33.             {
  34.                 string line = Console.ReadLine();
  35.  
  36.                 if (line == "end of clients")
  37.                 {
  38.                     break;
  39.                 }
  40.                 string[] tokens = line.Split(new[] { '-', ',' },  //нов char масив
  41.                     StringSplitOptions.RemoveEmptyEntries);         //да махне празните пространства
  42.                 string customerName = tokens[0];
  43.                 string productName = tokens[1];
  44.                 int productQuantity = int.Parse(tokens[2]);
  45.                 //прочетена е информацията за всеки клиент и продукт
  46.                 //извеждам проверка за случаите когато във входа ще има клиент с продукт извън листата
  47.                 if (!products.Any(p => p.Name == productName))
  48.                 {
  49.                     continue;
  50.                 }
  51.  
  52.                 Customer customer = customers.
  53.                     FirstOrDefault(c => c.Name == customerName);
  54.  
  55.                 if (customer == null)
  56.                 {
  57.                     Customer newCustomer = new Customer(customerName);
  58.                     newCustomer.OrderedProducts.Add(productName, productQuantity);
  59.                     customers.Add(newCustomer);
  60.                 }
  61.                 else
  62.                 {
  63.                     if (customer.OrderedProducts.ContainsKey(productName) == false)
  64.                     {
  65.                         customer.OrderedProducts.Add(productName, productQuantity);
  66.                     }
  67.                     else
  68.                     {
  69.                         customer.OrderedProducts[productName] += productQuantity;
  70.                     }
  71.                 }
  72.             }
  73.             decimal totalBill = 0;
  74.             foreach (Customer customer in customers.OrderBy(c=>c.Name))
  75.             {
  76.                 Console.WriteLine(customer.Name);
  77.                 decimal bill = 0;
  78.                 foreach (var orderedProduct in customer.OrderedProducts)
  79.                 {
  80.                     Console.WriteLine($"-- {orderedProduct.Key} - {orderedProduct.Value}");
  81.                     string productName = orderedProduct.Key;
  82.                     int quantity = orderedProduct.Value;
  83.  
  84.                     decimal price = products.First(p => p.Name == productName).Price; //първия продукт който е от списъка
  85.  
  86.                     bill += quantity * price;
  87.                 }
  88.  
  89.                 totalBill+= bill;
  90.                 Console.WriteLine($"Bill: {bill:f2}");
  91.             }
  92.             Console.WriteLine($"Total bill: {totalBill:f2}");
  93.  
  94.  
  95.  
  96.         }
  97.         class Product  //прочитат се продуктите поставени в клас
  98.         {
  99.             public Product(string name, decimal price) //конструктор
  100.             {
  101.                 Name = name;
  102.                 Price = price;
  103.             }
  104.  
  105.             public string Name { get; set; }
  106.             public decimal Price { get; set; }
  107.         }
  108.         class Customer
  109.         {
  110.             public Customer(string name)
  111.             {
  112.                 this.Name = name;
  113.                 this.OrderedProducts = new Dictionary<string,int>();
  114.             }
  115.  
  116.             public string Name { get; set; }
  117.             public Dictionary<string, int> OrderedProducts { get; set;}
  118.  
  119.         }
  120.     }
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement