Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- ConsoleInputHandler controller = new ConsoleInputHandler();
- BookStorage bookStorage = new BookStorage();
- Console.CursorVisible = false;
- controller.ShowMenu(bookStorage);
- }
- }
- }
- abstract class ConsoleHandler
- {
- protected const ConsoleKey MenuUp = ConsoleKey.UpArrow;
- protected const ConsoleKey MenuDown = ConsoleKey.DownArrow;
- protected const ConsoleKey MenuSelect = ConsoleKey.Enter;
- protected const int CommandAdd = 1;
- protected const int CommandRemove = 2;
- protected const int CommandShow = 3;
- protected const int CommandExit = 4;
- protected const int CommandFindAll = 1;
- protected const int CommandFindByTitle = 2;
- protected const int CommandFindByAuthor = 3;
- protected const int CommandFindByPublishingYear = 4;
- }
- class ConsoleInputHandler : ConsoleHandler
- {
- public void ShowMenu(BookStorage bookStorage)
- {
- bool isRunning = true;
- while (isRunning)
- {
- switch (SetSelection())
- {
- case CommandAdd:
- bookStorage.AddBook();
- break;
- case CommandRemove:
- bookStorage.RemoveBook();
- break;
- case CommandShow:
- FindMenu(bookStorage);
- break;
- case CommandExit:
- isRunning = false;
- break;
- }
- }
- }
- public void FindMenu(BookStorage bookStorage)
- {
- switch (SetShowSelection())
- {
- case CommandFindAll:
- bookStorage.FindAll();
- break;
- case CommandFindByTitle:
- bookStorage.FindByTitle();
- break;
- case CommandFindByAuthor:
- bookStorage.FindByAuthor();
- break;
- case CommandFindByPublishingYear:
- bookStorage.FindByPublishingYear();
- break;
- }
- }
- private int SetSelection()
- {
- bool isSelected = false;
- int selectOption = 1;
- while (!isSelected)
- {
- int leftPosition = 0;
- int topPosition = 1;
- string colorSet = "\u001b[32m";
- string colorReset = "\u001b[0m";
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine($"" +
- $"{colorReset}" +
- $"Use \"UpArrow\" and \"DownArrow\" to select, \"Enter\" to confirm\n" +
- $"\n{(selectOption == CommandAdd ? colorSet + CommandAdd : colorReset + ' ')} Add book" +
- $"\n{(selectOption == CommandRemove ? colorSet + CommandRemove : colorReset + ' ')} Remove book" +
- $"\n{(selectOption == CommandShow ? colorSet + CommandShow : colorReset + ' ')} Menu ShowBy" +
- $"\n{(selectOption == CommandExit ? colorSet + CommandExit : colorReset + ' ')} Exit" +
- $"{colorReset}");
- selectOption = Select(selectOption, CommandAdd, CommandExit, ref isSelected);
- }
- return selectOption;
- }
- private int SetShowSelection()
- {
- bool isSelected = false;
- int selectOption = 1;
- while (!isSelected)
- {
- int leftPosition = 0;
- int topPosition = 1;
- string colorSet = "\u001b[32m";
- string colorReset = "\u001b[0m";
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine($"" +
- $"{colorReset}" +
- $"Use \"UpArrow\" and \"DownArrow\" to select, \"Enter\" to confirm\n" +
- $"\n{(selectOption == CommandFindAll ? colorSet + CommandFindAll : colorReset + ' ')} Show All book" +
- $"\n{(selectOption == CommandFindByTitle ? colorSet + CommandFindByTitle : colorReset + ' ')} Find By Title" +
- $"\n{(selectOption == CommandFindByAuthor ? colorSet + CommandFindByAuthor : colorReset + ' ')} Find By Author" +
- $"\n{(selectOption == CommandFindByPublishingYear ? colorSet + CommandFindByPublishingYear : colorReset + ' ')} Find By Publishing Year" +
- $"{colorReset}");
- selectOption = Select(selectOption, CommandFindAll, CommandFindByPublishingYear, ref isSelected);
- }
- return selectOption;
- }
- private int Select(int selectOption, int minMenuValue, int maxMenuValue, ref bool isSelected)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case MenuUp:
- selectOption = (selectOption <= minMenuValue ? maxMenuValue : selectOption - 1);
- break;
- case MenuDown:
- selectOption = (selectOption >= maxMenuValue ? minMenuValue : selectOption + 1);
- break;
- case MenuSelect:
- isSelected = true;
- break;
- }
- return selectOption;
- }
- }
- class BookStorage
- {
- private List<Book> _book = new List<Book>();
- public void AddBook()
- {
- List<int> publishingYear = new List<int>();
- Console.CursorVisible = true;
- Console.Write("\nEnter author: ");
- string author = Console.ReadLine();
- Console.Write("\nEnter the title: ");
- string title = Console.ReadLine();
- Console.Write("\nEnter publishing year: ");
- int.TryParse(Console.ReadLine(), out int year);
- Console.CursorVisible = false;
- publishingYear.Add(year);
- if (TryGetBook2(title, author, year, out Book book) && !book.PublishingYear.Contains(year))
- {
- book.PublishingYear.Add(year);
- book.PublishingYear.Sort();
- Console.WriteLine($"\u001b[34mBook added: {book.Author} {book.Title} ({year})\u001b[0m");
- }
- else if (!TryGetBook1(title, author, year, out Book book1))
- {
- if (title != "" && author != "" && year != 0)
- {
- _book.Add(new Book(title, author, publishingYear));
- Console.WriteLine($"\u001b[34mBook added: {author} {title} ({year})\u001b[0m");
- }
- else
- {
- Console.WriteLine("\u001b[34mAll fields must be non - empty\u001b[0m");
- }
- }
- else
- {
- Console.WriteLine("\u001b[34m Already have this book \u001b[0m");
- Console.ReadKey();
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- public void RemoveBook()
- {
- if (Count != 0)
- {
- Console.CursorVisible = true;
- Console.WriteLine("\nWhich book needs to be removed?");
- Console.Write("\nEnter author: ");
- string author = Console.ReadLine();
- Console.Write("\nEnter the title: ");
- string title = Console.ReadLine();
- Console.Write("\nEnter publishing year: ");
- int.TryParse(Console.ReadLine(), out int year);
- Console.CursorVisible = false;
- if (TryGetBook1(title, author, year, out Book book) && book.PublishingYear.Count > 1)
- {
- book.PublishingYear.Remove(year);
- Console.WriteLine($"\u001b[34mBook removed: {author} {title} ({year})\u001b[0m");
- }
- else if (TryGetBook1(title, author, year, out Book book1) && book.PublishingYear.Count == 1)
- {
- _book.Remove(book1);
- Console.WriteLine($"\u001b[34mBook removed: {author} {title} ({year})\u001b[0m");
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- public void FindAll()
- {
- if (_book.Count() != 0)
- {
- for (int i = 0; i < Count; i++)
- {
- Console.Write($"{i + 1}. {_book[i].Author} {_book[i].Title} " +
- $"(");
- for (int j = 0; j < _book[i].PublishingYear.Count; j++)
- {
- Console.Write($"{_book[i].PublishingYear[j]}");
- if (j < _book[i].PublishingYear.Count - 1)
- Console.Write(", ");
- }
- Console.WriteLine(")");
- }
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- public void FindByTitle()
- {
- Console.CursorVisible = true;
- Console.Write("Enter the title of the book you want to find: ");
- string userInput = Console.ReadLine().ToUpper();
- Console.CursorVisible = false;
- var selectedBook = _book.Where(books => books.Title.ToUpper() == userInput);
- if (selectedBook.Count() != 0)
- {
- foreach (var book in selectedBook)
- {
- Console.Write($"{book.Author} {book.Title} " +
- $"(");
- for (int j = 0; j < book.PublishingYear.Count; j++)
- {
- Console.Write($"{book.PublishingYear[j]}");
- if (j < book.PublishingYear.Count - 1)
- Console.Write(", ");
- }
- Console.WriteLine(")");
- }
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- public void FindByAuthor()
- {
- Console.CursorVisible = true;
- Console.Write("Enter the author of the book you want to find: ");
- string userInput = Console.ReadLine().ToUpper();
- Console.CursorVisible = false;
- var selectedBook = _book.Where(books => books.Author.ToUpper() == userInput);
- if (selectedBook.Count() != 0)
- {
- foreach (var book in selectedBook)
- {
- Console.Write($"{book.Author} {book.Title} " +
- $"(");
- for (int j = 0; j < book.PublishingYear.Count; j++)
- {
- Console.Write($"{book.PublishingYear[j]}");
- if (j < book.PublishingYear.Count - 1)
- Console.Write(", ");
- }
- Console.WriteLine(")");
- }
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- public void FindByPublishingYear()
- {
- Console.CursorVisible = true;
- Console.Write("Enter the year of the book you want to find: ");
- int.TryParse(Console.ReadLine(), out int year);
- Console.CursorVisible = false;
- var selectedBook = _book.Where(books => books.PublishingYear.Contains(year));
- if (selectedBook.Count() != 0)
- {
- foreach (var book in selectedBook)
- {
- Console.Write($"{book.Author} {book.Title} ({year})\n");
- }
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- private int Count => _book.Count;
- private bool TryGetBook1(string title, string author, int year, out Book book)
- {
- book = null;
- if (title != "" && author != "" && year != 0)
- {
- var selectedAuthor = _book.Where(books => books.Author.ToUpper() == author.ToUpper());
- var selectedTitle = selectedAuthor.Where(books => books.Title.ToUpper() == title.ToUpper());
- var selectedYear = selectedTitle.Where(books => books.PublishingYear.Contains(year));
- foreach (var books in selectedYear)
- {
- book = books;
- }
- }
- if (book != null)
- {
- return true;
- }
- return false;
- }
- private bool TryGetBook2(string title, string author, int year, out Book book)
- {
- book = null;
- if (title != "" && author != "" && year != 0)
- {
- var selectedAuthor = _book.Where(books => books.Author.ToUpper() == author.ToUpper());
- var selectedTitle = selectedAuthor.Where(books => books.Title.ToUpper() == title.ToUpper());
- foreach (var books in selectedTitle)
- {
- book = books;
- }
- }
- if (book != null)
- {
- return true;
- }
- return false;
- }
- }
- class Book
- {
- public Book(string title, string author, List<int> publishingYear)
- {
- Title = title;
- Author = author;
- PublishingYear = publishingYear;
- }
- public string Title { get; private set; }
- public string Author { get; private set; }
- public List<int> PublishingYear { get; private set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement