Advertisement
Guest User

Untitled

a guest
May 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.17 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.  
  111.         public Note(string message)
  112.         {
  113.             Message = message;
  114.         }
  115.  
  116.     }
  117.  
  118.     class Book
  119.     {
  120.         private Note[] _notes = new Note[0];
  121.         public string NameBook;
  122.         static int nextId;
  123.         public int BookId { get; private set; }
  124.         public Book(string name)
  125.         {
  126.             NameBook = name;
  127.             BookId = Interlocked.Increment(ref nextId);
  128.         }
  129.        
  130.         public void AddNote(Note note)
  131.         {
  132.             Note[] notes = new Note[_notes.Length + 1];
  133.             for (int i = 0; i < _notes.Length; i++)
  134.             {
  135.                 notes[i] = _notes[i];
  136.             }
  137.             _notes = notes;
  138.             _notes[_notes.Length - 1] = note;
  139.             GC.Collect();
  140.         }
  141.  
  142.         public void ShowAllNotes()
  143.         {
  144.             for (int i = 0; i < _notes.Length; i++)
  145.             {
  146.                 Console.WriteLine(_notes[i].Message);
  147.             }
  148.         }
  149.     }
  150.  
  151.     class User
  152.     {
  153.         public string Name { get; private set; }
  154.         private string Password;
  155.         public bool isLogin { get; private set; }
  156.  
  157.         public User(string name, string password, bool isLogin = false)
  158.         {
  159.             Name = name;
  160.             Password = password;
  161.         }
  162.        
  163.         public bool Checkpassword(string password)
  164.         {
  165.             return Password == password;
  166.         }
  167.     }
  168.  
  169.     class DB
  170.     {
  171.         private static User[] Users = new User[0];
  172.         public static User CurrentUser;
  173.         private static Book[] Books = new Book[0];
  174.         public static Book CurrentBook;
  175.  
  176.         public static void AddUser(User user)
  177.         {
  178.             User[] tempArr = new User[Users.Length + 1];
  179.             for (int j = 0; j < Users.Length; j++)
  180.             {
  181.                 tempArr[j] = Users[j];
  182.             }
  183.             Users = tempArr;
  184.             Users[Users.Length - 1] = user;
  185.         }
  186.  
  187.         public static void ListUser()
  188.         {
  189.             for (int i = 0; i < Users.Length; i++)
  190.             {
  191.                 Console.WriteLine(Users[i].Name);
  192.             }
  193.         }
  194.  
  195.         public static bool Login(string name)
  196.         {
  197.             foreach (var u in Users)
  198.             {
  199.                 if (u.Name == name)
  200.                 {
  201.                     CurrentUser = u;
  202.                     return true;
  203.  
  204.                 }
  205.             }
  206.             return false;
  207.         }
  208.  
  209.         public static void AddBook(Book book)
  210.         {
  211.             Book[] tempArr = new Book[Books.Length + 1];
  212.             for (int j = 0; j < Books.Length; j++)
  213.             {
  214.                 tempArr[j] = Books[j];
  215.             }
  216.             Books = tempArr;
  217.             Books[Books.Length - 1] = book;
  218.         }
  219.  
  220.         public static void ListBook()
  221.         {
  222.             for (int i = 0; i < Books.Length; i++)
  223.             {
  224.                 Console.WriteLine(Books[i].NameBook);
  225.             }
  226.         }
  227.  
  228.         public static bool ChooseBook(string name)
  229.         {
  230.             foreach (var u in Books)
  231.             {
  232.                 if (u.NameBook == name)
  233.                 {
  234.                     CurrentBook = u;
  235.                     return true;
  236.                 }
  237.             }
  238.             return false;
  239.         }
  240.  
  241.         public static void BookID()
  242.         {
  243.             for (int i = 0; i < Books.Length; i++)
  244.             {
  245.                 Console.WriteLine(Books[i].BookId);
  246.             }
  247.         }
  248.     }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement