Rodunskiy

Untitled

Aug 1st, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         const string AddBookCommand = "1";
  9.         const string RemoveBookCommand = "2";
  10.         const string ShowAllBookCommand = "3";
  11.         const string ShowAllBookByParameterCommand = "4";
  12.         const string ExitCommand = "5";
  13.  
  14.         string userInput;
  15.         bool isWorking = true;
  16.  
  17.         StorageBook storageBook = new StorageBook();
  18.  
  19.         while (isWorking)
  20.         {
  21.             Console.ForegroundColor = ConsoleColor.Green;
  22.             Console.WriteLine("Хранилище книг.");
  23.             Console.ForegroundColor = ConsoleColor.White;
  24.             Console.WriteLine($"{AddBookCommand})Добавить книгу.\n{RemoveBookCommand})Удалить книгу.\n{ShowAllBookCommand})Показать все книги." +
  25.                 $"\n{ShowAllBookByParameterCommand})Показать книги по указанному параметру.\n{ExitCommand})Выход из программы.");
  26.  
  27.             userInput = Console.ReadLine();
  28.  
  29.             switch (userInput)
  30.             {
  31.                 case AddBookCommand:
  32.                     storageBook.AddBook();
  33.                     break;
  34.  
  35.                 case RemoveBookCommand:
  36.                     storageBook.RemoveBook();
  37.                     break;
  38.  
  39.                 case ShowAllBookCommand:
  40.                     storageBook.ShowInfo();
  41.                     break;
  42.  
  43.                 case ShowAllBookByParameterCommand:
  44.                     storageBook.ShowInfoBooksByParameter();
  45.                     break;
  46.  
  47.                 case ExitCommand:
  48.                     isWorking = false;
  49.                     break;
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. class StorageBook
  56. {
  57.     private List<Book> _books = new List<Book>();
  58.  
  59.     private int _universalNumber = 1000;
  60.  
  61.     public void AddBook()
  62.     {
  63.         string userInputName;
  64.         string userInputAuthor;
  65.         int userInputReleaseYear;
  66.  
  67.         Console.WriteLine("Введите название книги, её автора и год релиза.");
  68.  
  69.         userInputName = Console.ReadLine();
  70.         userInputAuthor = Console.ReadLine();
  71.         userInputReleaseYear = Utils.ConvertToInt();
  72.  
  73.         _books.Add(new Book(_universalNumber, userInputName, userInputAuthor, userInputReleaseYear));
  74.  
  75.         _universalNumber++;
  76.     }
  77.  
  78.     public void RemoveBook()
  79.     {
  80.         if (TryGetBook(out Book book))
  81.         {
  82.             _books.Remove(book);
  83.         }
  84.     }
  85.  
  86.     private bool TryGetBook(out Book book)
  87.     {
  88.         ShowInfo();
  89.  
  90.         Console.WriteLine("Книгу под каким уникальным номером вы хотите удалить?");
  91.  
  92.         int userInput = Utils.ConvertToInt();
  93.  
  94.         foreach (Book item in _books)
  95.         {
  96.             if (item.UniversalNumber == userInput)
  97.             {
  98.                 book = item;
  99.                 return true;
  100.             }
  101.         }
  102.  
  103.         book = null;
  104.  
  105.         Console.WriteLine("Книга по такому ID не найдена.");
  106.  
  107.         return false;
  108.     }
  109.  
  110.     public void ShowInfo()
  111.     {
  112.         foreach (Book book in _books)
  113.         {
  114.             book.ShowDescription();
  115.         }
  116.     }
  117.  
  118.     public void ShowInfoBooksByParameter()
  119.     {
  120.         const string SearchByNameCommand = "1";
  121.         const string SearchByAuthorCommand = "2";
  122.         const string SearchByReleaseCommand = "3";
  123.  
  124.         string userInput;
  125.  
  126.         Console.WriteLine($"По какому параметру вы бы хотели найти книгу?\n{SearchByNameCommand})По имени.\n{SearchByAuthorCommand})По автору.\n{SearchByReleaseCommand})По году релиза.");
  127.  
  128.         userInput = Console.ReadLine();
  129.  
  130.         switch (userInput)
  131.         {
  132.             case SearchByNameCommand:
  133.                 SearchByName("Введите название книги:", _books);
  134.                 break;
  135.  
  136.             case SearchByAuthorCommand:
  137.                 SearchByAuthor("Введите имя автора:", _books);
  138.                 break;
  139.  
  140.             case SearchByReleaseCommand:
  141.                 SearchByRelease("Введите год релиза:", _books);
  142.                 break;
  143.         }
  144.     }
  145.  
  146.     private void SearchByName(string text, List<Book> books)
  147.     {
  148.         Console.WriteLine(text);
  149.  
  150.         string userInput = Console.ReadLine();
  151.  
  152.         foreach (var book in books)
  153.         {
  154.             if (book.Name == userInput)
  155.             {
  156.                 book.ShowDescription();
  157.             }
  158.         }
  159.     }
  160.  
  161.     private void SearchByAuthor(string text, List<Book> books)
  162.     {
  163.         Console.WriteLine(text);
  164.  
  165.         string userInput = Console.ReadLine();
  166.  
  167.         foreach (var book in books)
  168.         {
  169.             if (book.Author == userInput)
  170.             {
  171.                 book.ShowDescription();
  172.             }
  173.         }
  174.     }
  175.  
  176.     private void SearchByRelease(string text, List<Book> books)
  177.     {
  178.         Console.WriteLine(text);
  179.  
  180.         int userInput = Utils.ConvertToInt();
  181.  
  182.         foreach (var book in books)
  183.         {
  184.             if (book.ReleaseYear == userInput)
  185.             {
  186.                 book.ShowDescription();
  187.             }
  188.         }
  189.     }
  190. }
  191.  
  192. class Book
  193. {
  194.     public Book(int universalNumber, string name, string author, int releaseYear)
  195.     {
  196.         UniversalNumber = universalNumber;
  197.         Name = name;
  198.         Author = author;
  199.         ReleaseYear = releaseYear;
  200.     }
  201.  
  202.     public int UniversalNumber { get; private set; }
  203.     public string Name { get;private set; }
  204.     public string Author { get; private set; }
  205.     public int ReleaseYear { get; private set; }
  206.  
  207.     public void ShowDescription()
  208.     {
  209.         Console.WriteLine($"Уникальный номер:{UniversalNumber}|Название книги:{Name}|Автор:{Author}|Год релиза:{ReleaseYear}");
  210.     }
  211. }
  212.  
  213. class Utils
  214. {
  215.     public static int ConvertToInt()
  216.     {
  217.         int templateNumber;
  218.         string userInput = string.Empty;
  219.  
  220.         while (int.TryParse(userInput, out templateNumber) == false)
  221.         {
  222.             userInput = Console.ReadLine();
  223.         }
  224.  
  225.         return templateNumber;
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment