Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _1422
- {
- class Program
- {
- public static void CreateBooks()
- {
- for (int i = 0; i < 2; i++)
- {
- new Book();
- }
- }
- static void Main(string[] args)
- {
- User u = new User("Aly"); //указать параметры которые вы указали в объявление конструктора
- u = null;
- GC.Collect();
- /*Console.WriteLine("Добро пожаловать!");
- string command;
- Program.CreateBooks();
- while (true)
- {
- command = Console.ReadLine();
- switch (command.ToLower())
- {
- case "list":
- {
- User.List();
- break;
- }
- case "adduser":
- {
- Console.WriteLine("Введите имя нового пользователя");
- string name;
- while (true)
- {
- name = Console.ReadLine();
- if (!User.exists(name))
- {
- new User(name);
- Console.WriteLine("Пользователь успешно добавлен");
- break;
- }
- else
- {
- Console.WriteLine("Пользователь уже существует! Введите новое имя пользователя");
- }
- }
- break;
- }
- case "login":
- {
- Console.WriteLine("Введите имя пользователя");
- string name;
- while (true)
- {
- name = Console.ReadLine();
- if (User.exists(name))
- {
- User.Login(name);
- Console.WriteLine("Вы успешно зашли под своим именем, {0}!", User.CurrentUser.Name);
- while (true)
- {
- command = Console.ReadLine();
- switch (command.ToLower())
- {
- case "books":
- {
- Book.GetBooksIDs();
- break;
- }
- case "showallnotes":
- {
- Console.WriteLine("Введите ID книги для просмотра записей");
- int ID;
- while (true)
- {
- if (int.TryParse(Console.ReadLine(), out ID))
- {
- if (Book.exists(ID))
- {
- Book.ShowAllNotes(ID);
- break;
- }
- else
- {
- Console.WriteLine("Книга с данным ID не существует. Введите другой ID");
- }
- }
- else
- {
- Console.WriteLine("ID введен некорректно. Введите корректный ID");
- }
- }
- break;
- }
- case "addnote":
- {
- Console.WriteLine("Введите ID книги для записи");
- int ID;
- while (true)
- {
- if (int.TryParse(Console.ReadLine(), out ID))
- {
- if (Book.exists(ID))
- {
- Console.WriteLine("Введите текст заметки");
- Book.AddNote(ID, Console.ReadLine());
- Console.WriteLine("Заметка успешно добавлена");
- break;
- }
- else
- {
- Console.WriteLine("Книга с данным ID не существует. Введите другой ID");
- }
- }
- else
- {
- Console.WriteLine("ID введен некорректно. Введите корректный ID");
- }
- }
- break;
- }
- case "exit":
- Console.WriteLine("До скорой встречи, {0}!", User.CurrentUser.Name);
- break;
- default:
- Console.WriteLine("Неверная команда");
- break;
- }
- if (command.Equals("exit"))
- {
- break;
- }
- }
- break;
- }
- else
- {
- Console.WriteLine("Пользователь не найден! Создайте новое имя пользователя используя команду AddUser");
- break;
- }
- }
- break;
- }
- default:
- Console.WriteLine("Неизвестная команда");
- break;
- }
- }*/
- }
- }
- class Book
- {
- private static List<Book> Books = new List<Book>();
- private List<Note> Notes;
- public int ID { get; }
- public Book()
- {
- Books.Add(this);
- ID = Books.Count - 1;
- Notes = new List<Note>();
- }
- public Book(params Note[] notes)
- {
- Books.Add(this);
- ID = Books.Count - 1;
- Notes = new List<Note>();
- foreach (Note note in notes)
- {
- Notes.Add(note);
- }
- }
- public static bool exists(int ID)
- {
- foreach (Book book in Books)
- {
- if (book.ID == ID)
- {
- return true;
- }
- }
- return false;
- }
- public static void GetBooksIDs()
- {
- Console.WriteLine("Список ID всех книг:");
- foreach (Book book in Books)
- {
- Console.WriteLine(book.ID);
- }
- }
- public static void ShowAllNotes(int ID)
- {
- if (Books[ID].Notes.Count == 0)
- {
- Console.WriteLine("В книге нет записей!");
- }
- else
- {
- Console.WriteLine("Список всех записей книги с ID = " + ID);
- foreach (Note note in Books[ID].Notes)
- {
- Console.WriteLine(note.Text);
- }
- }
- }
- public static void AddNote(int ID, string text)
- {
- Books[ID].Notes.Add(new Note(text));
- }
- }
- class Note
- {
- public string Text { get; set; }
- public Note(string text)
- {
- Text = text;
- }
- }
- class User
- {
- private static List<User> Users = new List<User>();
- public int ID { get; }
- public string Name { get; }
- public static User CurrentUser { get; set; }
- public User(string name = "petya")
- {
- Users.Add(this);
- ID = Users.Count - 1;
- Name = name;
- }
- public static bool exists(string name)
- {
- foreach (User user in Users)
- {
- if (user.Name == name)
- {
- return true;
- }
- }
- return false;
- }
- public static void Login(string name)
- {
- foreach (User user in Users)
- {
- if (user.Name == name)
- {
- CurrentUser = user;
- return;
- }
- }
- }
- public static void List()
- {
- if (Users.Count == 0)
- {
- Console.WriteLine("Список пользователей пуст! Создайте нового пользователя командой AddUser");
- return;
- }
- else
- {
- Console.WriteLine("Список всех пользователей:");
- foreach (User user in Users)
- {
- Console.WriteLine(user.Name);
- }
- }
- }
- ~User()
- {
- Console.WriteLine("{0} удален!", this.Name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment