Advertisement
grubcho

Filter base - .TryParse - Dictionaries

Jul 12th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Filter_base
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, int> age = new Dictionary<string, int>();
  14.             Dictionary<string, double> salary = new Dictionary<string, double>();
  15.             Dictionary<string, string> position = new Dictionary<string, string>();
  16.  
  17.             string input = Console.ReadLine();
  18.             while (input != "filter base")
  19.             {
  20.                 string[] arr = input.Split(' ').ToArray();
  21.                 string name = arr[0];
  22.                 string info = arr[2];
  23.  
  24.                 int valueInt;
  25.                 double valueDouble;
  26.  
  27.                 if (int.TryParse(info, out valueInt))
  28.                 {
  29.                     age[name] = int.Parse(info);
  30.                 }
  31.                 else if (double.TryParse(info, out valueDouble))
  32.                 {
  33.                     salary[name] = double.Parse(info);
  34.                 }
  35.                 else
  36.                 {
  37.                     position[name] = info;
  38.                 }
  39.                 input = Console.ReadLine();
  40.             }
  41.             string sort = Console.ReadLine();
  42.             switch (sort)
  43.             {
  44.                 case "Age":
  45.                     foreach (KeyValuePair<string, int> person in age)
  46.                     {
  47.                         Console.WriteLine($"Name: {person.Key}");                        
  48.                         Console.WriteLine($"Age: {person.Value}");
  49.                         Console.WriteLine("{0}", new string('=', 20));
  50.                     }
  51.                     break;
  52.                 case "Salary":
  53.                     foreach (KeyValuePair<string, double> person in salary)
  54.                     {
  55.                         Console.WriteLine($"Name: {person.Key}");                        
  56.                         Console.WriteLine("Salary: {0:f2}", person.Value);
  57.                         Console.WriteLine("{0}", new string('=', 20));
  58.                     }
  59.                     break;
  60.                 case "Position":
  61.                     foreach (KeyValuePair<string, string> person in position)
  62.                     {
  63.                         Console.WriteLine($"Name: {person.Key}");                        
  64.                         Console.WriteLine($"Position: {person.Value}");
  65.                         Console.WriteLine("{0}", new string('=', 20));
  66.                     }
  67.                     break;                
  68.             }  
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement