Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LinqTrain
- {
- class Program
- {
- public static void Main()
- {
- List<Soldier> soldiers = new List<Soldier>();
- Console.WriteLine("Вывод до проекции");
- for (int i = 0; i < 10; i++)
- {
- soldiers.Add(new Soldier());
- Console.WriteLine(soldiers[i].GetInfo());
- }
- Console.ReadKey();
- Console.Clear();
- var soldiersData = soldiers.Select(soldier => new
- {
- Name = soldier.Name,
- Rank = soldier.Rank
- });
- Console.WriteLine("Вывод после проекции");
- foreach (var item in soldiersData)
- {
- Console.WriteLine($"{item.Name} - {item.Rank}");
- }
- Console.ReadKey();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- }
- public static class GeneratorName
- {
- public static string CreateName()
- {
- string result = "";
- int length = RandomStatic.GetNext(3, 10);
- for (int i = 0; i < length; i++)
- {
- result += (char)RandomStatic.GetNext('A', 'Z' + 1);
- }
- return result;
- }
- }
- class Soldier
- {
- public string Name { get; private set; }
- public string Weapon { get; private set; }
- public string Rank { get; private set; }
- public int WorkTime { get; private set; }
- public Soldier()
- {
- Name = GeneratorName.CreateName();
- Weapon = new string[] { "Пистолет", "Автомат", "Винтовка" }[RandomStatic.GetNext(0, 3)];
- Rank = new string[] { "Рядовой", "Сержант", "Прапор" }[RandomStatic.GetNext(0, 3)];
- WorkTime = RandomStatic.GetNext(0, 13);
- }
- public string GetInfo()
- {
- return $"{Name} - {Weapon} - {Rank} - {WorkTime} месяцев";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement