Last active
June 16, 2025 12:20
-
-
Save Strelok78/23f51ba6b6cb58ac6cd986bb00025e99 to your computer and use it in GitHub Desktop.
Book Shell with search (by book params), add, remove options
This file contains hidden or 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
using System; | |
using System.Globalization; | |
using System.IO; | |
using iJuniorPractice; | |
namespace iJuniorPractice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BookShellView bookShellView = new BookShellView(new BookShell()); | |
bookShellView.Play(); | |
} | |
} | |
static class Utils | |
{ | |
public static int ReadInt(string message = "") | |
{ | |
int number; | |
string input; | |
Console.WriteLine(message); | |
input = Console.ReadLine(); | |
while (int.TryParse(input, out number) == false) | |
{ | |
Console.WriteLine("Incorrect input. Try again."); | |
input = Console.ReadLine(); | |
} | |
return number; | |
} | |
public static string ReadString(string message = "") | |
{ | |
string input; | |
Console.WriteLine(message); | |
input = Console.ReadLine(); | |
while (input == "") | |
{ | |
Console.WriteLine("Incorrect input, line is empty. Try again."); | |
input = Console.ReadLine(); | |
} | |
return input; | |
} | |
} | |
class BookShellView | |
{ | |
private const int CommandShowBooks = 1; | |
private const int CommandAddBook = 2; | |
private const int CommandRemoveBook = 3; | |
private const int CommanndByName = 4; | |
private const int CommanndByAuthor = 5; | |
private const int CommanndByYear = 6; | |
private const int CommandExit = 7; | |
private BookShell _bookShell; | |
public BookShellView(BookShell bookShell) | |
{ | |
_bookShell = bookShell; | |
} | |
public void Play() | |
{ | |
bool _isWork = true; | |
int input; | |
_bookShell = new BookShell(); | |
while (_isWork) | |
{ | |
ShowMenu(); | |
input = Utils.ReadInt("Please enter command: "); | |
switch (input) | |
{ | |
case CommandShowBooks: | |
_bookShell.ShowAllBooks(); | |
break; | |
case CommandAddBook: | |
_bookShell.AddBook(); | |
break; | |
case CommandRemoveBook: | |
_bookShell.RemoveBook(); | |
break; | |
case CommanndByName: | |
_bookShell.SearchByName(); | |
break; | |
case CommanndByAuthor: | |
_bookShell.SearchByAuthor(); | |
break; | |
case CommanndByYear: | |
_bookShell.SearchByPublishYear(); | |
break; | |
case CommandExit: | |
_isWork = false; | |
CloseApp(); | |
break; | |
default: | |
Console.WriteLine("Incorrect input."); | |
break; | |
} | |
Console.WriteLine("Press any key to continue..."); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void ShowMenu() | |
{ | |
Console.WriteLine($"Main Menu:"); | |
Console.WriteLine($"Show All Books - {CommandShowBooks}"); | |
Console.WriteLine($"Add Book - {CommandAddBook}"); | |
Console.WriteLine($"Remove Book - {CommandRemoveBook}"); | |
Console.WriteLine($"Search by Title Name - {CommanndByName}"); | |
Console.WriteLine($"Search by Author - {CommanndByAuthor}"); | |
Console.WriteLine($"Search by Publish Year - {CommanndByYear}"); | |
} | |
private void CloseApp() | |
{ | |
Console.WriteLine("Turning off application..."); | |
} | |
} | |
class BookShell | |
{ | |
private List<Book> _books; | |
private int _lastBookId; | |
public BookShell() | |
{ | |
_lastBookId = 0; | |
_books = new List<Book>(); | |
} | |
public void SearchByName() | |
{ | |
bool isFound = false; | |
string name = Utils.ReadString("Please enter book name: "); | |
foreach (var book in _books) | |
{ | |
if (book.Name.Contains(name)) | |
{ | |
isFound = true; | |
Console.WriteLine($"{book.GetInfo()}"); | |
} | |
} | |
if (isFound == false) | |
{ | |
Console.WriteLine("Books not found."); | |
} | |
} | |
public void SearchByAuthor() | |
{ | |
bool isFound = false; | |
string author = Utils.ReadString("Please enter book author: "); | |
foreach (var book in _books) | |
{ | |
if (book.Author.Contains(author)) | |
{ | |
isFound = true; | |
Console.WriteLine($"{book.GetInfo()}"); | |
} | |
} | |
if (isFound == false) | |
{ | |
Console.WriteLine("Books not found."); | |
} | |
} | |
public void SearchByPublishYear() | |
{ | |
bool isFound = false; | |
int year = Utils.ReadInt("Please enter book publish year: "); | |
foreach (var book in _books) | |
{ | |
if (book.PublishYear == year) | |
{ | |
isFound = true; | |
Console.WriteLine($"{book.GetInfo()}"); | |
} | |
} | |
if (isFound == false) | |
{ | |
Console.WriteLine("Books not found."); | |
} | |
} | |
public void AddBook() | |
{ | |
Book newBook = CreateBook(); | |
_books.Add(newBook); | |
} | |
public void RemoveBook() | |
{ | |
bool isFound = false; | |
int bookId = Utils.ReadInt("Please enter book ID: "); | |
foreach (var book in _books) | |
{ | |
if (book.Id == bookId) | |
{ | |
isFound = true; | |
_books.Remove(book); | |
break; | |
} | |
} | |
if(isFound == false) | |
{ | |
Console.WriteLine("Book not found."); | |
} | |
} | |
public void ShowAllBooks() | |
{ | |
if (_books.Count == 0) | |
{ | |
Console.WriteLine("No books found."); | |
} | |
else | |
{ | |
foreach (var book in _books) | |
{ | |
Console.WriteLine($"{book.GetInfo()}"); | |
} | |
} | |
} | |
private Book CreateBook() | |
{ | |
string name = Utils.ReadString("Please enter the name of the book: "); | |
string author = Utils.ReadString("Please enter the author: "); | |
int publishYear = Utils.ReadInt("Publish year: "); | |
return new Book(_lastBookId++, name, author, publishYear); | |
} | |
} | |
class Book | |
{ | |
public Book(int id, string name, string author, int publishYear) | |
{ | |
Id = id; | |
Name = name; | |
Author = author; | |
PublishYear = publishYear; | |
} | |
public int Id { get; private set; } | |
public string Name { get; private set; } | |
public string Author { get; private set; } | |
public int PublishYear { get; private set; } | |
public string GetInfo() | |
{ | |
return $"{Id} - {Name}, {Author}, {PublishYear}"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment