amphibia89

Sales Report

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