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");
- Tuple<List<Soldier>, List<Soldier>> squads = soldierBase.Transfer(squad1, squad2);
- squad1 = squads.Item1;
- squad2 = squads.Item2;
- soldierBase.Show(squad1, squad2);
- 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 Tuple<List<Soldier>, List<Soldier>> Transfer(List<Soldier> squad1, List<Soldier> squad2)
- {
- string initialLetter = "B";
- var transferredSoldiers = squad1.Where(soldier => soldier.Name.StartsWith(initialLetter));
- squad1 = squad1.Except(transferredSoldiers).ToList();
- squad2 = squad2.Union(transferredSoldiers).ToList();
- return Tuple.Create(squad1, squad2);
- }
- }
- 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.ToList();
- }
- }
- 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