Advertisement
Guest User

Untitled

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