Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Library
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Library library = new Library();
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine("1.Добавить книгу.\n2.Убрать книгу.\n3.Показать все книги.\n4.Показать книгу по параметру.\n5.Выйти.");
- switch (Console.ReadLine())
- {
- case "1":
- library.AddBook();
- break;
- case "2":
- library.DeleteBook();
- break;
- case "3":
- library.ShowAllBooks();
- break;
- case "4":
- library.SelectOption();
- break;
- case "5":
- isWork = false;
- break;
- }
- Console.WriteLine("\nЧтобы продолжить нажмите любую кнопку");
- Console.ReadLine();
- Console.Clear();
- }
- }
- }
- class Library
- {
- private List<Books> _book = new List<Books>();
- public void AddBook()
- {
- Console.WriteLine("Введите Название книги: ");
- string title = Console.ReadLine();
- Console.WriteLine("Введите Автора книги: ");
- string autor = Console.ReadLine();
- Console.WriteLine("Введите Жанр книги: ");
- string genre = Console.ReadLine();
- Console.WriteLine("Введите Год книги: ");
- int year;
- if (int.TryParse(Console.ReadLine(), out year) == true)
- _book.Add(new Books(title, autor, genre, year));
- }
- public void ShowAllBooks()
- {
- for (int i = 0; i < _book.Count; i++)
- {
- ShowInfo(i);
- }
- }
- public void DeleteBook()
- {
- for (int i = 0; i < _book.Count; i++)
- {
- Console.WriteLine($"Номер книги: {_book[i].Number}.\nИмя: {_book[i].Title}.\n");
- }
- Console.Write("Введите номер книги: ");
- int number;
- if (int.TryParse(Console.ReadLine(), out number) == true && number < _book.Count && number >= 0)
- _book.RemoveAt(number - 1);
- }
- public void SelectOption()
- {
- Console.WriteLine("Выбирите параметр.");
- Console.WriteLine("1.Название.\n2.Автор.\n3.Жанр.\nГод.");
- switch (Console.ReadLine())
- {
- case "1":
- NameSearch();
- break;
- case "2":
- SearchAuthor();
- break;
- case "3":
- SearchGenre();
- break;
- case "4":
- SearchYear();
- break;
- }
- }
- private void NameSearch()
- {
- string title = Console.ReadLine();
- for (int i = 0; i < _book.Count; i++)
- {
- if (title == _book[i].Title)
- {
- ShowInfo(i);
- }
- }
- }
- private void SearchAuthor()
- {
- string autor = Console.ReadLine();
- for (int i = 0; i < _book.Count; i++)
- {
- if (autor == _book[i].Author)
- {
- ShowInfo(i);
- }
- }
- }
- private void SearchGenre()
- {
- string genre = Console.ReadLine();
- for (int i = 0; i < _book.Count; i++)
- {
- if (genre == _book[i].Genre)
- {
- ShowInfo(i);
- }
- }
- }
- private void SearchYear()
- {
- int year;
- if (int.TryParse(Console.ReadLine(), out year) == true)
- for (int i = 0; i < _book.Count; i++)
- {
- if (year == _book[i].Year)
- {
- ShowInfo(i);
- }
- }
- }
- private void ShowInfo(int number)
- {
- Console.WriteLine($"Номер книги: {_book[number].Number}.\nИмя: {_book[number].Title}.\nАвтор: {_book[number].Author}.\nЖанр: {_book[number].Genre}.\nГод: {_book[number].Year}.\n");
- }
- }
- class Books
- {
- private static int _number = 0;
- public int Number { get; private set; }
- public string Title { get; private set; }
- public string Author { get; private set; }
- public string Genre { get; private set; }
- public int Year { get; private set; }
- public Books(string title, string autor, string genre, int year)
- {
- _number++;
- Number = _number;
- Title = title;
- Author = autor;
- Genre = genre;
- Year = year;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement