Advertisement
Guest User

Untitled

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