Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace C_Sharp_Junior
- {
- class Program
- {
- static void Main()
- {
- Storage storage = new Storage();
- bool isExit = false;
- while (isExit == false)
- {
- Console.Write("1 - Создать книгу\n2 - Удалить книгу\n3 - Показать все книги\n4 - Поиск книг по параметрам\n5 - Закрыть программу\nВаш выбор:");
- bool successfulConversion = int.TryParse(Console.ReadLine(), out int userInput);
- Console.Clear();
- if (successfulConversion == true)
- {
- switch (userInput)
- {
- case 1:
- storage.CreateBook();
- break;
- case 2:
- storage.DeleteBook();
- break;
- case 3:
- storage.ShowBooks();
- break;
- case 4:
- storage.BookSearch();
- break;
- case 5:
- isExit = true;
- break;
- }
- }
- Console.Write("Нажмите любую кнопку для возвращения в главное меню");
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Storage
- {
- private List<Book> _books;
- public Storage()
- {
- _books = new List<Book> { new Book("Хулиномика", "Алексей Марков", 2017, 5), new Book("Самый богатый человек в Вавилоне", "Джордж Клейсон", 2012), new Book("Теория игр", "Диксит Авинаш", 2022, 4) };
- }
- public void ShowBooks()
- {
- for (int i = 0; i < _books.Count; i++)
- {
- Console.Write($"№{i + 1} | ");
- _books[i].ShowInfo();
- }
- }
- public void DeleteBook()
- {
- ShowBooks();
- Console.Write("\nВведите название книги: ");
- try
- {
- int index = Convert.ToInt32(Console.ReadLine()) - 1;
- _books.RemoveAt(index);
- Console.WriteLine("Книга удалена успешна");
- }
- catch (Exception)
- {
- Console.WriteLine("\nПроизошла ошибка(такой книги нет), повторите попытку удалить книгу");
- Console.ReadKey();
- Console.Clear();
- DeleteBook();
- }
- }
- public void CreateBook()
- {
- int initialNumberElements = _books.Count;
- do
- {
- try
- {
- Console.Write("Введите название книги: ");
- string title = Console.ReadLine();
- Console.Write("Введите автора книги: ");
- string author = Console.ReadLine();
- Console.Write("Введите год издания книги: ");
- int yearRelease = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите рейтинг книги от 0 до 5: ");
- int rating = Convert.ToInt32(Console.ReadLine());
- _books.Add(new Book(title, author, yearRelease, rating));
- }
- catch (Exception)
- {
- Console.WriteLine("Произошла ошибка, повторите попытку создать книгу");
- CreateBook();
- }
- } while (initialNumberElements > _books.Count);
- Console.WriteLine("Книга созданна успешна");
- }
- public void BookSearch()
- {
- Console.Write("Введите значение по которому будет осуществляться поиск: ");
- string userInput = Console.ReadLine();
- if (userInput != null)
- {
- for (int i = 0; i < _books.Count; i++)
- {
- if (_books[i].Title.Contains(userInput) || _books[i].Author.Contains(userInput))
- {
- _books[i].ShowInfo();
- }
- bool successfulConversion = int.TryParse(userInput, out int numberUserInput);
- if (successfulConversion == true && numberUserInput == _books[i].Rating || numberUserInput == _books[i].YearRelease)
- {
- _books[i].ShowInfo();
- }
- }
- }
- else
- {
- Console.Write("Произошла ощибка. Повторите попытку поиска.");
- }
- }
- }
- class Book
- {
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int YearRelease { get; private set; }
- public int Rating { get; private set; }
- public Book(string title, string author, int yearRelease = 0, int rating = 0)
- {
- Title = title;
- Author = author;
- if (yearRelease >= 0)
- {
- YearRelease = yearRelease;
- }
- if (rating >= 0 && rating <= 5)
- {
- Rating = rating;
- }
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Название: {Title} | Автор: {Author} | Год издания: {YearRelease} | Рейтинг: {Rating}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement