Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSharpPreparation
  5. {
  6.     public class User
  7.     {
  8.         public User(string name)
  9.         {
  10.             this.Name = name;
  11.             this.Position = string.Empty;
  12.             this.Age = -1;
  13.             this.Salary = -1;
  14.         }
  15.  
  16.         public string Name { get; }
  17.  
  18.         public string Position { get; set; }
  19.  
  20.         public int Age { get; set; }
  21.  
  22.         public double Salary { get; set; }
  23.  
  24.         public override string ToString()
  25.         {
  26.             return $"Name: {this.Name}";
  27.         }
  28.     }
  29.  
  30.     public class Program
  31.     {
  32.         private static void Main(string[] args)
  33.         {
  34.             var input = Console.ReadLine();
  35.             var withPosition = new Dictionary<string, User>();
  36.             var withSalary = new Dictionary<string, User>();
  37.             var withAge = new Dictionary<string, User>();
  38.  
  39.             while (input != "filter base")
  40.             {
  41.                 var tokens = input.Split(new[] { ' ', '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
  42.                 var username = tokens[0];
  43.                 bool isAge = int.TryParse(tokens[1], out int age);
  44.                 bool isSalary = double.TryParse(tokens[1], out double salary);
  45.                 var user = new User(username);
  46.  
  47.                 if (isAge)
  48.                 {
  49.                     user.Age = age;
  50.                     withAge.Add(username, user);
  51.                 }
  52.                 else if (isSalary)
  53.                 {
  54.                     user.Salary = salary;
  55.                     withSalary.Add(username, user);
  56.                 }
  57.                 else
  58.                 {
  59.                     user.Position = tokens[1];
  60.                     withPosition.Add(username, user);
  61.                 }
  62.                
  63.                 input = Console.ReadLine();
  64.             }
  65.  
  66.             var filter = Console.ReadLine();
  67.  
  68.             if (filter == "Position")
  69.             {
  70.                 foreach (var user in withPosition)
  71.                 {
  72.                     Console.WriteLine(user.Value);
  73.                     Console.WriteLine($"Position: {user.Value.Position}");
  74.                     Console.WriteLine(new string('=', 20));
  75.                 }
  76.             }
  77.             else if (filter == "Salary")
  78.             {
  79.                 foreach (var user in withSalary)
  80.                 {
  81.                     Console.WriteLine(user.Value);
  82.                     Console.WriteLine($"Salary: {user.Value.Salary:F2}");
  83.                     Console.WriteLine(new string('=', 20));
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 foreach (var user in withAge)
  89.                 {
  90.                     Console.WriteLine(user.Value);
  91.                     Console.WriteLine($"Age: {user.Value.Age}");
  92.                     Console.WriteLine(new string('=', 20));
  93.                 }
  94.             }
  95.         }    
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement