Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Sale
- {
- public string Town { get; set; }
- public string Product { get; set; }
- public decimal Price { get; set; }
- public decimal Quantity { get; set; }
- public decimal TotalPrice
- {
- get
- {
- return Price * Quantity;
- }
- }
- public Sale(string town, string product, decimal price, decimal quantity)
- {
- Town = town;
- Product = product;
- Price = price;
- Quantity = quantity;
- }
- }
- public class Program
- {
- public static void Main()
- {
- var sales = ReadSales();
- var townTotalPair = new SortedDictionary<string, decimal>();
- foreach (var sale in sales)
- {
- var town = sale.Town;
- townTotalPair[town] = townTotalPair.ContainsKey(town)
- ? townTotalPair[town] + sale.TotalPrice
- : sale.TotalPrice;
- }
- foreach (var pair in townTotalPair)
- Console.WriteLine($"{pair.Key} -> {pair.Value:f2}");
- }
- private static Sale[] ReadSales()
- {
- var numberOfSales = int.Parse(Console.ReadLine());
- var sales = new Sale[numberOfSales];
- for (int index = 0; index < numberOfSales; index++)
- {
- var saleInfo = Console.ReadLine().Split();
- sales[index] = new Sale(
- town: saleInfo[0],
- product: saleInfo[1],
- price: decimal.Parse(saleInfo[2]),
- quantity: decimal.Parse(saleInfo[3]));
- }
- return sales;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment