Advertisement
Askor

NewHw39

Apr 15th, 2022 (edited)
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     private static List<Book> _books = new List<Book>();
  7.  
  8.     static void Main(string[] args)
  9.     {
  10.         ShowMenu();
  11.     }
  12.  
  13.     static void ShowMenu()
  14.     {
  15.         string userInput;
  16.  
  17.         do
  18.         {
  19.             Console.WriteLine("Menu: 1 - add book, 2 - show books, 3 - delete book, 4 - find by parameter, 5 - exit");
  20.             userInput = Console.ReadLine();
  21.  
  22.             switch (userInput)
  23.             {
  24.                 case "1":
  25.                     AddBook();
  26.                     break;
  27.                 case "2":
  28.                     ShowAllBooks();
  29.                     break;
  30.                 case "3":
  31.                     DeleteBookByIndex();
  32.                     break;
  33.                 case "4":
  34.                     FindBooksByParameter();
  35.                     break;
  36.                 default:
  37.                     Console.WriteLine("Invalid input");
  38.                     break;
  39.             }
  40.  
  41.             Console.ReadKey();
  42.             Console.Clear();
  43.         }
  44.         while (userInput != "5");
  45.     }
  46.  
  47.     static void AddBook()
  48.     {
  49.         string name;
  50.         string author;
  51.         string yearToConvert;
  52.         int year;
  53.         bool isNumber;
  54.  
  55.         Console.WriteLine("Input name:");
  56.         name = Console.ReadLine();
  57.  
  58.         Console.WriteLine("Input author:");
  59.         author = Console.ReadLine();
  60.  
  61.         do
  62.         {
  63.             Console.WriteLine("Input year:");
  64.             yearToConvert = Console.ReadLine();
  65.  
  66.             isNumber = int.TryParse(yearToConvert, out year);
  67.         }
  68.         while (isNumber == false);
  69.  
  70.         Book book = new Book(name, author, year);
  71.  
  72.         _books.Add(book);
  73.     }
  74.  
  75.     static void ShowAllBooks()
  76.     {
  77.         for (int i = 0; i < _books.Count; i++)
  78.         {
  79.             Console.Write($"{i + 1}. ");
  80.             _books[i].ShowInfo();
  81.         }
  82.     }
  83.  
  84.     static void DeleteBookByIndex()
  85.     {
  86.         ShowAllBooks();
  87.         int index;
  88.         bool isNumber;
  89.  
  90.         do
  91.         {
  92.             Console.WriteLine("Input index to delete:");
  93.             string indexToConvert = Console.ReadLine();
  94.  
  95.             isNumber = int.TryParse(indexToConvert, out index);
  96.         }
  97.         while (isNumber == false);
  98.  
  99.         if(index < 0 && index >= _books.Count)
  100.         {
  101.             Console.WriteLine("Invalid index");
  102.         }
  103.         else
  104.         {
  105.             _books.RemoveAt(index);
  106.  
  107.             Console.WriteLine($"Book whith index {index} deleted");
  108.         }
  109.     }
  110.  
  111.     static void FindBooksByParameter()
  112.     {
  113.         Console.WriteLine("Choose parameter: 1 - name, 2 - author, 3 - year");
  114.  
  115.         string userInput = Console.ReadLine();
  116.  
  117.         switch (userInput)
  118.         {
  119.             case "1":
  120.                 FindByName();
  121.                 break;
  122.             case "2":
  123.                 FindByAuthor();
  124.                 break;
  125.             case "3":
  126.                 FindByYear();
  127.                 break;
  128.             default:
  129.                 Console.WriteLine("Invalid input");
  130.                 break;
  131.         }
  132.     }
  133.  
  134.     static void FindByName()
  135.     {
  136.         Console.WriteLine("Input name: ");
  137.         string name = Console.ReadLine();
  138.  
  139.         foreach (var book in _books)
  140.         {
  141.             if (book.Name == name)
  142.             {
  143.                 book.ShowInfo();
  144.             }
  145.         }
  146.     }
  147.  
  148.     static void FindByAuthor()
  149.     {
  150.         Console.WriteLine("Input author: ");
  151.         string author = Console.ReadLine();
  152.  
  153.         foreach (var book in _books)
  154.         {
  155.             if (book.Author == author)
  156.             {
  157.                 book.ShowInfo();
  158.             }
  159.         }
  160.     }
  161.  
  162.     static void FindByYear()
  163.     {
  164.         bool isNumber;
  165.         int year;
  166.  
  167.         do
  168.         {
  169.             Console.WriteLine("Input year: ");
  170.             string inputToConvert = Console.ReadLine();
  171.  
  172.             isNumber = int.TryParse(inputToConvert, out year);
  173.         }
  174.         while (isNumber == false);
  175.  
  176.         foreach (var book in _books)
  177.         {
  178.             if (book.YearOfPublication == year)
  179.             {
  180.                 book.ShowInfo();
  181.             }
  182.         }
  183.     }
  184. }
  185.  
  186. class Book
  187. {
  188.     public string Name { get; private set; }
  189.     public string Author { get; private set; }
  190.     public int YearOfPublication { get; private set; }
  191.  
  192.     public Book(string name, string author, int year)
  193.     {
  194.         Name = name;
  195.         Author = author;
  196.         YearOfPublication = year;
  197.     }
  198.  
  199.     public void ShowInfo()
  200.     {
  201.         Console.WriteLine($"Name - {Name}, Author - {Author}, Year - {YearOfPublication}");
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement