Advertisement
Guest User

Untitled

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