Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- internal class Program
- {
- static void Main(string[] args)
- {
- Book book = new Book();
- List<Book> library = new List<Book>();
- book.WorkingProgram();
- }
- class Book
- {
- public List<Book> library = new List<Book>();
- public Book(string bookTitle = " ", string authorName = " ", int yearOfPublishing = 0)
- {
- BookTitle = bookTitle;
- AuthorName = authorName;
- YearOfPublishing = yearOfPublishing;
- }
- public string BookTitle { get; private set; }
- public string AuthorName { get; private set; }
- public int YearOfPublishing { get; private set; }
- public void WorkingProgram()
- {
- const string AddBookCommand = "1";
- const string FindBookCommand = "2";
- const string DeleteBookCommand = "3";
- const string ShowAllBooksCommand = "4";
- const string ExitCommand = "5";
- Book book = new Book();
- bool isWorking = true;
- Console.Write("Добавте книгу: ");
- book.AddBook(library);
- Console.Clear();
- while (isWorking)
- {
- Console.WriteLine($"Хранилище книг\n\n\n" +
- $"Введите {AddBookCommand} если хотите добавить книгу.\n" +
- $"Введите {FindBookCommand} если хотите найти книгу.\n" +
- $"Введите {DeleteBookCommand} если хотите удалить книгу.\n" +
- $"Введите {ShowAllBooksCommand} если хотите увидеть список книг.\n" +
- $"Введите {ExitCommand} если хотите выйти из программы.\n\n" +
- $"Ввод: ");
- string? userInput = Console.ReadLine();
- switch (userInput)
- {
- case AddBookCommand:
- book.AddBook(library);
- break;
- case FindBookCommand:
- book.FindBook(library);
- break;
- case ShowAllBooksCommand:
- book.ShowAllBooks(library);
- break;
- case DeleteBookCommand:
- book.DeleteBook(library);
- break;
- case ExitCommand:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Введена неверная команда.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- public void AddBook(List<Book> _library)
- {
- bool isWorking = true;
- string? userInput;
- Console.WriteLine("Введите название книги: ");
- userInput = Console.ReadLine();
- BookTitle = userInput;
- Console.WriteLine("Введите имя автора: ");
- userInput = Console.ReadLine();
- AuthorName = userInput;
- Console.WriteLine("Введите год издания: ");
- bool isSucces = int.TryParse(Console.ReadLine(), out int result);
- while (isWorking == true)
- {
- if (isSucces == true)
- {
- YearOfPublishing = result;
- isWorking = false;
- }
- else
- {
- Console.WriteLine("Нужно ввести число.");
- }
- }
- Book newBook = new Book(BookTitle, AuthorName, YearOfPublishing);
- _library.Add(newBook);
- }
- public void ShowAllBooks(List<Book> _library)
- {
- foreach (var book in _library)
- {
- Console.WriteLine($"{book.BookTitle} {book.AuthorName} {book.YearOfPublishing}");
- }
- }
- public void FindBook(List<Book> _library)
- {
- Book book = new Book();
- Console.WriteLine("Введите название книги или имя автора для поиска: ");
- string? userInput = Console.ReadLine();
- _library.FindAll(item => item.AuthorName == userInput);
- _library.FindAll(item => item.AuthorName == userInput);
- List<Book> foundBooks = _library;
- Console.WriteLine("Список найденых книг:");
- book.ShowAllBooks(foundBooks);
- }
- public void DeleteBook(List<Book> _library)
- {
- Book book = new Book();
- Console.WriteLine("Введите название книги или имя автора для удаления: ");
- string? userInput = Console.ReadLine();
- _library.RemoveAll(item => item.AuthorName == userInput);
- _library.RemoveAll(item => item.AuthorName == userInput);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement