Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace Homework12
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- List<Book> books = new List<Book>
- {
- new Book("Title1", "Author1", 2005),
- new Book("Title2", "Author1", 2009),
- new Book("Title3", "Author2", 2013),
- new Book("Title4", "Author2", 1998),
- };
- Storage storage = new Storage(books);
- string userInput = null;
- bool isActive = true;
- while (isActive)
- {
- ChooseOption(userInput, storage, ref isActive);
- Console.ReadKey();
- Console.Clear();
- }
- }
- private static void ChooseOption(string input, Storage storage, ref bool isActive)
- {
- Console.WriteLine("Добавить книгу\n" +
- "Убрать книгу\n" +
- "Показать все книги\n" +
- "Показать книги по указанному параметру\n" +
- "Выход");
- input = Console.ReadLine();
- switch (input)
- {
- case "Добавить книгу":
- storage.AddBook();
- break;
- case "Убрать книгу":
- storage.RemoveBook();
- break;
- case "Показать все книги":
- storage.ShowBooks();
- break;
- case "Показать книги по указанному параметру":
- storage.ShowBooks(storage.SortBooks());
- break;
- case "Выход":
- isActive = false;
- break;
- default:
- throw new NullReferenceException();
- }
- }
- }
- class Book
- {
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int ReleaseDate { get; private set; }
- public Book(string title, string author, int releasaeDate)
- {
- Title = title;
- Author = author;
- ReleaseDate = releasaeDate;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"|Название: {Title}|Автор: {Author}|Год выпуска: {ReleaseDate}|");
- }
- }
- class Storage
- {
- private List<Book> _books;
- public Storage(List<Book> books = null)
- {
- _books = books;
- }
- public void AddBook()
- {
- string userTitle;
- string userAuthor;
- int userReleaseDate;
- Console.WriteLine("Введите название:");
- userTitle = Console.ReadLine();
- Console.WriteLine("Введите автора:");
- userAuthor = Console.ReadLine();
- Console.WriteLine("Введи год выпуска:");
- userReleaseDate = int.Parse(Console.ReadLine());
- Console.WriteLine("Новая книга добавлена!\n" +
- $"|Название: {userTitle}|Автор: {userAuthor}|Год выпуска: {userReleaseDate}|");
- _books.Add(new Book(userTitle, userAuthor, userReleaseDate));
- }
- public void RemoveBook()
- {
- ShowBooks();
- Console.Write("Номер книги, которую нужно убрать:");
- int number = int.Parse(Console.ReadLine());
- if (number >= 0 && number < _books.Count)
- _books.RemoveAt(number);
- else
- throw new ArgumentOutOfRangeException();
- }
- public void ShowBooks()
- {
- if (_books != null)
- foreach (var book in _books)
- book.ShowInfo();
- else
- throw new NullReferenceException();
- }
- public void ShowBooks(List<Book> books)
- {
- if (books != null)
- foreach (var book in books)
- book.ShowInfo();
- else
- throw new NullReferenceException();
- }
- public List<Book> SortBooks()
- {
- if (_books == null)
- throw new NullReferenceException();
- Console.WriteLine("По какому параметру сортировать?");
- string input = Console.ReadLine();
- switch (input)
- {
- case "по названию":
- return _books.Where(book => book.Title == Console.ReadLine()).OrderBy(book => book.Title).ToList();
- case "по автору":
- return _books.Where(book => book.Author == Console.ReadLine()).OrderBy(book => book.Author).ToList();
- case "по году выпуска":
- return _books.Where(book => book.ReleaseDate == int.Parse(Console.ReadLine())).OrderBy(book => book.ReleaseDate).ToList();
- default:
- return null;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment