Advertisement
simonradev

FixedAndrey

Feb 10th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 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 AndreyAndBilliard
  8. {
  9.     class AndreyAndBilliard
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int countProd = int.Parse(Console.ReadLine());
  14.             var Products = new Dictionary<string, decimal>();
  15.             var listOfCustomers = new List<Customer>();
  16.             for (int i = 0; i < countProd; i++)
  17.             {
  18.                 var inputProd = Console.ReadLine().Split('-');
  19.  
  20.                 //няма смисъл от проверка на този ред
  21.                 Products[inputProd[0]] = decimal.Parse(inputProd[1]);
  22.             }
  23.  
  24.             var inputCustomer = Console.ReadLine().Split('-');
  25.             while (inputCustomer[0] != "end of clients")
  26.             {
  27.                 string currClient = inputCustomer[0];
  28.                 string[] currProducts = inputCustomer[1].Split(',');
  29.  
  30.                 //Пропуснал си да поемеш варианта в който някой идва да си купи още нещо
  31.                 //Тоест ако един човек си купи един път бира и един път кола трябва да добавиш колата към сметката
  32.                 //Това което правиш ти е постоянно да създаваш нов клиент
  33.  
  34.                 //първо проверявам дали има такъв продукт в листа и ако няма пропускам следващите редове с "continue"
  35.                 //като не забравяме да си прочетем входа отново за да не гръмне
  36.                 if (!Products.ContainsKey(currProducts[0]))
  37.                 {
  38.                     inputCustomer = Console.ReadLine().Split('-');
  39.  
  40.                     continue;
  41.                 }
  42.  
  43.                 //ако има такъв продукт проверяваме дали в листа с клиени има вече такъв клиент
  44.                 //ще ти го напиша с "foreach" както ти си почнал за да не ти променям стила на кода твърде много
  45.                 //Правя си един нов "Customer" но ако има такъв вече си го променяме в цикъла
  46.                 //Пазим си една булева променлива за да знаем дали клиента съществува или не за да може по надолу да я ползваме
  47.                 Customer newCustomer = new Customer();
  48.                 newCustomer.Name = currClient;
  49.                 newCustomer.Product = new Dictionary<string, int>();
  50.  
  51.                 bool custExists = false;
  52.                 foreach (Customer cust in listOfCustomers)
  53.                 {
  54.                     if (cust.Name == currClient)
  55.                     {
  56.                         newCustomer = cust;
  57.                         custExists = true;
  58.  
  59.                         break;
  60.                     }
  61.                 }
  62.  
  63.                 //тук проверяваме дали съществува такъв продукт и ако не създаваме
  64.                 //след това добавяме количество
  65.                 if (!newCustomer.Product.ContainsKey(currProducts[0]))
  66.                 {
  67.                     newCustomer.Product.Add(currProducts[0], 0);
  68.                 }
  69.  
  70.                 newCustomer.Product[currProducts[0]] += int.Parse(currProducts[1]);
  71.  
  72.                 //тези три вложени цикъла не ти трябват
  73.                 //просто вземаме клиента и добавяме към сметката която вече има новата цена
  74.                 newCustomer.Bill += int.Parse(currProducts[1]) * Products[currProducts[0]];
  75.  
  76.                 //добавяме клиента в листа ако не съществува вече такъв
  77.                 if (!custExists)
  78.                 {
  79.                     listOfCustomers.Add(newCustomer);
  80.                 }
  81.  
  82.                 inputCustomer = Console.ReadLine().Split('-');
  83.             }
  84.  
  85.             decimal totalBill = 0;
  86.             var sortedList = listOfCustomers
  87.                 .OrderBy(a => a.Name);
  88.             foreach (var cust in sortedList)
  89.             {
  90.                 Console.WriteLine(cust.Name);
  91.                 foreach (var item in cust.Product)
  92.                 {
  93.                     Console.WriteLine($"-- {item.Key} - {item.Value}");
  94.                 }
  95.  
  96.                 Console.WriteLine($"Bill: {cust.Bill:f2}");
  97.                 totalBill += cust.Bill;
  98.             }
  99.             Console.WriteLine($"Total bill: {totalBill:f2}");
  100.  
  101.         }
  102.     }
  103.  
  104.     class Customer
  105.     {
  106.         public string Name { get; set; }
  107.         public Dictionary<string, int> Product { get; set; }
  108.         public decimal Bill { get; set; }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement