Advertisement
Guest User

Untitled

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