Advertisement
Nikolay_Kashev

Sales Report

May 27th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.  
  11.         Sale[] sales = new Sale[n];
  12.         for (int i = 0; i < n; i++)
  13.         {
  14.             sales[i] = readSale();
  15.         }
  16.         SortedDictionary<string, decimal> salesByTown = new SortedDictionary<string, decimal>();
  17.         for (int i = 0; i < n; i++)
  18.         {
  19.             if (!salesByTown.ContainsKey(sales[i].town))
  20.             {
  21.                 salesByTown.Add(sales[i].town, 0);
  22.             }
  23.  
  24.             salesByTown[sales[i].town] += sales[i].price * sales[i].quantity;
  25.         }
  26.         foreach (var town in salesByTown)
  27.         {
  28.             Console.WriteLine($"{town.Key} -> {town.Value:f2}");
  29.         }
  30.     }
  31.  
  32.     private static Sale readSale()
  33.     {
  34.  
  35.         string[] sale = Console.ReadLine().Split().ToArray();
  36.         Sale singleSale = new Sale() { town = sale[0], product = sale[1], price = decimal.Parse(sale[2]), quantity = decimal.Parse(sale[3]) };
  37.         return singleSale;
  38.     }
  39. }
  40.  
  41. class Sale
  42. {
  43.     public string town { get; set; }
  44.     public string product { get; set; }
  45.     public decimal price { get; set; }
  46.     public decimal quantity { get; set; }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement