Advertisement
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;
- using System.Threading.Tasks;
- namespace AndreyAndBilliard
- {
- class AndreyAndBilliard
- {
- static void Main(string[] args)
- {
- int countProd = int.Parse(Console.ReadLine());
- var Products = new Dictionary<string, decimal>();
- var listOfCustomers = new List<Customer>();
- for (int i = 0; i < countProd; i++)
- {
- var inputProd = Console.ReadLine().Split('-');
- //няма смисъл от проверка на този ред
- Products[inputProd[0]] = decimal.Parse(inputProd[1]);
- }
- var inputCustomer = Console.ReadLine().Split('-');
- while (inputCustomer[0] != "end of clients")
- {
- string currClient = inputCustomer[0];
- string[] currProducts = inputCustomer[1].Split(',');
- //Пропуснал си да поемеш варианта в който някой идва да си купи още нещо
- //Тоест ако един човек си купи един път бира и един път кола трябва да добавиш колата към сметката
- //Това което правиш ти е постоянно да създаваш нов клиент
- //първо проверявам дали има такъв продукт в листа и ако няма пропускам следващите редове с "continue"
- //като не забравяме да си прочетем входа отново за да не гръмне
- if (!Products.ContainsKey(currProducts[0]))
- {
- inputCustomer = Console.ReadLine().Split('-');
- continue;
- }
- //ако има такъв продукт проверяваме дали в листа с клиени има вече такъв клиент
- //ще ти го напиша с "foreach" както ти си почнал за да не ти променям стила на кода твърде много
- //Правя си един нов "Customer" но ако има такъв вече си го променяме в цикъла
- //Пазим си една булева променлива за да знаем дали клиента съществува или не за да може по надолу да я ползваме
- Customer newCustomer = new Customer();
- newCustomer.Name = currClient;
- newCustomer.Product = new Dictionary<string, int>();
- bool custExists = false;
- foreach (Customer cust in listOfCustomers)
- {
- if (cust.Name == currClient)
- {
- newCustomer = cust;
- custExists = true;
- break;
- }
- }
- //тук проверяваме дали съществува такъв продукт и ако не създаваме
- //след това добавяме количество
- if (!newCustomer.Product.ContainsKey(currProducts[0]))
- {
- newCustomer.Product.Add(currProducts[0], 0);
- }
- newCustomer.Product[currProducts[0]] += int.Parse(currProducts[1]);
- //тези три вложени цикъла не ти трябват
- //просто вземаме клиента и добавяме към сметката която вече има новата цена
- newCustomer.Bill += int.Parse(currProducts[1]) * Products[currProducts[0]];
- //добавяме клиента в листа ако не съществува вече такъв
- if (!custExists)
- {
- listOfCustomers.Add(newCustomer);
- }
- inputCustomer = Console.ReadLine().Split('-');
- }
- decimal totalBill = 0;
- var sortedList = listOfCustomers
- .OrderBy(a => a.Name);
- foreach (var cust in sortedList)
- {
- Console.WriteLine(cust.Name);
- foreach (var item in cust.Product)
- {
- Console.WriteLine($"-- {item.Key} - {item.Value}");
- }
- Console.WriteLine($"Bill: {cust.Bill:f2}");
- totalBill += cust.Bill;
- }
- Console.WriteLine($"Total bill: {totalBill:f2}");
- }
- }
- class Customer
- {
- public string Name { get; set; }
- public Dictionary<string, int> Product { get; set; }
- public decimal Bill { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement