Created
April 18, 2024 20:47
-
-
Save Dead-in-side/4bf1c6a362d4c0cb559b903226ff5cfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace IJunior | |
{ | |
public class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Bookcase bookcase = new Bookcase(); | |
bool isWork = true; | |
int userInput; | |
while (isWork) | |
{ | |
Console.WriteLine((int)ConsoleCommand.AddBook + ". Добавить книгу"); | |
Console.WriteLine((int)ConsoleCommand.RemoveBook + ". Убрать книгу"); | |
Console.WriteLine((int)ConsoleCommand.SearchBookByName + ". Поиск по названию"); | |
Console.WriteLine((int)ConsoleCommand.SearchBookByAuthorName + ". Поиск по автору"); | |
Console.WriteLine((int)ConsoleCommand.SearchBookByYearsOfIssue + ". Поиск по году выхода"); | |
Console.WriteLine((int)ConsoleCommand.ShowBooks + ". Показать все книги"); | |
Console.WriteLine((int)ConsoleCommand.Exit + ". Выход"); | |
if(int.TryParse(Console.ReadLine(), out userInput)) | |
{ | |
switch (userInput) | |
{ | |
case (int)ConsoleCommand.AddBook: | |
bookcase.AddBook(); | |
break; | |
case (int)ConsoleCommand.RemoveBook: | |
bookcase.RemoveBook(); | |
break; | |
case (int)ConsoleCommand.SearchBookByName: | |
bookcase.SearchBookByName(); | |
break; | |
case (int)ConsoleCommand.SearchBookByAuthorName: | |
bookcase.SearchBookByAuthorName(); | |
break; | |
case (int)ConsoleCommand.SearchBookByYearsOfIssue: | |
bookcase.SearchBookByYearsOfIssue(); | |
break; | |
case (int)ConsoleCommand.ShowBooks: | |
bookcase.ShowBooks(); | |
break; | |
case (int)ConsoleCommand.Exit: | |
isWork = false; | |
break; | |
default: | |
Console.WriteLine("Некорректный ввод"); | |
break; | |
} | |
} | |
Console.ReadLine(); | |
Console.Clear(); | |
} | |
} | |
} | |
public enum ConsoleCommand | |
{ | |
AddBook=1, | |
RemoveBook, | |
SearchBookByName, | |
SearchBookByAuthorName, | |
SearchBookByYearsOfIssue, | |
ShowBooks, | |
Exit | |
} | |
public class Bookcase | |
{ | |
private List<Book> _books = new List<Book>(); | |
private int id = 1; | |
public void AddBook() | |
{ | |
Console.Write("Введите название книги: "); | |
string bookName = Console.ReadLine(); | |
Console.Write("\nВведите автора книги: "); | |
string authorName = Console.ReadLine(); | |
Console.WriteLine("\nВведите год выхода книги: "); | |
if (int.TryParse(Console.ReadLine(), out int year)) | |
{ | |
Book book = new Book(id, bookName, authorName, year); | |
_books.Add(book); | |
id++; | |
} | |
} | |
public void RemoveBook() | |
{ | |
Console.WriteLine("Введите Id книги для удаления"); | |
if (int.TryParse(Console.ReadLine(), out int id)) | |
{ | |
Book foundBook = _books.Find(delegate (Book book) { return book.Id == id; }); | |
if (_books.Remove(foundBook)) | |
{ | |
Console.WriteLine("Книга удалена"); | |
} | |
else | |
{ | |
Console.WriteLine("Книга не найдена"); | |
} | |
} | |
} | |
public void ShowBooks() | |
{ | |
ShowResults(_books); | |
} | |
public void SearchBookByName() | |
{ | |
List<Book> results = new List<Book>(); | |
Console.WriteLine("Введите название книги"); | |
string name = Console.ReadLine(); | |
results = _books.FindAll(delegate (Book book) { return book.Name == name; }); | |
ShowResults(results); | |
} | |
public void SearchBookByAuthorName() | |
{ | |
List<Book> results = new List<Book>(); | |
Console.WriteLine("Введите имя автора книги"); | |
string name = Console.ReadLine(); | |
results = _books.FindAll(delegate (Book book) { return book.AuthorName == name; }); | |
ShowResults(results); | |
} | |
public void SearchBookByYearsOfIssue() | |
{ | |
List<Book> results = new List<Book>(); | |
Console.WriteLine("Введите год выпуска книги"); | |
if (int.TryParse(Console.ReadLine(), out int year)) | |
{ | |
results = _books.FindAll(delegate (Book book) { return book.YearOfIssue == year; }); | |
} | |
ShowResults(results); | |
} | |
private void ShowResults(List<Book> books) | |
{ | |
foreach (Book book in books) | |
{ | |
book.ShowInfo(); | |
} | |
} | |
} | |
public class Book | |
{ | |
public Book(int id, string name, string authorName, int yearOfIssue) | |
{ | |
Id = id; | |
Name = name; | |
AuthorName = authorName; | |
YearOfIssue = yearOfIssue; | |
} | |
public int Id { get; private set; } | |
public string Name { get; private set; } | |
public string AuthorName { get; private set; } | |
public int YearOfIssue { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Книга № {Id}: {Name}, напсанная {AuthorName} в {YearOfIssue}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment