Advertisement
AnyaAS

AddressBook(with Menu)

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