Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace FightersTransfer
- {
- internal class Program
- {
- static void Main()
- {
- SoldierBase soldierBase = new SoldierBase();
- List<Soldier> squad1 = soldierBase.Fill();
- List<Soldier> squad2 = soldierBase.Fill();
- soldierBase.Show(squad1, squad2);
- Console.WriteLine("\nSquads after transfer");
- soldierBase.Transfer(squad1, squad2, out List<Soldier> transferredSquad1, out List<Soldier> transferredSquad2);
- soldierBase.Show(transferredSquad1, transferredSquad2);
- Console.ReadKey();
- }
- }
- public class SoldierBase
- {
- private readonly SoldierFactory _soldierFactory;
- public SoldierBase()
- {
- _soldierFactory = new SoldierFactory();
- }
- public List<Soldier> Fill()
- {
- return _soldierFactory.Create().ToList();
- }
- public void Show(List<Soldier> squad1, List<Soldier> squad2)
- {
- for (int i = 0; i < Math.Max(squad1.Count, squad2.Count); i++)
- {
- string soldier1 = i < squad1.Count ? squad1[i].Name : "";
- string soldier2 = i < squad2.Count ? squad2[i].Name : "";
- string tabulation = "\t";
- int maxLength = 7;
- if (soldier1.Length <= maxLength)
- {
- tabulation += tabulation;
- }
- Console.WriteLine($"{soldier1}{tabulation}{soldier2}");
- }
- }
- public void Transfer(List<Soldier> squad1, List<Soldier> squad2, out List<Soldier> transferredSquad1, out List<Soldier> transferredSquad2)
- {
- string initialLetter = "B";
- var transferredSoldiers = squad1.Where(soldier => soldier.Name.StartsWith(initialLetter));
- transferredSquad1 = squad1.Except(transferredSoldiers).ToList();
- transferredSquad2 = squad2.Union(transferredSoldiers).ToList();
- }
- }
- public class SoldierFactory
- {
- private readonly NameProvider _nameProvider;
- private readonly Random _random;
- public SoldierFactory()
- {
- _nameProvider = new NameProvider();
- _random = new Random();
- }
- public List<Soldier> Create()
- {
- int soldierCount = 10;
- List<Soldier> soldiers = new List<Soldier>();
- for (int i = 0; i < soldierCount; i++)
- {
- soldiers.Add(new Soldier(_nameProvider.GetNames()[_random.Next(_nameProvider.GetNames().Count)]));
- }
- return soldiers.ToList();
- }
- }
- public class NameProvider
- {
- private readonly List<string> _names;
- public NameProvider()
- {
- _names = new List<string>()
- {
- "Shevchenko", "Vorobev", "Romanov", "Sergeev", "Grigorev", "Kiselev",
- "Bondarenko", "Baranov","Bondarenko", "Baranov","Bondarenko", "Baranov", "Kuzmin", "Frolov"
- };
- }
- public List<string> GetNames()
- {
- return _names;
- }
- }
- public class Soldier
- {
- public Soldier(string name)
- {
- Name = name;
- }
- public string Name { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement