Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace OOPv2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string command = "";
- while (command != "exit")
- {
- command = Console.ReadLine();
- switch (command)
- {
- case "AddUser":
- Console.WriteLine("Добавление пользователя. Введите имя:");
- var n = Console.ReadLine();
- Console.WriteLine("Введите пароль:");
- var p = Console.ReadLine();
- DB.AddUser(new User(n, p));
- Console.WriteLine("Пользователь сохранен.");
- break;
- case "ListUser":
- Console.WriteLine(" Список пользователей программы:");
- DB.ListUser();
- break;
- case "Login":
- Console.WriteLine("Авторизация: \n Введите логин:");
- var l = Console.ReadLine();
- if (DB.Login(l))
- {
- Console.WriteLine("Введите пароль:");
- var s = Console.ReadLine();
- if (DB.CurrentUser.Checkpassword(s))
- {
- Console.WriteLine("Вы авторизованы!");
- }
- else
- {
- Console.WriteLine("Неверный пароль");
- }
- }
- else
- {
- Console.WriteLine("Такого пользователя не существует");
- }
- break;
- }
- if (DB.CurrentUser != null)
- {
- switch (command)
- {
- case "AddBook":
- Console.WriteLine("Добавление книги. \n Введите название книги:");
- DB.AddBook(new Book(Console.ReadLine()));
- Console.WriteLine("Книга добавлена");
- break;
- case "ListBook":
- Console.WriteLine("Список книг для добавления заметок:");
- DB.ListBook();
- break;
- case "ChooseBook":
- Console.WriteLine("Введите название книги для добавления заметок");
- var cb = Console.ReadLine();
- if (DB.ChooseBook(cb))
- {
- Console.WriteLine("Книга {0} выбрана!", cb);
- }
- else
- {
- Console.WriteLine("Такой книги не существует");
- }
- break;
- case "BookID":
- Console.WriteLine("Список ID всех книг:");
- DB.BookID();
- break;
- }
- if (DB.CurrentBook != null)
- {
- switch (command)
- {
- case "AddNote":
- Console.WriteLine("Введите текст заметки:");
- var nt = Console.ReadLine();
- DB.CurrentBook.AddNote(new Note(nt));
- Console.WriteLine("Заметка сохранена в книге {0}", DB.CurrentBook.NameBook);
- break;
- case "ShowAllNotes":
- Console.WriteLine("Список всех заметок в книге:");
- DB.CurrentBook.ShowAllNotes();
- break;
- }
- }
- }
- }
- }
- }
- class Note
- {
- public string Message
- {
- get;
- private set;
- }
- public Note(string message)
- {
- Message = message;
- }
- }
- class Book
- {
- private Note[] _notes = new Note[0];
- public string NameBook;
- static int nextId;
- public int BookId { get; private set; }
- public Book(string name)
- {
- NameBook = name;
- BookId = Interlocked.Increment(ref nextId);
- }
- public void AddNote(Note note)
- {
- Note[] notes = new Note[_notes.Length + 1];
- for (int i = 0; i < _notes.Length; i++)
- {
- notes[i] = _notes[i];
- }
- _notes = notes;
- _notes[_notes.Length - 1] = note;
- GC.Collect();
- }
- public void ShowAllNotes()
- {
- for (int i = 0; i < _notes.Length; i++)
- {
- Console.WriteLine(_notes[i].Message);
- }
- }
- }
- class User
- {
- public string Name { get; private set; }
- private string Password;
- public bool isLogin { get; private set; }
- public User(string name, string password, bool isLogin = false)
- {
- Name = name;
- Password = password;
- }
- public bool Checkpassword(string password)
- {
- return Password == password;
- }
- }
- class DB
- {
- private static User[] Users = new User[0];
- public static User CurrentUser;
- private static Book[] Books = new Book[0];
- public static Book CurrentBook;
- public static void AddUser(User user)
- {
- User[] tempArr = new User[Users.Length + 1];
- for (int j = 0; j < Users.Length; j++)
- {
- tempArr[j] = Users[j];
- }
- Users = tempArr;
- Users[Users.Length - 1] = user;
- }
- public static void ListUser()
- {
- for (int i = 0; i < Users.Length; i++)
- {
- Console.WriteLine(Users[i].Name);
- }
- }
- public static bool Login(string name)
- {
- foreach (var u in Users)
- {
- if (u.Name == name)
- {
- CurrentUser = u;
- return true;
- }
- }
- return false;
- }
- public static void AddBook(Book book)
- {
- Book[] tempArr = new Book[Books.Length + 1];
- for (int j = 0; j < Books.Length; j++)
- {
- tempArr[j] = Books[j];
- }
- Books = tempArr;
- Books[Books.Length - 1] = book;
- }
- public static void ListBook()
- {
- for (int i = 0; i < Books.Length; i++)
- {
- Console.WriteLine(Books[i].NameBook);
- }
- }
- public static bool ChooseBook(string name)
- {
- foreach (var u in Books)
- {
- if (u.NameBook == name)
- {
- CurrentBook = u;
- return true;
- }
- }
- return false;
- }
- public static void BookID()
- {
- for (int i = 0; i < Books.Length; i++)
- {
- Console.WriteLine(Books[i].BookId);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement