Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 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.                 int age = 0;
  44.                 double salary = 0;
  45.                 bool isAge = int.TryParse(tokens[1], out age);
  46.                 bool isSalary = double.TryParse(tokens[1], out salary);
  47.                 var user = new User(username);
  48.  
  49.                 if (isAge)
  50.                 {
  51.                     user.Age = age;
  52.                     withAge.Add(username, user);
  53.                 }
  54.                 else if (isSalary)
  55.                 {
  56.                     user.Salary = salary;
  57.                     withSalary.Add(username, user);
  58.                 }
  59.                 else
  60.                 {
  61.                     user.Position = tokens[1];
  62.                     withPosition.Add(username, user);
  63.                 }
  64.                
  65.                 input = Console.ReadLine();
  66.             }
  67.  
  68.             var filter = Console.ReadLine();
  69.  
  70.             if (filter == "Position")
  71.             {
  72.                 foreach (var user in withPosition)
  73.                 {
  74.                     Console.WriteLine(user.Value);
  75.                     Console.WriteLine($"Position: {user.Value.Position}");
  76.                     Console.WriteLine(new string('=', 20));
  77.                 }
  78.             }
  79.             else if (filter == "Salary")
  80.             {
  81.                 foreach (var user in withSalary)
  82.                 {
  83.                     Console.WriteLine(user.Value);
  84.                     Console.WriteLine($"Salary: {user.Value.Salary:F2}");
  85.                     Console.WriteLine(new string('=', 20));
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 foreach (var user in withAge)
  91.                 {
  92.                     Console.WriteLine(user.Value);
  93.                     Console.WriteLine($"Age: {user.Value.Age}");
  94.                     Console.WriteLine(new string('=', 20));
  95.                 }
  96.             }
  97.         }    
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement