Advertisement
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 BookStorage
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool isOpen = true;
- Storage storage = new Storage();
- while (isOpen)
- {
- Console.Clear();
- Console.CursorVisible = false;
- Console.WriteLine("Хранилище книг.\n");
- Console.WriteLine("F1 - Добавить книгу");
- Console.WriteLine("F2 - Удалить книгу.");
- Console.WriteLine("F3 - Показать все книги.");
- Console.WriteLine("F4 - Найти книгу.");
- Console.WriteLine("F5 - Выход.");
- ConsoleKeyInfo key = Console.ReadKey();
- Console.WriteLine();
- switch (key.Key)
- {
- case ConsoleKey.F1:
- storage.AddBook();
- break;
- case ConsoleKey.F2:
- storage.DeleteBook();
- break;
- case ConsoleKey.F3:
- storage.ShowAllBooks();
- break;
- case ConsoleKey.F4:
- storage.FindBook();
- break;
- case ConsoleKey.F5:
- isOpen = false;
- break;
- default:
- break;
- }
- }
- }
- }
- class Storage
- {
- private List<Book> _books;
- private List<Book> _filteredBooks = new List<Book>();
- private Book _tempBook;
- private string _tempName;
- private string _tempAutor;
- private string _tempGenre;
- private int _tempYear;
- private int _tempNumber;
- private bool _isCheckingInput;
- public Storage()
- {
- _books = new List<Book>();
- _books.Add(new Book("Влюблённые в любовь", "Нарцисов", "Драма", 1999));
- _books.Add(new Book("Практикуем практику", "Труднин", "Детектив", 2000));
- _books.Add(new Book("Гуляя на прогулке", "Хотьбин", "Боевик", 2000));
- _books.Add(new Book("Четвертый", "Пятов", "Драма", 2001));
- _books.Add(new Book("Подарите мне чувства", "Труднин", "Драма", 2003));
- _books.Add(new Book("Против всех", "Пятов", "Боевик", 2003));
- }
- public void AddBook()
- {
- PrepareScreen("Добавление книги.");
- Console.Write("\nВведите название книги: ");
- _tempName = Console.ReadLine();
- Console.Write("Введите автора: ");
- _tempAutor = Console.ReadLine();
- Console.Write("Введите жанр: ");
- _tempGenre = Console.ReadLine();
- _tempYear = ReadYear();
- _tempBook = new Book(_tempName, _tempAutor, _tempGenre, _tempYear);
- AddBook(_tempBook);
- Console.CursorVisible = false;
- Console.WriteLine("\nДобавлена новая книга: ");
- _tempBook.ShowBook();
- WriteMessage();
- }
- public void DeleteBook()
- {
- PrepareScreen("Хранилище: ");
- ShowNumberedBooks();
- _isCheckingInput = true;
- while (_isCheckingInput)
- {
- Console.Write("\nДля удаления выберете номер книги: ");
- if (TryConvertStringToInt(Console.ReadLine(), out _tempNumber) && TryFindBook(_tempNumber - 1))
- {
- _isCheckingInput = false;
- _tempBook = DeleteBook(_tempNumber - 1);
- Console.CursorVisible = false;
- Console.WriteLine("\nУдалена книга: ");
- _tempBook.ShowBook();
- WriteMessage();
- }
- else
- {
- WriteMessage("Не корректно введен номер книги или такой книги не существует. Попробуйте еще раз.");
- }
- }
- }
- public void ShowAllBooks()
- {
- PrepareScreen("Хранилище: ");
- ShowNumberedBooks();
- Console.CursorVisible = false;
- WriteMessage();
- }
- public void FindBook()
- {
- PrepareScreen("Поиск книги.");
- Console.CursorVisible = false;
- Console.WriteLine("\nВыберете параметр поиска книги:\n");
- Console.WriteLine("F1 - По названию");
- Console.WriteLine("F2 - По автору");
- Console.WriteLine("F3 - По жанру");
- Console.WriteLine("F4 - По году");
- Console.WriteLine("F5 - Назад");
- ConsoleKeyInfo key = Console.ReadKey();
- Console.WriteLine();
- switch (key.Key)
- {
- case ConsoleKey.F1:
- FindBooksByName();
- break;
- case ConsoleKey.F2:
- FindBooksByAutor();
- break;
- case ConsoleKey.F3:
- FindBooksByGenre();
- break;
- case ConsoleKey.F4:
- FindBooksByYear();
- break;
- default:
- case ConsoleKey.F5:
- break;
- }
- }
- private void FindBooksByName()
- {
- Console.CursorVisible = true;
- Console.Write("Введите название книги: ");
- string userInput = Console.ReadLine();
- List<Book> filteredBooks = FindBooksByName(userInput);
- ShowSearchBooks(filteredBooks);
- Console.CursorVisible = false;
- WriteMessage();
- }
- private List<Book> FindBooksByName(string name)
- {
- _filteredBooks.Clear();
- _filteredBooks = _books.Where(book => book.Name == name).ToList();
- return _filteredBooks;
- }
- private void FindBooksByAutor()
- {
- Console.CursorVisible = true;
- Console.Write("Введите автора книги: ");
- string userInput = Console.ReadLine();
- List<Book> filteredBooks = FindBooksByAutor(userInput);
- ShowSearchBooks(filteredBooks);
- Console.CursorVisible = false;
- WriteMessage();
- }
- private List<Book> FindBooksByAutor(string autor)
- {
- _filteredBooks.Clear();
- _filteredBooks = _books.Where(book => book.Autor == autor).ToList();
- return _filteredBooks;
- }
- private void FindBooksByGenre()
- {
- Console.CursorVisible = true;
- Console.Write("Введите жанр книги: ");
- string userInput = Console.ReadLine();
- List<Book> filteredBooks = FindBooksByGenre(userInput);
- ShowSearchBooks(filteredBooks);
- Console.CursorVisible = false;
- WriteMessage();
- }
- private List<Book> FindBooksByGenre(string genre)
- {
- _filteredBooks.Clear();
- _filteredBooks = _books.Where(book => book.Genre == genre).ToList();
- return _filteredBooks;
- }
- private void FindBooksByYear()
- {
- _tempYear = ReadYear();
- List<Book> filteredBooks = FindBooksByYear(_tempYear);
- ShowSearchBooks(filteredBooks);
- Console.CursorVisible = false;
- WriteMessage();
- }
- private List<Book> FindBooksByYear(int year)
- {
- _filteredBooks.Clear();
- _filteredBooks = _books.Where(book => book.Year == year).ToList();
- return _filteredBooks;
- }
- private void ShowNumberedBooks()
- {
- for (int i = 0; i < _books.Count; i++)
- {
- Console.Write($"{i + 1} ");
- _books[i].ShowBook();
- }
- Console.WriteLine();
- }
- private void ShowNumberedBooks(List<Book> books)
- {
- for (int i = 0; i < books.Count; i++)
- {
- Console.Write($"{i + 1} ");
- books[i].ShowBook();
- }
- Console.WriteLine();
- }
- private void AddBook(Book book)
- {
- _books.Add(book);
- }
- private Book DeleteBook(int number)
- {
- Book book = _books[number];
- _books.RemoveAt(number);
- return book;
- }
- private void ShowSearchBooks(List<Book> books)
- {
- Console.Write($"\nНайдено книг {books.Count}: \n");
- if (books.Count > 0)
- ShowNumberedBooks(books);
- else
- Console.WriteLine("<<Пусто>>");
- }
- private bool TryFindBook(int number)
- {
- if (number >= 0 && number < _books.Count)
- return true;
- else
- return false;
- }
- private int ReadYear()
- {
- int year = Int32.MinValue;
- _isCheckingInput = true;
- while (_isCheckingInput)
- {
- Console.Write("Введите год издания: ");
- if (TryConvertStringToInt(Console.ReadLine(), out _tempYear))
- {
- year = _tempYear;
- _isCheckingInput = false;
- }
- else
- {
- year = Int32.MinValue;
- WriteMessage("Не корректно введен год издания. Попробуйте еще раз.");
- }
- }
- return year;
- }
- private bool TryConvertStringToInt(string text, out int value)
- {
- bool isCorrectValue = Int32.TryParse(text, out int valueInt);
- if (isCorrectValue)
- {
- value = valueInt;
- return true;
- }
- else
- {
- value = Int32.MinValue;
- return false;
- }
- }
- private void WriteMessage(string message = "\nДля продолжения нажмите любую клавишу.",
- ConsoleColor consoleColor = ConsoleColor.DarkYellow)
- {
- Console.ForegroundColor = consoleColor;
- Console.WriteLine(message);
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.ReadKey();
- }
- private void PrepareScreen(string headerText)
- {
- Console.Clear();
- Console.CursorVisible = true;
- Console.WriteLine(headerText);
- }
- }
- class Book
- {
- private string _name;
- private string _autor;
- private string _genre;
- private int _year;
- public string Name => _name;
- public string Autor => _autor;
- public string Genre => _genre;
- public int Year => _year;
- public Book(string name, string autor, string genre, int year)
- {
- _name = name;
- _autor = autor;
- _genre = genre;
- _year = year;
- }
- public void ShowBook()
- {
- Console.WriteLine("{0}; Автор: {1}; Жанр: {2}; Год издания: {3};", _name, _autor, _genre, _year);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement