Advertisement
TwinFrame

BookStorage

Mar 28th, 2022 (edited)
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BookStorage
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool isOpen = true;
  14.             Storage storage = new Storage();
  15.  
  16.             while (isOpen)
  17.             {
  18.                 Console.Clear();
  19.                 Console.CursorVisible = false;
  20.                 Console.WriteLine("Хранилище книг.\n");
  21.                 Console.WriteLine("F1 - Добавить книгу");
  22.                 Console.WriteLine("F2 - Удалить книгу.");
  23.                 Console.WriteLine("F3 - Показать все книги.");
  24.                 Console.WriteLine("F4 - Найти книгу.");
  25.                 Console.WriteLine("F5 - Выход.");
  26.  
  27.                 ConsoleKeyInfo key = Console.ReadKey();
  28.                 Console.WriteLine();
  29.  
  30.                 switch (key.Key)
  31.                 {
  32.                     case ConsoleKey.F1:
  33.                         storage.AddBook();
  34.                         break;
  35.  
  36.                     case ConsoleKey.F2:
  37.                         storage.DeleteBook();
  38.                         break;
  39.  
  40.                     case ConsoleKey.F3:
  41.                         storage.ShowAllBooks();
  42.                         break;
  43.  
  44.                     case ConsoleKey.F4:
  45.                         storage.FindBook();
  46.                         break;
  47.  
  48.                     case ConsoleKey.F5:
  49.                         isOpen = false;
  50.                         break;
  51.  
  52.                     default:
  53.                         break;
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     class Storage
  60.     {
  61.         private List<Book> _books;
  62.         private List<Book> _filteredBooks = new List<Book>();
  63.  
  64.         private Book _tempBook;
  65.         private string _tempName;
  66.         private string _tempAutor;
  67.         private string _tempGenre;
  68.         private int _tempYear;
  69.         private int _tempNumber;
  70.         private bool _isCheckingInput;
  71.  
  72.         public Storage()
  73.         {
  74.             _books = new List<Book>();
  75.  
  76.             _books.Add(new Book("Влюблённые в любовь", "Нарцисов", "Драма", 1999));
  77.             _books.Add(new Book("Практикуем практику", "Труднин", "Детектив", 2000));
  78.             _books.Add(new Book("Гуляя на прогулке", "Хотьбин", "Боевик", 2000));
  79.             _books.Add(new Book("Четвертый", "Пятов", "Драма", 2001));
  80.             _books.Add(new Book("Подарите мне чувства", "Труднин", "Драма", 2003));
  81.             _books.Add(new Book("Против всех", "Пятов", "Боевик", 2003));
  82.         }
  83.  
  84.         public void AddBook()
  85.         {
  86.             PrepareScreen("Добавление книги.");
  87.  
  88.             Console.Write("\nВведите название книги: ");
  89.             _tempName = Console.ReadLine();
  90.  
  91.             Console.Write("Введите автора: ");
  92.             _tempAutor = Console.ReadLine();
  93.  
  94.             Console.Write("Введите жанр: ");
  95.             _tempGenre = Console.ReadLine();
  96.  
  97.             _tempYear = ReadYear();
  98.  
  99.             _tempBook = new Book(_tempName, _tempAutor, _tempGenre, _tempYear);
  100.             AddBook(_tempBook);
  101.  
  102.             Console.CursorVisible = false;
  103.             Console.WriteLine("\nДобавлена новая книга: ");
  104.             _tempBook.ShowBook();
  105.             WriteMessage();
  106.         }
  107.  
  108.         public void DeleteBook()
  109.         {
  110.             PrepareScreen("Хранилище: ");
  111.  
  112.             ShowNumberedBooks();
  113.  
  114.             _isCheckingInput = true;
  115.             while (_isCheckingInput)
  116.             {
  117.                 Console.Write("\nДля удаления выберете номер книги: ");
  118.  
  119.                 if (TryConvertStringToInt(Console.ReadLine(), out _tempNumber) && TryFindBook(_tempNumber - 1))
  120.                 {
  121.                     _isCheckingInput = false;
  122.  
  123.                     _tempBook = DeleteBook(_tempNumber - 1);
  124.  
  125.                     Console.CursorVisible = false;
  126.                     Console.WriteLine("\nУдалена книга: ");
  127.                     _tempBook.ShowBook();
  128.                     WriteMessage();
  129.                 }
  130.                 else
  131.                 {
  132.                     WriteMessage("Не корректно введен номер книги или такой книги не существует. Попробуйте еще раз.");
  133.                 }
  134.             }
  135.         }
  136.  
  137.         public void ShowAllBooks()
  138.         {
  139.             PrepareScreen("Хранилище: ");
  140.  
  141.             ShowNumberedBooks();
  142.  
  143.             Console.CursorVisible = false;
  144.             WriteMessage();
  145.         }
  146.  
  147.         public void FindBook()
  148.         {
  149.             PrepareScreen("Поиск книги.");
  150.             Console.CursorVisible = false;
  151.  
  152.             Console.WriteLine("\nВыберете параметр поиска книги:\n");
  153.             Console.WriteLine("F1 - По названию");
  154.             Console.WriteLine("F2 - По автору");
  155.             Console.WriteLine("F3 - По жанру");
  156.             Console.WriteLine("F4 - По году");
  157.             Console.WriteLine("F5 - Назад");
  158.  
  159.             ConsoleKeyInfo key = Console.ReadKey();
  160.             Console.WriteLine();
  161.  
  162.             switch (key.Key)
  163.             {
  164.                 case ConsoleKey.F1:
  165.                     FindBooksByName();
  166.                     break;
  167.  
  168.                 case ConsoleKey.F2:
  169.                     FindBooksByAutor();
  170.                     break;
  171.  
  172.                 case ConsoleKey.F3:
  173.                     FindBooksByGenre();
  174.                     break;
  175.  
  176.                 case ConsoleKey.F4:
  177.                     FindBooksByYear();
  178.                     break;
  179.  
  180.                 default:
  181.                 case ConsoleKey.F5:
  182.                     break;
  183.             }
  184.         }
  185.  
  186.  
  187.         private void FindBooksByName()
  188.         {
  189.             Console.CursorVisible = true;
  190.             Console.Write("Введите название книги: ");
  191.             string userInput = Console.ReadLine();
  192.  
  193.             List<Book> filteredBooks = FindBooksByName(userInput);
  194.             ShowSearchBooks(filteredBooks);
  195.  
  196.             Console.CursorVisible = false;
  197.             WriteMessage();
  198.         }
  199.  
  200.         private List<Book> FindBooksByName(string name)
  201.         {
  202.             _filteredBooks.Clear();
  203.             _filteredBooks = _books.Where(book => book.Name == name).ToList();
  204.             return _filteredBooks;
  205.         }
  206.  
  207.         private void FindBooksByAutor()
  208.         {
  209.             Console.CursorVisible = true;
  210.             Console.Write("Введите автора книги: ");
  211.             string userInput = Console.ReadLine();
  212.  
  213.             List<Book> filteredBooks = FindBooksByAutor(userInput);
  214.             ShowSearchBooks(filteredBooks);
  215.  
  216.             Console.CursorVisible = false;
  217.             WriteMessage();
  218.         }
  219.  
  220.         private List<Book> FindBooksByAutor(string autor)
  221.         {
  222.             _filteredBooks.Clear();
  223.             _filteredBooks = _books.Where(book => book.Autor == autor).ToList();
  224.             return _filteredBooks;
  225.         }
  226.  
  227.         private void FindBooksByGenre()
  228.         {
  229.             Console.CursorVisible = true;
  230.             Console.Write("Введите жанр книги: ");
  231.             string userInput = Console.ReadLine();
  232.  
  233.             List<Book> filteredBooks = FindBooksByGenre(userInput);
  234.             ShowSearchBooks(filteredBooks);
  235.  
  236.             Console.CursorVisible = false;
  237.             WriteMessage();
  238.         }
  239.  
  240.         private List<Book> FindBooksByGenre(string genre)
  241.         {
  242.             _filteredBooks.Clear();
  243.             _filteredBooks = _books.Where(book => book.Genre == genre).ToList();
  244.             return _filteredBooks;
  245.         }
  246.  
  247.  
  248.         private void FindBooksByYear()
  249.         {
  250.             _tempYear = ReadYear();
  251.  
  252.             List<Book> filteredBooks = FindBooksByYear(_tempYear);
  253.             ShowSearchBooks(filteredBooks);
  254.  
  255.             Console.CursorVisible = false;
  256.             WriteMessage();
  257.         }
  258.  
  259.         private List<Book> FindBooksByYear(int year)
  260.         {
  261.             _filteredBooks.Clear();
  262.             _filteredBooks = _books.Where(book => book.Year == year).ToList();
  263.             return _filteredBooks;
  264.         }
  265.  
  266.         private void ShowNumberedBooks()
  267.         {
  268.             for (int i = 0; i < _books.Count; i++)
  269.             {
  270.                 Console.Write($"{i + 1} ");
  271.                 _books[i].ShowBook();
  272.             }
  273.  
  274.             Console.WriteLine();
  275.         }
  276.  
  277.         private void ShowNumberedBooks(List<Book> books)
  278.         {
  279.             for (int i = 0; i < books.Count; i++)
  280.             {
  281.                 Console.Write($"{i + 1} ");
  282.                 books[i].ShowBook();
  283.             }
  284.  
  285.             Console.WriteLine();
  286.         }
  287.  
  288.         private void AddBook(Book book)
  289.         {
  290.             _books.Add(book);
  291.         }
  292.  
  293.         private Book DeleteBook(int number)
  294.         {
  295.             Book book = _books[number];
  296.  
  297.             _books.RemoveAt(number);
  298.  
  299.             return book;
  300.         }
  301.  
  302.         private void ShowSearchBooks(List<Book> books)
  303.         {
  304.             Console.Write($"\nНайдено книг {books.Count}: \n");
  305.  
  306.             if (books.Count > 0)
  307.                 ShowNumberedBooks(books);
  308.             else
  309.                 Console.WriteLine("<<Пусто>>");
  310.         }
  311.  
  312.         private bool TryFindBook(int number)
  313.         {
  314.             if (number >= 0 && number < _books.Count)
  315.                 return true;
  316.             else
  317.                 return false;
  318.         }
  319.  
  320.         private int ReadYear()
  321.         {
  322.             int year = Int32.MinValue;
  323.  
  324.             _isCheckingInput = true;
  325.             while (_isCheckingInput)
  326.             {
  327.                 Console.Write("Введите год издания: ");
  328.  
  329.                 if (TryConvertStringToInt(Console.ReadLine(), out _tempYear))
  330.                 {
  331.                     year = _tempYear;
  332.  
  333.                     _isCheckingInput = false;
  334.                 }
  335.                 else
  336.                 {
  337.                     year = Int32.MinValue;
  338.  
  339.                     WriteMessage("Не корректно введен год издания. Попробуйте еще раз.");
  340.                 }
  341.             }
  342.  
  343.             return year;
  344.         }
  345.  
  346.         private bool TryConvertStringToInt(string text, out int value)
  347.         {
  348.             bool isCorrectValue = Int32.TryParse(text, out int valueInt);
  349.  
  350.             if (isCorrectValue)
  351.             {
  352.                 value = valueInt;
  353.                 return true;
  354.             }
  355.             else
  356.             {
  357.                 value = Int32.MinValue;
  358.                 return false;
  359.             }
  360.         }
  361.  
  362.         private void WriteMessage(string message = "\nДля продолжения нажмите любую клавишу.",
  363.                                         ConsoleColor consoleColor = ConsoleColor.DarkYellow)
  364.         {
  365.             Console.ForegroundColor = consoleColor;
  366.             Console.WriteLine(message);
  367.             Console.ForegroundColor = ConsoleColor.Gray;
  368.             Console.ReadKey();
  369.         }
  370.  
  371.         private void PrepareScreen(string headerText)
  372.         {
  373.             Console.Clear();
  374.             Console.CursorVisible = true;
  375.             Console.WriteLine(headerText);
  376.         }
  377.     }
  378.  
  379.     class Book
  380.     {
  381.         private string _name;
  382.         private string _autor;
  383.         private string _genre;
  384.         private int _year;
  385.  
  386.         public string Name => _name;
  387.         public string Autor => _autor;
  388.         public string Genre => _genre;
  389.         public int Year => _year;
  390.  
  391.         public Book(string name, string autor, string genre, int year)
  392.         {
  393.             _name = name;
  394.             _autor = autor;
  395.             _genre = genre;
  396.             _year = year;
  397.         }
  398.  
  399.         public void ShowBook()
  400.         {
  401.             Console.WriteLine("{0}; Автор: {1}; Жанр: {2}; Год издания: {3};", _name, _autor, _genre, _year);
  402.         }
  403.     }
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement