Advertisement
simonradev

AndreyAndBilliardUpdated

Feb 9th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Pr07AndreyAndBilliard
  8. {
  9.     class Pr07AndreyAndBilliard
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             Dictionary<string, decimal> infoItems = new Dictionary<string, decimal>();
  15.            
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 var info = Console.ReadLine().Split('-').ToArray();
  19.                 string product = info[0];
  20.                 decimal price = decimal.Parse(info[1]);
  21.                
  22.                 //премахнах проверката понеже условието не изисква да има такава
  23.                 //ако въведат отново такъв продукт цената се презаписва
  24.                 infoItems[product] = price;
  25.             }
  26.  
  27.             string line = string.Empty;
  28.             List<Customer> customers = new List<Customer>();
  29.  
  30.             while ((line = Console.ReadLine()) != "end of clients")
  31.             {
  32.                 string[] infocustomer = line.Split('-');
  33.                 string[] orderInfo = infocustomer[1].Split(',');
  34.  
  35.                 if (infoItems.ContainsKey(orderInfo[0]))
  36.                 {
  37.                     Customer customer = new Customer();
  38.                     customer.Name = infocustomer[0];
  39.  
  40.                     int piecesProduct = int.Parse(orderInfo[1]);
  41.                     string product = orderInfo[0];
  42.                    
  43.                     if (customers.Any(c1 => c1.Name == infocustomer[0]))
  44.                     {
  45.                         customer = customers.First(cl => cl.Name == infocustomer[0]);
  46.  
  47.                         if (!customer.Order.ContainsKey(product))
  48.                         {
  49.                             customer.Order.Add(product, piecesProduct);
  50.                             //забравил си когато добавяш продукти да добавяш и сегашната сума към сметката
  51.                             //ако използваш само знака "=" презаписваш сумата и ако си имал 12.00 лв на сметката
  52.                             //а сега имаш 3.00 ще запишеш 3.00
  53.                             customer.Bill += piecesProduct * infoItems[product];
  54.                         }
  55.                         else
  56.                         {
  57.                             customer.Order[product] += piecesProduct;
  58.                             //тук отново си забравил да добавиш към старата сума новата
  59.                             //и не трябва да умножаваш с "customer.Order[product]", а с "piecesProduct"
  60.                             //ако имаш поръчани 3 бири и 3 коли по 1 лев тоест 6лв общо
  61.                             //ако после се опиташ да добавиш още 3 бири ще ти сметне 6 бири по 1лв и ще обърка цената
  62.                             customer.Bill += piecesProduct * infoItems[product];
  63.                         }
  64.                     }
  65.                     else
  66.                     {
  67.                         customer.Order = new Dictionary<string, int>();
  68.  
  69.                         customer.Order.Add(product, piecesProduct);
  70.                         customer.Bill = piecesProduct * infoItems[product];
  71.                         customers.Add(customer);
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             decimal sum = 0;
  77.             foreach (var client in customers.OrderBy(cl => cl.Name))
  78.             {
  79.                 Console.WriteLine(client.Name);
  80.  
  81.                 foreach (var item in client.Order)
  82.                 {
  83.                     Console.WriteLine($"-- {item.Key} - {item.Value}");
  84.                 }
  85.  
  86.                 Console.WriteLine($"Bill: {client.Bill:f2}");
  87.  
  88.                 sum += client.Bill;
  89.             }
  90.  
  91.             Console.WriteLine($"Total bill: {sum:f2}");
  92.         }
  93.     }
  94.  
  95.     public class Customer
  96.     {
  97.         public string Name { get; set; }
  98.  
  99.         public Dictionary<string, int> Order { get; set; }
  100.  
  101.         public decimal Bill { get; set; }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement