Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LinqTask7SoldiersTransfer
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Army army = new Army();
- army.TransferSoldiers("Б");
- }
- }
- public class Army
- {
- private List<Soldier> _soldiers1;
- private List<Soldier> _soldiers2;
- public Army()
- {
- FillSoldiers();
- }
- public void TransferSoldiers(string firstCharacter)
- {
- Console.WriteLine("Первый отряд до перевода:");
- ShowSoldiers(_soldiers1);
- Console.WriteLine("\nВторой отряд до перевода:");
- ShowSoldiers(_soldiers2);
- List<Soldier> soldiersToTransfer = _soldiers1.Where(soldier => soldier.Surname.StartsWith(firstCharacter)).ToList();
- _soldiers1 = _soldiers1.Except(soldiersToTransfer).ToList();
- _soldiers2 = _soldiers2.Union(soldiersToTransfer).ToList();
- Console.WriteLine("\nПервый отряд после перевода:");
- ShowSoldiers(_soldiers1);
- Console.WriteLine("\nВторой отряд после перевода:");
- ShowSoldiers(_soldiers2);
- Console.WriteLine();
- }
- public void ShowSoldiers(List<Soldier> soldiers)
- {
- foreach (var soldier in soldiers)
- {
- soldier.ShowInfo();
- }
- }
- private void FillSoldiers()
- {
- _soldiers1 = new List<Soldier>
- {
- new Soldier("Вася", "Пупкин"),
- new Soldier("Петя", "Булкин"),
- new Soldier("Дима", "Кашкин"),
- new Soldier("Леша", "Бубен"),
- new Soldier("Коля", "Зяблик"),
- new Soldier("Гена", "Гейтс")
- };
- _soldiers2 = new List<Soldier>
- {
- new Soldier("Вася", "Жабкин"),
- new Soldier("Петя", "Перкин"),
- new Soldier("Дима", "Дудин"),
- new Soldier("Леша", "Хавен"),
- new Soldier("Коля", "Костин"),
- new Soldier("Гена", "Маск")
- };
- }
- }
- public class Soldier
- {
- private string _name;
- public Soldier(string name, string surname)
- {
- _name = name;
- Surname = surname;
- }
- public string Surname { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Surname} {_name}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement