Advertisement
Guest User

Untitled

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