Advertisement
Aborigenius

SalesReport

Aug 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 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 Rectangle
  8. {
  9.  
  10.  
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int n = int.Parse(Console.ReadLine());
  16.             List<Sale> sales = new List<Sale>();
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 Sale sale = ReadSale(Console.ReadLine());
  20.                 sales.Add(sale);
  21.             }
  22.  
  23.             List<string> towns = sales.Select(s => s.Town).Distinct().OrderBy(t => t).ToList();
  24.  
  25.             foreach (string town in towns)
  26.             {
  27.                 double currentSum = sales.Where(s => s.Town == town).Select(s => s.Price * s.Quantity).Sum();
  28.                 Console.WriteLine($"{town} -> {currentSum:F2}");
  29.             }
  30.         }
  31.  
  32.         private static Sale ReadSale(string input)
  33.         {
  34.             string[] tokens = input.Split(' ');
  35.             string town = tokens[0];
  36.             string product = tokens[1];
  37.             double price = double.Parse(tokens[2]);
  38.             double quantity = double.Parse(tokens[3]);
  39.  
  40.             return new Sale
  41.             {
  42.                 Town = town,
  43.                 Product = product,
  44.                 Price = price,
  45.                 Quantity = quantity
  46.             };
  47.         }
  48.     }
  49.     class Sale
  50.     {
  51.         public string Town { get; set; }
  52.         public string Product { get; set; }
  53.         public double Price { get; set; }
  54.         public double Quantity { get; set; }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement