Advertisement
Nikolay_Kashev

Sales Report (second solution)

May 27th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _07._Sales_Report__second_solution_
  5. {
  6.     class Sale
  7.     {
  8.         public string Town { get; set; }
  9.         public string Product { get; set; }
  10.         public decimal Price { get; set; }
  11.         public decimal Quantity { get; set; }
  12.  
  13.         public Sale(string Town, string Product, decimal Price, decimal Quantity)
  14.         {
  15.             this.Town = Town;
  16.             this.Product = Product;
  17.             this.Price = Price;
  18.             this.Quantity = Quantity;
  19.         }
  20.  
  21.         public decimal Multiply()
  22.         {
  23.             return Price * Quantity;
  24.         }
  25.     }
  26.  
  27.     class Program
  28.     {
  29.         static void Main(string[] args)
  30.         {
  31.             int n = int.Parse(Console.ReadLine());
  32.  
  33.             List<Sale> sales = new List<Sale>();
  34.  
  35.             SortedDictionary<string, decimal> totalSalesByTown = new SortedDictionary<string, decimal>();
  36.  
  37.             for (int i = 0; i < n; i++)
  38.             {
  39.                 string[] data = Console.ReadLine().Split();
  40.                 string town = data[0];
  41.                 string product = data[1];
  42.                 decimal price = decimal.Parse(data[2]);
  43.                 decimal quantity = decimal.Parse(data[3]);
  44.  
  45.                 Sale sale = new Sale(town, product, price, quantity);
  46.                 sales.Add(sale);
  47.             }
  48.  
  49.             foreach (var sale in sales)
  50.             {
  51.                 if (!totalSalesByTown.ContainsKey(sale.Town))
  52.                 {
  53.                     totalSalesByTown.Add(sale.Town, sale.Multiply());
  54.                 }
  55.                 else
  56.                 {
  57.                     totalSalesByTown[sale.Town] += sale.Multiply();
  58.                 }
  59.             }
  60.             foreach (var town in totalSalesByTown)
  61.             {
  62.                 Console.WriteLine($"{town.Key} -> {town.Value:f2}");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement