Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Library_with_books;
- class Program
- {
- static void Main(string[] args)
- {
- Library library = new Library();
- library.Work();
- }
- class Library
- {
- private List<Book> _library = new List<Book>();
- public void Work()
- {
- const string AddBookCommand = "1";
- const string FindBookCommand = "2";
- const string DeleteBookCommand = "3";
- const string ShowAllBooksCommand = "4";
- const string ExitCommand = "5";
- Library library = new Library();
- bool isWorking = true;
- Console.WriteLine("Добавте первую книгу в библиотеку. ");
- library.AddBook();
- Console.Clear();
- while (isWorking)
- {
- Console.WriteLine($"Хранилище книг\n\n\n" +
- $"Введите {AddBookCommand} если хотите добавить книгу.\n" +
- $"Введите {FindBookCommand} если хотите найти книгу.\n" +
- $"Введите {DeleteBookCommand} если хотите удалить книгу.\n" +
- $"Введите {ShowAllBooksCommand} если хотите увидеть список книг.\n" +
- $"Введите {ExitCommand} если хотите выйти из программы.\n\n" +
- $"Ввод: ");
- string? userInput = Console.ReadLine();
- switch (userInput)
- {
- case AddBookCommand:
- library.AddBook();
- break;
- case FindBookCommand:
- library.FindBook();
- break;
- case ShowAllBooksCommand:
- library.ShowAllBooks();
- break;
- case DeleteBookCommand:
- library.DeleteBook();
- break;
- case ExitCommand:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Введена неверная команда.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void AddBook()
- {
- Book newBook = new Book();
- Console.WriteLine("Введите название книги: ");
- newBook.AddBookTitle();
- Console.WriteLine("Введите имя автора: ");
- newBook.AddAuthorName();
- Console.WriteLine("Введите год издания: ");
- newBook.AddYearOfPublishing();
- _library.Add(newBook);
- }
- private void ShowAllBooks()
- {
- int numberOfbook = 1;
- if (_library.Count == 0)
- {
- Console.WriteLine("Список пуст.");
- }
- foreach (var book in _library)
- {
- Console.WriteLine($"{numberOfbook} {book.BookTitle} {book.AuthorName} {book.YearOfPublishing}");
- numberOfbook++;
- }
- }
- private void FindAuthor()
- {
- List<Book> libraryOfFindBooks = new List<Book>();
- Console.WriteLine("Введите имя автора для поиска: ");
- string userInput = Console.ReadLine();
- libraryOfFindBooks = _library.FindAll(item => item.AuthorName == userInput);
- Console.WriteLine("Список найденых книг:");
- foreach (var book in libraryOfFindBooks)
- {
- Console.WriteLine($"{book.BookTitle} {book.AuthorName} {book.YearOfPublishing}");
- }
- }
- private void FindNameOfBook()
- {
- List<Book> libraryOfFindBooks = new List<Book>();
- Console.WriteLine("Введите название книги для поиска: ");
- string userInput = Console.ReadLine();
- libraryOfFindBooks.AddRange(_library.FindAll(item => item.BookTitle == userInput));
- Console.WriteLine("Список найденых книг:");
- foreach (var book in libraryOfFindBooks)
- {
- Console.WriteLine($"{book.BookTitle} {book.AuthorName} {book.YearOfPublishing}");
- }
- }
- private void FindBook()
- {
- Book newBook = new Book();
- string resultOfSelect = newBook.SelectNameOrTitle();
- if (resultOfSelect == "name")
- {
- FindAuthor();
- }
- else
- {
- FindNameOfBook();
- }
- }
- private void DeleteBook()
- {
- Book newBook = new Book();
- bool isWorking = true;
- while (isWorking)
- {
- if (_library.Count == 0)
- {
- Console.WriteLine("Список пуст.");
- isWorking = false;
- }
- Console.Write("Введите номер книги для удаления: ");
- int indexOfBook = newBook.CheckNumber() - 1;
- if (indexOfBook >= _library.Count || indexOfBook < 0)
- {
- Console.WriteLine("Номер книги отсутствует. Попробуйте снова");
- }
- else
- {
- _library.RemoveAt(indexOfBook);
- isWorking = false;
- }
- }
- }
- }
- class Book
- {
- public Book(string bookTitle = "", string authorName = "", int yearOfPublishing = 0)
- {
- BookTitle = bookTitle;
- AuthorName = authorName;
- YearOfPublishing = yearOfPublishing;
- }
- public string BookTitle { get; private set; }
- public string AuthorName { get; private set; }
- public int YearOfPublishing { get; private set; }
- public void AddAuthorName()
- {
- bool isWork = true;
- string userInput;
- while (isWork)
- {
- userInput = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(userInput))
- {
- Console.WriteLine("Пустая строка. Введите имя авторав.");
- Console.ReadKey();
- Console.Clear();
- }
- else
- {
- AuthorName = userInput;
- isWork = false;
- }
- }
- }
- public void AddBookTitle()
- {
- bool isWork = true;
- string userInput;
- while (isWork)
- {
- userInput = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(userInput))
- {
- Console.WriteLine("Пустая строка. Введите имя название книги.");
- Console.ReadKey();
- Console.Clear();
- }
- else
- {
- BookTitle = userInput;
- isWork = false;
- }
- }
- }
- public void AddYearOfPublishing()
- {
- YearOfPublishing = CheckNumber();
- }
- public int CheckNumber()
- {
- bool isWorking = true;
- int number = 0;
- while (isWorking)
- {
- bool isSucces = int.TryParse(Console.ReadLine(), out int result);
- if (isSucces == false)
- {
- Console.WriteLine("Нужно ввести число.");
- Console.ReadKey();
- }
- else
- {
- number = result;
- isWorking = false;
- }
- }
- return number;
- }
- public string SelectNameOrTitle()
- {
- const string SelectNameCommand = "1";
- const string SelectTitleCommand = "2";
- string result = "nothing";
- string name = "name";
- string title = "title";
- string? userInput;
- while (result == "nothing")
- {
- Console.WriteLine($"Введите: \n{SelectNameCommand} для поиска по имени автора" +
- $"\n{SelectTitleCommand} для поиска по названию книги\nВвод: ");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case SelectTitleCommand:
- result = title;
- break;
- case SelectNameCommand:
- result = name;
- break;
- default:
- Console.WriteLine("Введена неверная команда.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement