Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace books_storage
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Library library = new Library();
- library.Work();
- }
- }
- class Book
- {
- public Book(string name, string authorName, int yearOfPublishng)
- {
- Name = name;
- AuthorName = authorName;
- YearOfPublishng = yearOfPublishng;
- }
- public string Name { get; private set; }
- public string AuthorName { get; private set; }
- public int YearOfPublishng { get; private set; }
- }
- class Library
- {
- private List<Book> storageBooks = 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";
- bool isWorking = true;
- 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:
- AddBook();
- break;
- case FindBookCommand:
- ShowFindBooks();
- break;
- case ShowAllBooksCommand:
- ShowBooks(storageBooks);
- break;
- case DeleteBookCommand:
- DeleteBook();
- break;
- case ExitCommand:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Введена неверная команда.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void AddBook()
- {
- Console.Write("Введите название книги:");
- string name = Console.ReadLine();
- Console.Write("\nВведите имя автора:");
- string authorName = Console.ReadLine();
- Console.Write("\nВведите год книги:");
- int yearOfPublishng = ReadInt();
- Book newBook = new Book(name, authorName, yearOfPublishng);
- storageBooks.Add(newBook);
- }
- private void ShowBooks(List<Book> books)
- {
- int numberBook = 1;
- if (IsEmptyValue(storageBooks) == true)
- {
- return;
- }
- foreach (var book in books)
- {
- Console.WriteLine($"{numberBook}. {book.Name} - {book.AuthorName} - {book.YearOfPublishng}");
- numberBook++;
- }
- }
- private void ShowFindBooks()
- {
- List<Book> books = FindBook();
- ShowBooks(books);
- }
- private void DeleteBook()
- {
- List<Book> books = FindBook();
- for (int i = 0; i < storageBooks.Count; i++)
- {
- for (int j = 0; j < books.Count; j++)
- {
- if (storageBooks[i] == books[i])
- {
- storageBooks.RemoveAt(i);
- }
- }
- }
- }
- private string SelectSpecifitedParameter()
- {
- const int NameBookSelectCommand = 1;
- const int AuthorNameSelectCommand = 2;
- const int YearSelectCommand = 3;
- const string NameBookChoice = "nameBookSelect";
- const string AuthorNameChoice = "authorNameSelect";
- const string YearBookChoice = "yearSelect";
- string userChoice = "";
- Console.WriteLine($"Введите какой способ хотите выбрать:\n" +
- $"{NameBookSelectCommand} - если по названию книги.\n" +
- $"{AuthorNameSelectCommand} - есла по имени автора.\n" +
- $"{YearSelectCommand} - если по году издания.");
- switch (ReadInt())
- {
- case NameBookSelectCommand:
- userChoice = NameBookChoice;
- break;
- case AuthorNameSelectCommand:
- userChoice = AuthorNameChoice;
- break;
- case YearSelectCommand:
- userChoice = YearBookChoice;
- break;
- default:
- Console.WriteLine("Неизвестная команда.");
- break;
- }
- return userChoice;
- }
- private List<Book> FindBook()
- {
- const string NameBookChoice = "nameBookSelect";
- const string AuthorNameChoice = "authorNameSelect";
- const string YearBookChoice = "yearSelect";
- List<Book> books = new List<Book>();
- int numberUserInput;
- string userChoiceSelect;
- string userInput;
- if (IsEmptyValue(storageBooks) == true)
- {
- return null;
- }
- userChoiceSelect = SelectSpecifitedParameter();
- if (userChoiceSelect == YearBookChoice)
- {
- Console.WriteLine("Введите год книги: ");
- numberUserInput = ReadInt();
- foreach (var bookNeed in storageBooks)
- {
- if (bookNeed.YearOfPublishng == numberUserInput)
- {
- books.Add(bookNeed);
- }
- }
- }
- else if (userChoiceSelect == NameBookChoice)
- {
- Console.Write("\nВведите название книги: ");
- userInput = Console.ReadLine();
- foreach (var bookNeed in storageBooks)
- {
- if (bookNeed.Name == userInput)
- {
- books.Add(bookNeed);
- }
- }
- }
- else if (userChoiceSelect == AuthorNameChoice)
- {
- Console.Write("\nВведите имя автора: ");
- userInput = Console.ReadLine();
- foreach (var bookNeed in storageBooks)
- {
- if (bookNeed.AuthorName == userInput)
- {
- books.Add(bookNeed);
- }
- }
- }
- if (books.Count == 0)
- {
- Console.WriteLine("Книги с таким названием нет.");
- }
- return books;
- }
- private bool IsEmptyValue(List<Book> books)
- {
- bool isEmpty = false;
- if (books.Count == 0)
- {
- isEmpty = true;
- Console.WriteLine("Список пуст.");
- }
- return isEmpty;
- }
- private int ReadInt()
- {
- bool isWorking = true;
- int number = 0;
- while (isWorking)
- {
- bool isNumber = int.TryParse(Console.ReadLine(), out number);
- if (isNumber == false)
- {
- Console.WriteLine("Введите число.");
- }
- else
- {
- isWorking = false;
- }
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment