Advertisement
AnyaAS

AddressBook (interfases)

Jun 3rd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     interface IFriend
  10.     {
  11.         string Surname { get; set; }
  12.         string Telephone { get; set; }
  13.         void Print();
  14.     }
  15.     interface IEmployee
  16.     {
  17.         string WorkPhone { get; set; }
  18.         string Position { get; set; }
  19.     }
  20.     interface ICompetitor
  21.     {
  22.         string HomePhone { get; set; }
  23.         string HomeAddress { get; set; }
  24.     }
  25.  
  26.     class Friend : IFriend
  27.     {
  28.         public string Surname { get; set; }
  29.  
  30.         public string Telephone { get; set; }
  31.         public Friend(string surname = "", string telephone = "")
  32.         {
  33.             Surname = surname;
  34.             Telephone = telephone;
  35.         }
  36.         public void Print()
  37.         {
  38.             Console.WriteLine("ФИО: " + Surname + ". Номер: " + Telephone);
  39.         }
  40.     }
  41.     class Employee : IFriend, IEmployee
  42.     {
  43.  
  44.         public string Surname { get; set; }
  45.         public string Telephone { get; set; }
  46.         public string WorkPhone { get; set; }
  47.         public string Position { get; set; }
  48.         public Employee(string surname = "", string telephone = "", string workPhone = "", string position = "")
  49.         {
  50.             Surname = surname;
  51.             Telephone = telephone;
  52.             WorkPhone = workPhone;
  53.             Position = position;
  54.         }
  55.         public void Print()
  56.         {
  57.             Console.WriteLine("ФИО: " + Surname + ". Номер: " + Telephone);
  58.             Console.WriteLine("рабочий тел.: " + WorkPhone + " должность: " + Position);
  59.         }
  60.     }
  61.     class Competitor : IFriend, IEmployee, ICompetitor
  62.     {
  63.  
  64.         public string Surname { get; set; }
  65.         public string Telephone { get; set; }
  66.         public string WorkPhone { get; set; }
  67.         public string Position { get; set; }
  68.         public string HomePhone { get; set; }
  69.         public string HomeAddress { get; set; }
  70.         public Competitor(string surname = "", string telephone = "", string workPhone = "", string position = "", string homePhone = "", string homeAddress = "")
  71.         {
  72.             Surname = surname;
  73.             Telephone = telephone;
  74.             WorkPhone = workPhone;
  75.             Position = position;
  76.             HomePhone = homePhone;
  77.             HomeAddress = homeAddress;
  78.         }
  79.         public void Print()
  80.         {
  81.             Console.WriteLine("ФИО: " + Surname + ". Номер: " + Telephone);
  82.             Console.WriteLine("рабочий тел.: " + WorkPhone + " должность: " + Position);
  83.             Console.WriteLine("дом. тел.: " + HomePhone + " дом. адрес: " + HomeAddress);
  84.         }
  85.     }
  86.     class AddressBook
  87.     {
  88.         Friend[] arrFriend;
  89.         Employee[] arrEmployee;
  90.         Competitor[] arrCompetitor;
  91.  
  92.         public AddressBook()
  93.         {
  94.             arrFriend = new Friend[0];
  95.             arrEmployee = new Employee[0];
  96.             arrCompetitor = new Competitor[0];
  97.         }
  98.  
  99.         public void AddSubscriber(object obj, string ch)
  100.         {
  101.             switch (ch)
  102.             {
  103.                 case "друг":
  104.                     Friend[] newarrF = new Friend[arrFriend.Length + 1];
  105.                     for (int i = 0; i < arrFriend.Length; i++)
  106.                     {
  107.                         newarrF[i] = arrFriend[i];
  108.                     }
  109.                     newarrF[arrFriend.Length] = (Friend)obj;
  110.                     arrFriend = newarrF;
  111.                     break;
  112.                 case "сотрудник":
  113.                     Employee[] newarrE = new Employee[arrEmployee.Length + 1];
  114.                     for (int i = 0; i < arrEmployee.Length; i++)
  115.                     {
  116.                         newarrE[i] = arrEmployee[i];
  117.                     }
  118.                     newarrE[arrEmployee.Length] = (Employee)obj;
  119.                     arrEmployee = newarrE;
  120.                     break;
  121.                 case "конкурент":
  122.                     Competitor[] newarrC = new Competitor[arrCompetitor.Length + 1];
  123.                     for (int i = 0; i < arrCompetitor.Length; i++)
  124.                     {
  125.                         newarrC[i] = arrCompetitor[i];
  126.                     }
  127.                     newarrC[arrCompetitor.Length] = (Competitor)obj;
  128.                     arrCompetitor = newarrC;
  129.                     break;
  130.             }
  131.  
  132.         }
  133.         public void PrintBook()
  134.         {
  135.             Console.WriteLine("\nДрузья:\n");
  136.             foreach (Friend a in arrFriend)
  137.             {
  138.                 a.Print();
  139.             }
  140.             Console.WriteLine("\nСотрудники:\n");
  141.             foreach (Employee a in arrEmployee)
  142.             {              
  143.                 a.Print();
  144.             }
  145.             Console.WriteLine("\nКонкуренты:\n");
  146.             foreach (Competitor a in arrCompetitor)
  147.             {
  148.                 a.Print();
  149.             }
  150.         }
  151.  
  152.         class Program
  153.         {
  154.             static readonly string arrow = "=> ";
  155.             static readonly string[] menu = { "Добавить абонента", "Просмотреть книгу", "Выйти из приложения" };
  156.             static readonly string[] submenu = { "Друг", "Сотрудник", "Конкурент", "Возврат в предыдущее меню" };
  157.             static void PrintMenu(string[] arr, int choice)
  158.             {
  159.                 Console.Clear();
  160.                 for (int i = 0; i < arr.Length; i++)
  161.                 {
  162.                     if (i == choice)
  163.                     {
  164.                         Console.Write(arrow);
  165.                         Console.WriteLine(arr[i]);
  166.                     }
  167.                     else
  168.                     {
  169.                         Console.Write("   ");
  170.                         Console.WriteLine(arr[i]);
  171.                     }
  172.                 }
  173.             }
  174.             static void SelectFunc(AddressBook a, int choice)
  175.             {
  176.                 Console.CursorVisible = false;
  177.                 int ch = 0;
  178.                 switch (choice)
  179.                 {
  180.                     case 0:
  181.                         while (true)
  182.                         {
  183.                             PrintMenu(submenu, ch);
  184.                             switch (Console.ReadKey(true).Key)
  185.                             {
  186.                                 case ConsoleKey.UpArrow:
  187.                                     if (ch != 0)
  188.                                     {
  189.                                         ch--;
  190.                                     }
  191.                                     break;
  192.                                 case ConsoleKey.DownArrow:
  193.                                     if (ch != submenu.Length - 1)
  194.                                     {
  195.                                         ch++;
  196.                                     }
  197.                                     break;
  198.                                 case ConsoleKey.Enter:
  199.                                     Console.CursorVisible = true;
  200.                                     switch (ch)
  201.                                     {
  202.                                         case 0:
  203.                                             Console.WriteLine("Заполните следующие поля: ФИО, телефон");
  204.                                             Friend f = new Friend(Console.ReadLine(), Console.ReadLine());
  205.                                             a.AddSubscriber(f, "друг");
  206.                                             break;
  207.                                         case 1:
  208.                                             Console.WriteLine("Заполните следующие поля: ФИО, телефон, рабочий телефон, должность");
  209.                                             Employee e = new Employee(Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine());
  210.                                             a.AddSubscriber(e, "сотрудник");
  211.                                             break;
  212.                                         case 2:
  213.                                             Console.WriteLine("Заполните следующие поля: ФИО, телефон, рабочий телефон, должность, домашний телефон, домашний адрес");
  214.                                             Competitor c = new Competitor(Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine());
  215.                                             a.AddSubscriber(c, "конкурент");
  216.                                             break;
  217.                                         case 3:
  218.                                             return;
  219.                                     }
  220.                                     break;
  221.                             }
  222.                         }
  223.                         break;
  224.                     case 1:
  225.                         a.PrintBook();
  226.                         Console.ReadKey();
  227.                         break;
  228.                     case 2:
  229.                         System.Environment.Exit(0);
  230.                         break;
  231.                 }
  232.             }
  233.  
  234.             static void Main(string[] args)
  235.             {
  236.                 AddressBook a = new AddressBook();
  237.                 Console.CursorVisible = false;
  238.                 int choice = 0;
  239.  
  240.                 while (true)
  241.                 {
  242.                     PrintMenu(menu, choice);
  243.                     switch (Console.ReadKey(true).Key)
  244.                     {
  245.                         case ConsoleKey.UpArrow:
  246.                             if (choice != 0)
  247.                             {
  248.                                 choice--;
  249.                             }
  250.                             break;
  251.                         case ConsoleKey.DownArrow:
  252.                             if (choice != menu.Length - 1)
  253.                             {
  254.                                 choice++;
  255.                             }
  256.                             break;
  257.                         case ConsoleKey.Enter:
  258.                             Console.CursorVisible = true;
  259.                             SelectFunc(a, choice);
  260.                             break;
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement