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)
- {
- switch (userInput)
- {
- case 1:
- storage.CreateBook();
- break;
- case 2:
- storage.DeleteBook();
- break;
- case 3:
- storage.ShowBooks();
- break;
- case 4:
- storage.SearchBook();
- 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, 4), new Book("Хулиномика. Теория игр", "Диксит Авинаш", 2022, 4) };
- }
- public void ShowBooks()
- {
- for (int i = 0; i < _books.Count; i++)
- {
- Console.Write($"№{i + 1} | ");
- _books[i].ShowInfo();
- }
- }
- private (string Title, string Author, int PublicationDate, int Rating) GenerateInfoAboutBook()
- {
- bool successfulConversionPublicationDate, successfulConversionRating;
- string title, author;
- int publicationDate, rating;
- do
- {
- Console.Write("Введите название книги: ");
- title = Console.ReadLine();
- Console.Write("Введите автора книги: ");
- author = Console.ReadLine();
- Console.Write("Введите год издания книги: ");
- successfulConversionPublicationDate = int.TryParse(Console.ReadLine(), out publicationDate);
- Console.Write("Введите рейтинг книги от 0 до 5: ");
- successfulConversionRating = int.TryParse(Console.ReadLine(), out rating);
- if (successfulConversionPublicationDate != true || successfulConversionRating != true)
- {
- Console.WriteLine("Ошибка. Повторите ещё раз.");
- }
- } while (successfulConversionPublicationDate == false && successfulConversionRating == false);
- return (title, author, publicationDate, rating);
- }
- public void CreateBook()
- {
- var book = GenerateInfoAboutBook();
- _books.Add(new Book(book.Title, book.Author, book.PublicationDate, book.Rating));
- Console.WriteLine("Книга созданна успешна");
- }
- public void DeleteBook()
- {
- Range rangeAvailableBooks = 1.._books.Count;
- int index;
- ShowBooks();
- do
- {
- Console.Write("Введите индекс книги: ");
- bool successfulConversionIndex = int.TryParse(Console.ReadLine(), out index);
- if (successfulConversionIndex && index >= rangeAvailableBooks.Start.Value && index <= rangeAvailableBooks.End.Value)
- {
- _books.RemoveAt(index - 1);
- }
- else
- {
- Console.WriteLine("Ошибка. Такого индекса нет.");
- }
- } while (index < rangeAvailableBooks.Start.Value || index >= rangeAvailableBooks.End.Value);
- Console.WriteLine("Книга успешна удалена");
- }
- public int SelectSearchParameter()
- {
- int number;
- Range userSelectionRange = 1..5;
- do
- {
- Console.Write("Выберете параметр поиска.\n1 - Название книги\n2 - Автор\n3 - Год издания\n4 - Рейтинг книги\n");
- Console.Write("Ваш Выбор: ");
- bool successfulConversionNumber = int.TryParse(Console.ReadLine(), out number);
- if (successfulConversionNumber == false)
- {
- Console.WriteLine("Ошибка. Неверный ввод.");
- }
- } while (number < userSelectionRange.Start.Value || number >= userSelectionRange.End.Value);
- return number;
- }
- public void SearchBook()
- {
- int numberParameter = SelectSearchParameter();
- Console.Write("Введите значение по которому будет осуществляться поиск: ");
- string userInput = Console.ReadLine();
- if (userInput != null)
- {
- foreach (var book in _books)
- {
- switch (numberParameter)
- {
- case 1:
- if (book.Title.ToUpper() == userInput.ToUpper())
- book.ShowInfo();
- break;
- case 2:
- if (book.Author.ToUpper() == userInput.ToUpper())
- book.ShowInfo();
- break;
- case 3:
- if (book.PublicationDate.ToString().ToUpper() == userInput.ToUpper())
- book.ShowInfo();
- break;
- case 4:
- if (book.Rating.ToString().ToUpper() == userInput.ToUpper())
- book.ShowInfo();
- break;
- }
- }
- }
- }
- }
- class Book
- {
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int PublicationDate { get; private set; }
- public int Rating { get; private set; }
- public Book(string title, string author, int publicationDate = 0, int rating = 0)
- {
- Title = title;
- Author = author;
- var curryYear = new DateTime();
- if (publicationDate >= 0 && publicationDate <= curryYear.Year)
- {
- PublicationDate = publicationDate;
- }
- PublicationDate = curryYear.Year;
- int minRating = 0;
- int maxRating = 5;
- if (rating >= minRating && rating <= maxRating)
- {
- Rating = rating;
- }
- }
- public void ShowInfo()
- {
- Console.WriteLine("Название: {0} | Автор: {1} | Год издания: {2} | Рейтинг: {3}", Title, Author, PublicationDate, Rating);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement