Advertisement
Zlobin2021

Library

Mar 16th, 2022 (edited)
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Library
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Library library = new Library();
  11.             bool isWork = true;
  12.  
  13.             while (isWork)
  14.             {
  15.                 Console.WriteLine("1.Добавить книгу.\n2.Убрать книгу.\n3.Показать все книги.\n4.Показать книгу по параметру.\n5.Выйти.");
  16.                 switch (Console.ReadLine())
  17.                 {
  18.                     case "1":
  19.                         library.AddBook();
  20.                         break;
  21.                     case "2":
  22.                         library.DeleteBook();
  23.                         break;
  24.                     case "3":
  25.                         library.ShowAllBooks();
  26.                         break;
  27.                     case "4":
  28.                         library.SelectOption();
  29.                         break;
  30.                     case "5":
  31.                         isWork = false;
  32.                         break;
  33.                 }
  34.                 Console.WriteLine("\nЧтобы продолжить нажмите любую кнопку");
  35.                 Console.ReadLine();
  36.                 Console.Clear();
  37.             }
  38.         }
  39.     }
  40.  
  41.     class Library
  42.     {
  43.         private List<Books> _book = new List<Books>();
  44.  
  45.         public void AddBook()
  46.         {
  47.             Console.WriteLine("Введите Название книги: ");
  48.             string title = Console.ReadLine();
  49.             Console.WriteLine("Введите Автора книги: ");
  50.             string autor = Console.ReadLine();
  51.             Console.WriteLine("Введите Жанр книги: ");
  52.             string genre = Console.ReadLine();
  53.             Console.WriteLine("Введите Год книги: ");
  54.             int year;
  55.             if (int.TryParse(Console.ReadLine(), out year) == true)
  56.                 _book.Add(new Books(title, autor, genre, year));
  57.         }
  58.  
  59.         public void ShowAllBooks()
  60.         {
  61.             for (int i = 0; i < _book.Count; i++)
  62.             {
  63.                 ShowInfo(i);
  64.             }
  65.         }
  66.  
  67.         public void DeleteBook()
  68.         {
  69.             for (int i = 0; i < _book.Count; i++)
  70.             {
  71.                 Console.WriteLine($"Номер книги: {_book[i].Number}.\nИмя: {_book[i].Title}.\n");
  72.             }
  73.  
  74.             Console.Write("Введите номер книги: ");
  75.             int number;
  76.             if (int.TryParse(Console.ReadLine(), out number) == true && number < _book.Count && number >= 0)
  77.                 _book.RemoveAt(number - 1);
  78.         }
  79.  
  80.         public void SelectOption()
  81.         {
  82.             Console.WriteLine("Выбирите параметр.");
  83.             Console.WriteLine("1.Название.\n2.Автор.\n3.Жанр.\nГод.");
  84.  
  85.             switch (Console.ReadLine())
  86.             {
  87.                 case "1":
  88.                     NameSearch();
  89.                     break;
  90.                 case "2":
  91.                     SearchAuthor();
  92.                     break;
  93.                 case "3":
  94.                     SearchGenre();
  95.                     break;
  96.                 case "4":
  97.                     SearchYear();
  98.                     break;
  99.             }
  100.         }
  101.  
  102.         private void NameSearch()
  103.         {
  104.             string title = Console.ReadLine();
  105.             for (int i = 0; i < _book.Count; i++)
  106.             {
  107.                 if (title == _book[i].Title)
  108.                 {
  109.                     ShowInfo(i);
  110.                 }
  111.             }
  112.         }
  113.  
  114.         private void SearchAuthor()
  115.         {
  116.             string autor = Console.ReadLine();
  117.             for (int i = 0; i < _book.Count; i++)
  118.             {
  119.                 if (autor == _book[i].Author)
  120.                 {
  121.                     ShowInfo(i);
  122.                 }
  123.             }
  124.         }
  125.  
  126.         private void SearchGenre()
  127.         {
  128.             string genre = Console.ReadLine();
  129.             for (int i = 0; i < _book.Count; i++)
  130.             {
  131.                 if (genre == _book[i].Genre)
  132.                 {
  133.                     ShowInfo(i);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         private void SearchYear()
  139.         {
  140.             int year;
  141.             if (int.TryParse(Console.ReadLine(), out year) == true)
  142.                 for (int i = 0; i < _book.Count; i++)
  143.                 {
  144.                     if (year == _book[i].Year)
  145.                     {
  146.                         ShowInfo(i);
  147.                     }
  148.                 }
  149.         }
  150.  
  151.         private void ShowInfo(int number)
  152.         {
  153.             Console.WriteLine($"Номер книги: {_book[number].Number}.\nИмя: {_book[number].Title}.\nАвтор: {_book[number].Author}.\nЖанр: {_book[number].Genre}.\nГод: {_book[number].Year}.\n");
  154.         }
  155.     }
  156.  
  157.     class Books
  158.     {
  159.         private static int _number = 0;
  160.         public int Number { get; private set; }
  161.         public string Title { get; private set; }
  162.         public string Author { get; private set; }
  163.         public string Genre { get; private set; }
  164.         public int Year { get; private set; }
  165.  
  166.         public Books(string title, string autor, string genre, int year)
  167.         {
  168.             _number++;
  169.             Number = _number;
  170.             Title = title;
  171.             Author = autor;
  172.             Genre = genre;
  173.             Year = year;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement