Advertisement
dimipan80

Office Stuff

May 18th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. /* You are given a sequence of n companies in format |<company> - <amount> - <product>|.
  2.  * Write a program that prints all companies in alphabetical order. For each company print the product type and their aggregated ordered amounts. Order the products by order of appearance. Print the result in the following format: <company>: <product>-<amount>, <product>-<amount>,… */
  3.  
  4. namespace Office_Stuff
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.Text.RegularExpressions;
  9.  
  10.     class OfficeStuff
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var companiesInfo = new SortedDictionary<string, Dictionary<string, int>>();
  15.             GetInputInformation(companiesInfo);
  16.             PrintResults(companiesInfo);
  17.         }
  18.  
  19.         private static void GetInputInformation(SortedDictionary<string, Dictionary<string, int>> companiesInfo)
  20.         {
  21.             const string pattern = @"\|\s*(\w+)\s*-\s*(\d{1,4})\s*-\s*(\w+)\s*\|";
  22.  
  23.             Regex rgx = new Regex(pattern);
  24.             int n = int.Parse(Console.ReadLine());
  25.             for (int i = 0; i < n; i++)
  26.             {
  27.                 Match match = rgx.Match(Console.ReadLine());
  28.                 string compName = match.Groups[1].Value;
  29.                 int amount = int.Parse(match.Groups[2].Value);
  30.                 string product = match.Groups[3].Value;
  31.  
  32.                 if (!companiesInfo.ContainsKey(compName))
  33.                 {
  34.                     companiesInfo[compName] = new Dictionary<string, int>();
  35.                 }
  36.  
  37.                 if (!companiesInfo[compName].ContainsKey(product))
  38.                 {
  39.                     companiesInfo[compName][product] = 0;
  40.                 }
  41.  
  42.                 companiesInfo[compName][product] += amount;
  43.             }
  44.         }
  45.  
  46.         private static void PrintResults(SortedDictionary<string, Dictionary<string, int>> companiesInfo)
  47.         {
  48.             foreach (var company in companiesInfo)
  49.             {
  50.                 Console.Write("{0}: ", company.Key);
  51.                 int counter = company.Value.Count;
  52.                 foreach (var product in company.Value)
  53.                 {
  54.                     counter--;
  55.                     Console.Write("{0}-{1}", product.Key, product.Value);
  56.                     if (counter > 0)
  57.                     {
  58.                         Console.Write(", ");
  59.                     }
  60.                 }
  61.  
  62.                 Console.WriteLine();
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement