Advertisement
elena1234

Explicit Interfaces ( interface )

Apr 10th, 2021 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using ExplicitInterfaces.Interfaces;
  2. using ExplicitInterfaces.IO;
  3. using ExplicitInterfaces.Models;
  4. using System.Collections.Generic;
  5.  
  6. namespace ExplicitInterfaces.Core
  7. {
  8.     public class Engine : IEngine
  9.     {
  10.         private IReader reader;
  11.         private IWriter writer;
  12.  
  13.         private List<Citizen> citizens;
  14.  
  15.         private Engine()
  16.         {
  17.             this.citizens = new List<Citizen>();
  18.         }
  19.  
  20.         public Engine(IReader reader, IWriter writer)
  21.             :this()
  22.         {
  23.             this.reader = reader;
  24.             this.writer = writer;
  25.         }
  26.  
  27.         public void Run()
  28.         {
  29.             string input;
  30.             while((input = reader.ReadLine()) != "End")
  31.             {
  32.                 string[] citizen = input.Split();
  33.                 string name = citizen[0];
  34.                 string country = citizen[1];
  35.                 int age = int.Parse(citizen[2]);
  36.                 Citizen newCitizen = new Citizen(name, country, age);
  37.                 citizens.Add(newCitizen);
  38.             }
  39.  
  40.             foreach ( Citizen citizen in citizens)
  41.             {
  42.                 writer.WriteLine(((IPerson)citizen).GetName());
  43.                 writer.WriteLine(((IResident)citizen).GetName());
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. //    using ExplicitInterfaces.Interfaces;
  52.  
  53. namespace ExplicitInterfaces.Models
  54. {
  55.     public class Citizen : IPerson, IResident
  56.     {
  57.         public Citizen(string name, string country, int age)
  58.         {
  59.             this.Name = name;
  60.             this.Country = country;
  61.             this.Age = age;
  62.         }
  63.  
  64.         public string Name { get; }
  65.         public string Country { get; }
  66.         public int Age { get; }
  67.  
  68.        string IResident.GetName()
  69.         {
  70.            return $"Mr/Ms/Mrs {this.Name}";
  71.         }
  72.  
  73.         string IPerson.GetName()
  74.         {
  75.             return this.Name;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement