Nemo048

Untitled

Jun 18th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _1422
  8. {
  9.     class Program
  10.     {
  11.         public static void CreateBooks()
  12.         {
  13.             for (int i = 0; i < 2; i++)
  14.             {
  15.                 new Book();
  16.             }
  17.         }
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             User u = new User("Aly"); //указать параметры которые вы указали в объявление конструктора
  22.             u = null;
  23.             GC.Collect();
  24.  
  25.             /*Console.WriteLine("Добро пожаловать!");
  26.             string command;
  27.             Program.CreateBooks();
  28.  
  29.             while (true)
  30.             {
  31.                 command = Console.ReadLine();
  32.  
  33.                 switch (command.ToLower())
  34.                 {
  35.                     case "list":
  36.                         {
  37.                             User.List();
  38.                             break;
  39.                         }
  40.                     case "adduser":
  41.                         {
  42.                             Console.WriteLine("Введите имя нового пользователя");
  43.                             string name;
  44.                             while (true)
  45.                             {
  46.                                 name = Console.ReadLine();
  47.                                 if (!User.exists(name))
  48.                                 {
  49.                                     new User(name);
  50.                                     Console.WriteLine("Пользователь успешно добавлен");
  51.                                     break;
  52.                                 }
  53.                                 else
  54.                                 {
  55.                                     Console.WriteLine("Пользователь уже существует! Введите новое имя пользователя");
  56.                                 }
  57.                             }
  58.                             break;
  59.                         }
  60.  
  61.                     case "login":
  62.                         {
  63.                             Console.WriteLine("Введите имя пользователя");
  64.                             string name;
  65.                             while (true)
  66.                             {
  67.                                 name = Console.ReadLine();
  68.                                 if (User.exists(name))
  69.                                 {
  70.                                     User.Login(name);
  71.                                     Console.WriteLine("Вы успешно зашли под своим именем, {0}!", User.CurrentUser.Name);
  72.  
  73.                                     while (true)
  74.                                     {
  75.                                         command = Console.ReadLine();
  76.                                         switch (command.ToLower())
  77.                                         {
  78.                                             case "books":
  79.                                                 {
  80.                                                     Book.GetBooksIDs();
  81.                                                     break;
  82.                                                 }
  83.                                             case "showallnotes":
  84.                                                 {
  85.                                                     Console.WriteLine("Введите ID книги для просмотра записей");
  86.                                                     int ID;
  87.                                                     while (true)
  88.                                                     {
  89.                                                         if (int.TryParse(Console.ReadLine(), out ID))
  90.                                                         {
  91.                                                             if (Book.exists(ID))
  92.                                                             {
  93.                                                                 Book.ShowAllNotes(ID);
  94.                                                                 break;
  95.                                                             }
  96.                                                             else
  97.                                                             {
  98.                                                                 Console.WriteLine("Книга с данным ID не существует. Введите другой ID");
  99.                                                             }
  100.                                                         }
  101.                                                         else
  102.                                                         {
  103.                                                             Console.WriteLine("ID введен некорректно. Введите корректный ID");
  104.                                                         }
  105.                                                     }
  106.                                                     break;
  107.                                                 }
  108.                                             case "addnote":
  109.                                                 {
  110.                                                     Console.WriteLine("Введите ID книги для записи");
  111.                                                     int ID;
  112.                                                     while (true)
  113.                                                     {
  114.                                                         if (int.TryParse(Console.ReadLine(), out ID))
  115.                                                         {
  116.                                                             if (Book.exists(ID))
  117.                                                             {
  118.                                                                 Console.WriteLine("Введите текст заметки");
  119.                                                                 Book.AddNote(ID, Console.ReadLine());
  120.                                                                 Console.WriteLine("Заметка успешно добавлена");
  121.                                                                 break;
  122.                                                             }
  123.                                                             else
  124.                                                             {
  125.                                                                 Console.WriteLine("Книга с данным ID не существует. Введите другой ID");
  126.                                                             }
  127.                                                         }
  128.                                                         else
  129.                                                         {
  130.                                                             Console.WriteLine("ID введен некорректно. Введите корректный ID");
  131.                                                         }
  132.                                                     }
  133.                                                     break;
  134.                                                 }
  135.                                             case "exit":
  136.                                                 Console.WriteLine("До скорой встречи, {0}!", User.CurrentUser.Name);
  137.                                                 break;
  138.                                             default:
  139.                                                 Console.WriteLine("Неверная команда");
  140.                                                 break;
  141.                                         }
  142.                                         if (command.Equals("exit"))
  143.                                         {
  144.                                             break;
  145.                                         }
  146.                                     }
  147.                                     break;
  148.                                 }
  149.                                 else
  150.                                 {
  151.                                     Console.WriteLine("Пользователь не найден! Создайте новое имя пользователя используя команду AddUser");
  152.                                     break;
  153.                                 }
  154.                             }
  155.                             break;
  156.                         }
  157.                     default:
  158.                         Console.WriteLine("Неизвестная команда");
  159.                         break;
  160.  
  161.                 }
  162.             }*/
  163.         }
  164.     }
  165.  
  166.     class Book
  167.     {
  168.         private static List<Book> Books = new List<Book>();
  169.         private List<Note> Notes;
  170.  
  171.         public int ID { get; }
  172.  
  173.         public Book()
  174.         {
  175.             Books.Add(this);
  176.             ID = Books.Count - 1;
  177.             Notes = new List<Note>();
  178.         }
  179.  
  180.         public Book(params Note[] notes)
  181.         {
  182.             Books.Add(this);
  183.             ID = Books.Count - 1;
  184.             Notes = new List<Note>();
  185.  
  186.             foreach (Note note in notes)
  187.             {
  188.                 Notes.Add(note);
  189.             }
  190.         }
  191.  
  192.         public static bool exists(int ID)
  193.         {
  194.             foreach (Book book in Books)
  195.             {
  196.                 if (book.ID == ID)
  197.                 {
  198.                     return true;
  199.                 }
  200.             }
  201.             return false;
  202.         }
  203.  
  204.         public static void GetBooksIDs()
  205.         {
  206.             Console.WriteLine("Список ID всех книг:");
  207.             foreach (Book book in Books)
  208.             {
  209.                 Console.WriteLine(book.ID);
  210.             }
  211.         }
  212.  
  213.         public static void ShowAllNotes(int ID)
  214.         {
  215.             if (Books[ID].Notes.Count == 0)
  216.             {
  217.                 Console.WriteLine("В книге нет записей!");
  218.             }
  219.             else
  220.             {
  221.                 Console.WriteLine("Список всех записей книги с ID = " + ID);
  222.                 foreach (Note note in Books[ID].Notes)
  223.                 {
  224.                     Console.WriteLine(note.Text);
  225.                 }
  226.             }
  227.         }
  228.  
  229.         public static void AddNote(int ID, string text)
  230.         {
  231.             Books[ID].Notes.Add(new Note(text));
  232.         }
  233.     }
  234.  
  235.     class Note
  236.     {
  237.         public string Text { get; set; }
  238.  
  239.         public Note(string text)
  240.         {
  241.             Text = text;
  242.         }
  243.     }
  244.  
  245.     class User
  246.     {
  247.         private static List<User> Users = new List<User>();
  248.  
  249.         public int ID { get; }
  250.         public string Name { get; }
  251.  
  252.         public static User CurrentUser { get; set; }
  253.  
  254.         public User(string name = "petya")
  255.         {
  256.             Users.Add(this);
  257.             ID = Users.Count - 1;
  258.             Name = name;
  259.         }
  260.  
  261.         public static bool exists(string name)
  262.         {
  263.             foreach (User user in Users)
  264.             {
  265.                 if (user.Name == name)
  266.                 {
  267.                     return true;
  268.                 }
  269.             }
  270.             return false;
  271.         }
  272.  
  273.         public static void Login(string name)
  274.         {
  275.             foreach (User user in Users)
  276.             {
  277.                 if (user.Name == name)
  278.                 {
  279.                     CurrentUser = user;
  280.                     return;
  281.                 }
  282.             }
  283.         }
  284.  
  285.         public static void List()
  286.         {
  287.             if (Users.Count == 0)
  288.             {
  289.                 Console.WriteLine("Список пользователей пуст! Создайте нового пользователя командой AddUser");
  290.                 return;
  291.             }
  292.             else
  293.             {
  294.                 Console.WriteLine("Список всех пользователей:");
  295.                 foreach (User user in Users)
  296.                 {
  297.                     Console.WriteLine(user.Name);
  298.                 }
  299.             }
  300.         }
  301.  
  302.         ~User()
  303.         {
  304.             Console.WriteLine("{0} удален!", this.Name);
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment