SaNik74

books_storage

Jul 3rd, 2024
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. namespace books_storage
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Library library = new Library();
  8.             library.Work();
  9.         }
  10.     }
  11.  
  12.     class Book
  13.     {
  14.         public Book(string name, string authorName, int yearOfPublishng)
  15.         {
  16.             Name = name;
  17.             AuthorName = authorName;
  18.             YearOfPublishng = yearOfPublishng;
  19.         }
  20.  
  21.         public string Name { get; private set; }
  22.         public string AuthorName { get; private set; }
  23.         public int YearOfPublishng { get; private set; }
  24.     }
  25.  
  26.     class Library
  27.     {
  28.         private List<Book> storageBooks = new List<Book>();
  29.  
  30.         public void Work()
  31.         {
  32.             const string AddBookCommand = "1";
  33.             const string FindBookCommand = "2";
  34.             const string DeleteBookCommand = "3";
  35.             const string ShowAllBooksCommand = "4";
  36.             const string ExitCommand = "5";
  37.  
  38.             bool isWorking = true;
  39.  
  40.             while (isWorking)
  41.             {
  42.                 Console.WriteLine($"Хранилище книг\n\n\n" +
  43.                     $"Введите {AddBookCommand} если хотите добавить книгу.\n" +
  44.                     $"Введите {FindBookCommand} если хотите найти книгу.\n" +
  45.                     $"Введите {DeleteBookCommand} если хотите удалить книгу.\n" +
  46.                     $"Введите {ShowAllBooksCommand} если хотите увидеть список книг.\n" +
  47.                     $"Введите {ExitCommand} если хотите выйти из программы.\n\n" +
  48.                     $"Ввод: ");
  49.  
  50.                 string? userInput = Console.ReadLine();
  51.  
  52.                 switch (userInput)
  53.                 {
  54.                     case AddBookCommand:
  55.                         AddBook();
  56.                         break;
  57.  
  58.                     case FindBookCommand:
  59.                         ShowFindBooks();
  60.                         break;
  61.  
  62.                     case ShowAllBooksCommand:
  63.                         ShowBooks(storageBooks);
  64.                         break;
  65.  
  66.                     case DeleteBookCommand:
  67.                         DeleteBook();
  68.                         break;
  69.  
  70.                     case ExitCommand:
  71.                         isWorking = false;
  72.                         break;
  73.  
  74.                     default:
  75.                         Console.WriteLine("Введена неверная команда.");
  76.                         break;
  77.                 }
  78.                 Console.ReadKey();
  79.                 Console.Clear();
  80.             }
  81.         }
  82.  
  83.         private void AddBook()
  84.         {
  85.             Console.Write("Введите название книги:");
  86.             string name = Console.ReadLine();
  87.  
  88.             Console.Write("\nВведите имя автора:");
  89.             string authorName = Console.ReadLine();
  90.  
  91.             Console.Write("\nВведите год книги:");
  92.             int yearOfPublishng = ReadInt();
  93.  
  94.             Book newBook = new Book(name, authorName, yearOfPublishng);
  95.             storageBooks.Add(newBook);
  96.         }
  97.  
  98.         private void ShowBooks(List<Book> books)
  99.         {
  100.             int numberBook = 1;
  101.  
  102.             if (IsEmptyValue(storageBooks) == true)
  103.             {
  104.                 return;
  105.             }
  106.  
  107.             foreach (var book in books)
  108.             {
  109.                 Console.WriteLine($"{numberBook}. {book.Name} - {book.AuthorName} - {book.YearOfPublishng}");
  110.                 numberBook++;
  111.             }
  112.         }
  113.  
  114.         private void ShowFindBooks()
  115.         {
  116.             List<Book> books = FindBook();
  117.             ShowBooks(books);
  118.         }
  119.  
  120.         private void DeleteBook()
  121.         {
  122.             List<Book> books = FindBook();
  123.  
  124.             for (int i = 0; i < storageBooks.Count; i++)
  125.             {
  126.                 for (int j = 0; j < books.Count; j++)
  127.                 {
  128.                     if (storageBooks[i] == books[i])
  129.                     {
  130.                         storageBooks.RemoveAt(i);
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.  
  136.         private string SelectSpecifitedParameter()
  137.         {
  138.             const int NameBookSelectCommand = 1;
  139.             const int AuthorNameSelectCommand = 2;
  140.             const int YearSelectCommand = 3;
  141.  
  142.             const string NameBookChoice = "nameBookSelect";
  143.             const string AuthorNameChoice = "authorNameSelect";
  144.             const string YearBookChoice = "yearSelect";
  145.  
  146.             string userChoice = "";
  147.  
  148.             Console.WriteLine($"Введите какой способ хотите выбрать:\n" +
  149.                 $"{NameBookSelectCommand} - если по названию книги.\n" +
  150.                 $"{AuthorNameSelectCommand} - есла по имени автора.\n" +
  151.                 $"{YearSelectCommand} - если по году издания.");
  152.  
  153.             switch (ReadInt())
  154.             {
  155.                 case NameBookSelectCommand:
  156.                     userChoice = NameBookChoice;
  157.                     break;
  158.  
  159.                 case AuthorNameSelectCommand:
  160.                     userChoice = AuthorNameChoice;
  161.                     break;
  162.  
  163.                 case YearSelectCommand:
  164.                     userChoice = YearBookChoice;
  165.                     break;
  166.  
  167.                 default:
  168.                     Console.WriteLine("Неизвестная команда.");
  169.                     break;
  170.             }
  171.  
  172.             return userChoice;
  173.         }
  174.  
  175.         private List<Book> FindBook()
  176.         {
  177.             const string NameBookChoice = "nameBookSelect";
  178.             const string AuthorNameChoice = "authorNameSelect";
  179.             const string YearBookChoice = "yearSelect";
  180.  
  181.             List<Book> books = new List<Book>();
  182.  
  183.             int numberUserInput;
  184.             string userChoiceSelect;
  185.             string userInput;
  186.  
  187.             if (IsEmptyValue(storageBooks) == true)
  188.             {
  189.                 return null;
  190.             }
  191.  
  192.             userChoiceSelect = SelectSpecifitedParameter();
  193.  
  194.             if (userChoiceSelect == YearBookChoice)
  195.             {
  196.                 Console.WriteLine("Введите год книги: ");
  197.  
  198.                 numberUserInput = ReadInt();
  199.  
  200.                 foreach (var bookNeed in storageBooks)
  201.                 {
  202.                     if (bookNeed.YearOfPublishng == numberUserInput)
  203.                     {
  204.                         books.Add(bookNeed);
  205.                     }
  206.                 }
  207.             }
  208.             else if (userChoiceSelect == NameBookChoice)
  209.             {
  210.                 Console.Write("\nВведите название книги: ");
  211.                 userInput = Console.ReadLine();
  212.  
  213.                 foreach (var bookNeed in storageBooks)
  214.                 {
  215.                     if (bookNeed.Name == userInput)
  216.                     {
  217.                         books.Add(bookNeed);
  218.                     }
  219.                 }
  220.             }
  221.             else if (userChoiceSelect == AuthorNameChoice)
  222.             {
  223.                 Console.Write("\nВведите имя автора: ");
  224.                 userInput = Console.ReadLine();
  225.  
  226.                 foreach (var bookNeed in storageBooks)
  227.                 {
  228.                     if (bookNeed.AuthorName == userInput)
  229.                     {
  230.                         books.Add(bookNeed);
  231.                     }
  232.                 }
  233.             }
  234.  
  235.             if (books.Count == 0)
  236.             {
  237.                 Console.WriteLine("Книги с таким названием нет.");
  238.             }
  239.  
  240.             return books;
  241.         }
  242.  
  243.         private bool IsEmptyValue(List<Book> books)
  244.         {
  245.             bool isEmpty = false;
  246.  
  247.             if (books.Count == 0)
  248.             {
  249.                 isEmpty = true;
  250.                 Console.WriteLine("Список пуст.");
  251.             }
  252.  
  253.             return isEmpty;
  254.         }
  255.         private int ReadInt()
  256.         {
  257.             bool isWorking = true;
  258.             int number = 0;
  259.  
  260.             while (isWorking)
  261.             {
  262.                 bool isNumber = int.TryParse(Console.ReadLine(), out number);
  263.  
  264.                 if (isNumber == false)
  265.                 {
  266.                     Console.WriteLine("Введите число.");
  267.                 }
  268.                 else
  269.                 {
  270.                     isWorking = false;
  271.                 }
  272.             }
  273.  
  274.             return number;
  275.         }
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment