Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.36 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace OOPv2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string command = "";
  11.  
  12.             while (command != "exit")
  13.             {
  14.                 command = Console.ReadLine();
  15.  
  16.                 switch (command)
  17.                 {
  18.                     case "AddUser":
  19.                         Console.WriteLine("Добавление пользователя. Введите имя:");
  20.                         var n = Console.ReadLine();
  21.                         Console.WriteLine("Введите пароль:");
  22.                         var p = Console.ReadLine();
  23.                         DB.AddUser(new User(n, p));
  24.                         Console.WriteLine("Пользователь сохранен.");
  25.                         break;
  26.                     case "ListUser":
  27.                         Console.WriteLine(" Список пользователей программы:");
  28.                         DB.ListUser();
  29.                         break;
  30.                     case "Login":
  31.                         Console.WriteLine("Авторизация: \n Введите логин:");
  32.                         var l = Console.ReadLine();
  33.                         if (DB.Login(l))
  34.                         {
  35.                             Console.WriteLine("Введите пароль:");
  36.                             var s = Console.ReadLine();
  37.                             if (DB.CurrentUser.Checkpassword(s))
  38.                             {
  39.                                 Console.WriteLine("Вы авторизованы!");
  40.                             }
  41.                             else
  42.                             {
  43.                                 Console.WriteLine("Неверный пароль");
  44.                             }
  45.                         }
  46.                         else
  47.                         {
  48.                             Console.WriteLine("Такого пользователя не существует");
  49.                         }
  50.                         break;
  51.                 }
  52.                 if (DB.CurrentUser != null)
  53.                 {
  54.                     switch (command)
  55.                     {
  56.                         case "AddBook":
  57.                             Console.WriteLine("Добавление книги. \n Введите название книги:");
  58.                             DB.AddBook(new Book(Console.ReadLine()));
  59.                             Console.WriteLine("Книга добавлена");
  60.                             break;
  61.                         case "ListBook":
  62.                             Console.WriteLine("Список книг для добавления заметок:");
  63.                             DB.ListBook();
  64.                             break;
  65.                         case "ChooseBook":
  66.                             Console.WriteLine("Введите название книги для добавления заметок");
  67.                             var cb = Console.ReadLine();
  68.                             if (DB.ChooseBook(cb))
  69.                             {
  70.                                 Console.WriteLine("Книга {0} выбрана!", cb);
  71.                             }
  72.                             else
  73.                             {
  74.                                 Console.WriteLine("Такой книги не существует");
  75.                             }
  76.                             break;
  77.                         case "BookID":
  78.                             Console.WriteLine("Список ID всех книг:");
  79.                             DB.BookID();
  80.                             break;
  81.                     }
  82.                     if (DB.CurrentBook != null)
  83.                     {
  84.                         switch (command)
  85.                         {
  86.                             case "AddNote":
  87.                                 Console.WriteLine("Введите текст заметки:");
  88.                                 var nt = Console.ReadLine();
  89.                                 DB.CurrentBook.AddNote(new Note(nt));
  90.                                 Console.WriteLine("Заметка сохранена в книге {0}", DB.CurrentBook.NameBook);
  91.                                 break;
  92.                             case "ShowAllNotes":
  93.                                 Console.WriteLine("Список всех заметок в книге:");
  94.                                 DB.CurrentBook.ShowAllNotes();
  95.                                 break;
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     class Note
  104.     {
  105.         public string Message
  106.         {
  107.             get;
  108.             private set;
  109.         }
  110.         public Note(string message)
  111.         {
  112.             Message = message;
  113.             DateTime f = DateTime.Now;
  114.         }
  115.  
  116.         public Note()
  117.         {
  118.  
  119.         }
  120.  
  121.         ~Note()
  122.         {
  123.             Console.WriteLine("Объект Note уничтожен");
  124.         }
  125.  
  126.     }
  127.  
  128.     class Book
  129.     {
  130.         private Note[] _notes = new Note[0];
  131.         public string NameBook;
  132.         public string Edition;
  133.         static int Count;
  134.         public int BookId
  135.         {
  136.             get;
  137.             private set;
  138.         }
  139.  
  140.         public Book(string name)
  141.         {
  142.             NameBook = name;
  143.             BookId = ++Count;
  144.         }
  145.  
  146.         public Book(string name, string edition)
  147.         {
  148.             NameBook = name;
  149.             BookId = ++Count;
  150.             Edition = edition;
  151.         }
  152.  
  153.         ~Book()
  154.         {
  155.             Console.WriteLine("Объект Book уничтожен");
  156.         }
  157.  
  158.         public void AddNote(Note note)
  159.         {
  160.             Note[] notes = new Note[_notes.Length + 1];
  161.             for (int i = 0; i < _notes.Length; i++)
  162.             {
  163.                 notes[i] = _notes[i];
  164.             }
  165.             _notes = notes;
  166.             _notes[_notes.Length - 1] = note;
  167.             GC.Collect();
  168.         }
  169.  
  170.         public void ShowAllNotes()
  171.         {
  172.             for (int i = 0; i < _notes.Length; i++)
  173.             {
  174.                 Console.WriteLine(_notes[i].Message);
  175.             }
  176.         }
  177.     }
  178.  
  179.     class User
  180.     {
  181.         public string Name
  182.         {
  183.             get;
  184.             private set;
  185.         }
  186.         public string Position
  187.         {
  188.             get;
  189.             private set;
  190.         }
  191.         private string Password;
  192.         public bool isLogin
  193.         {
  194.             get;
  195.             private set;
  196.         }
  197.         static int Count = 0;
  198.         public int id;
  199.  
  200.  
  201.         public User()
  202.         {
  203.             Count++;
  204.         }
  205.  
  206.         public User(string name, string password, bool isLogin = false)
  207.             :this()
  208.         {
  209.             Name = name;
  210.             Password = password;
  211.             id = Count;
  212.         }
  213.  
  214.         public User(string name, string password, string position, bool isLogin = false)
  215.         {
  216.             Name = name;
  217.             Position = position;
  218.             Password = password;
  219.         }
  220.  
  221.         ~User()
  222.         {
  223.             Console.WriteLine("Объект User уничтожен");
  224.         }
  225.  
  226.         public bool Checkpassword(string password)
  227.         {
  228.             return Password == password;
  229.         }
  230.  
  231.  
  232.     }
  233.  
  234.     class DB
  235.     {
  236.         private static User[] Users = new User[0];
  237.         public static User CurrentUser;
  238.         private static Book[] Books = new Book[0];
  239.         public static Book CurrentBook;
  240.  
  241.         public static void AddUser(User user)
  242.         {
  243.             User[] tempArr = new User[Users.Length + 1];
  244.             for (int j = 0; j < Users.Length; j++)
  245.             {
  246.                 tempArr[j] = Users[j];
  247.             }
  248.             Users = tempArr;
  249.             Users[Users.Length - 1] = user;
  250.         }
  251.  
  252.         public static void ListUser()
  253.         {
  254.             for (int i = 0; i < Users.Length; i++)
  255.             {
  256.                 Console.WriteLine(Users[i].Name + "  "+ Users[i].id);
  257.             }
  258.         }
  259.  
  260.         public static bool Login(string name)
  261.         {
  262.             foreach (var u in Users)
  263.             {
  264.                 if (u.Name == name)
  265.                 {
  266.                     CurrentUser = u;
  267.                     return true;
  268.  
  269.                 }
  270.             }
  271.             return false;
  272.         }
  273.  
  274.         public static void AddBook(Book book)
  275.         {
  276.             Book[] tempArr = new Book[Books.Length + 1];
  277.             for (int j = 0; j < Books.Length; j++)
  278.             {
  279.                 tempArr[j] = Books[j];
  280.             }
  281.             Books = tempArr;
  282.             Books[Books.Length - 1] = book;
  283.         }
  284.  
  285.         public static void ListBook()
  286.         {
  287.             for (int i = 0; i < Books.Length; i++)
  288.             {
  289.                 Console.WriteLine(Books[i].NameBook);
  290.             }
  291.         }
  292.  
  293.         public static bool ChooseBook(string name)
  294.         {
  295.             foreach (var u in Books)
  296.             {
  297.                 if (u.NameBook == name)
  298.                 {
  299.                     CurrentBook = u;
  300.                     return true;
  301.                 }
  302.             }
  303.             return false;
  304.         }
  305.  
  306.         public static void BookID()
  307.         {
  308.             for (int i = 0; i < Books.Length; i++)
  309.             {
  310.                 Console.WriteLine(Books[i].BookId);
  311.             }
  312.         }
  313.     }
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement