Advertisement
ZhongNi

WeaponsReport

Dec 20th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace WeaponsReport
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             SoldierBase soldierBase = new SoldierBase();
  12.             List<Soldier> soldiers = soldierBase.Fill();
  13.             soldierBase.Show(soldiers);
  14.  
  15.             Console.WriteLine("\n Received data");
  16.             List<Soldier> selectedSoldiers = soldierBase.GetDataset(soldiers);
  17.             soldierBase.Show(selectedSoldiers);
  18.             Console.ReadKey();
  19.         }
  20.     }
  21.  
  22.     public class SoldierBase
  23.     {
  24.         public List<Soldier> Fill()
  25.         {
  26.             SoldierProvider soldierProvider = new SoldierProvider();
  27.  
  28.             return soldierProvider.GetSoldiers();
  29.         }
  30.  
  31.         public void Show(List<Soldier> soldiers)
  32.         {
  33.             foreach (Soldier soldier in soldiers)
  34.             {
  35.                 string serviceTime = soldier.ServiceTime != 0 ? soldier.ServiceTime.ToString() : null;
  36.                 Console.WriteLine($"{soldier.Rank} {soldier.Name}\t{soldier.Weapon} {serviceTime}");
  37.             }
  38.         }
  39.  
  40.         public List<Soldier> GetDataset(List<Soldier> soldiers)
  41.         {
  42.             List<Soldier> selectedSoldiers = new List<Soldier>();
  43.             var dataset = soldiers.Select(soldier => (soldier.Name, soldier.Rank));
  44.  
  45.             foreach (var record in dataset)
  46.             {
  47.                 selectedSoldiers.Add(new Soldier(record.Name, record.Rank));
  48.             }
  49.  
  50.             return selectedSoldiers.ToList();
  51.         }
  52.     }
  53.  
  54.     public class SoldierProvider
  55.     {
  56.         private readonly List<Soldier> _soldiers;
  57.  
  58.         public SoldierProvider()
  59.         {
  60.             _soldiers = new List<Soldier>() {
  61.                 new Soldier( "Ivanov","private","assault rifle",1),
  62.                 new Soldier( "Kuznetsov","private","assault rifle",2),
  63.                 new Soldier( "Smirnov","private","assault rifle",1),
  64.                 new Soldier( "Popov","private","machine gun",2),
  65.                 new Soldier( "Petrov","sergeant","assault rifle",5),
  66.             };
  67.         }
  68.  
  69.         public List<Soldier> GetSoldiers()
  70.         {
  71.             return _soldiers;
  72.         }
  73.     }
  74.  
  75.     public class Soldier
  76.     {
  77.         public Soldier(string name, string rank)
  78.         {
  79.             Name = name;
  80.             Rank = rank;
  81.         }
  82.  
  83.         public Soldier(string name, string rank, string weapon, int serviceTime)
  84.         {
  85.             Name = name;
  86.             Rank = rank;
  87.             Weapon = weapon;
  88.             ServiceTime = serviceTime;
  89.         }
  90.  
  91.         public string Name { get; private set; }
  92.         public string Rank { get; private set; }
  93.         public string Weapon { get; private set; }
  94.         public int ServiceTime { get; private set; }
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement