Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LinqTask6SoldiersNames
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Army army = new Army();
- army.ShowNamesPositions();
- }
- }
- public class Army
- {
- private List <Soldier> _soldiers;
- public Army()
- {
- FillSoldiers();
- }
- public void ShowNamesPositions()
- {
- Console.WriteLine("Полный список информации о солдатах:");
- ShowAllSoldiers();
- Console.WriteLine("\nСписок имен и занимаемых должностей:");
- var namesPositions = _soldiers.Select(soldier => new { Name = soldier.Name, Position = soldier.Position });
- foreach (var namePosition in namesPositions)
- {
- Console.WriteLine($"{namePosition.Name} - {namePosition.Position}");
- }
- }
- public void ShowAllSoldiers()
- {
- foreach (var soldier in _soldiers)
- {
- soldier.ShowInfo();
- }
- }
- private void FillSoldiers()
- {
- _soldiers = new List<Soldier>
- {
- new Soldier("Вася", "рядовой", "пистолет", 10),
- new Soldier("Петя", "рядовой", "узи", 5),
- new Soldier("Дима", "лейтенант", "топор", 15),
- new Soldier("Леша", "ефрейтор", "половник", 20),
- new Soldier("Коля", "полковник", "зубная щетка", 25),
- new Soldier("Гена", "генерал", "рупор", 30)
- };
- }
- }
- public class Soldier
- {
- private string _weapon;
- private int _workTime;
- public Soldier(string name, string position, string weapon, int workTime)
- {
- Name = name;
- Position = position;
- _weapon = weapon;
- _workTime = workTime;
- }
- public string Name { get; private set; }
- public string Position { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Name} - {Position}, служит {_workTime} месяцев и носит {_weapon}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement