Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using static Classes.ConstantsKeeper;
- namespace Classes
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Administrator administrator = new Administrator();
- BookStorage bookStorage = new BookStorage();
- Console.CursorVisible = false;
- administrator.WorkMenu(bookStorage);
- }
- }
- static class ConstantsKeeper
- {
- public const ConsoleKey MenuUp = ConsoleKey.UpArrow;
- public const ConsoleKey MenuDown = ConsoleKey.DownArrow;
- public const ConsoleKey MenuSelect = ConsoleKey.Enter;
- public const int CommandAdd = 1;
- public const int CommandRemove = 2;
- public const int CommandFindAll = 3;
- public const int CommandFindByTitle = 4;
- public const int CommandFindByAuthor = 5;
- public const int CommandFindByPublishingYear = 6;
- public const int CommandExit = 7;
- }
- class Administrator
- {
- public void WorkMenu(BookStorage bookStorage)
- {
- bool isRunning = true;
- while (isRunning)
- {
- switch (GetWorkMenuSelection())
- {
- case CommandAdd:
- bookStorage.AddBook();
- break;
- case CommandRemove:
- bookStorage.RemoveBook();
- break;
- case CommandFindAll:
- bookStorage.FindAll();
- break;
- case CommandFindByTitle:
- bookStorage.FindByTitle();
- break;
- case CommandFindByAuthor:
- bookStorage.FindByAuthor();
- break;
- case CommandFindByPublishingYear:
- bookStorage.FindByPublishingYear();
- break;
- case CommandExit:
- isRunning = false;
- break;
- }
- }
- }
- private int GetWorkMenuSelection()
- {
- bool isSelected = false;
- int selectOption = 1;
- while (isSelected == false)
- {
- 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 == 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" +
- $"\n{(selectOption == CommandExit ? colorSet + CommandExit : colorReset + ' ')} Exit" +
- $"{colorReset}");
- selectOption = Select(selectOption, CommandAdd, CommandExit, out isSelected);
- }
- return selectOption;
- }
- private int Select(int selectOption, int minMenuValue, int maxMenuValue, out bool isSelected)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- isSelected = false;
- 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> _books = new List<Book>();
- private int Count => _books.Count;
- public void AddBook()
- {
- 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;
- if (title != "" && author != "" && year != 0)
- {
- _books.Add(new Book(title, author, year));
- Console.WriteLine($"\u001b[34mBook added: Author {author} - Title: {title} ({year})\u001b[0m");
- }
- else
- {
- Console.WriteLine("\u001b[34mAll fields must be non - empty\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- public void RemoveBook()
- {
- if (Count > 0)
- {
- Console.CursorVisible = true;
- Console.WriteLine("\nBook with which index should be deleted?");
- Console.Write("\nEnter index: ");
- int.TryParse(Console.ReadLine(), out int index);
- Console.CursorVisible = false;
- if (index > 0 && index <= Count)
- {
- index--;
- Console.WriteLine($"\u001b[34mBook removed: Author {_books[index].Author} - Title: {_books[index].Title} ({_books[index].PublishingYear})\u001b[0m");
- _books.RemoveAt(index);
- }
- else
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- public void FindAll()
- {
- if (Count > 0)
- {
- for (int i = 0; i < Count; i++)
- {
- Console.Write($"{i + 1}. Author {_books[i].Author} - Title: {_books[i].Title} ({_books[i].PublishingYear})\n");
- }
- }
- 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;
- bool hasValue = false;
- foreach (var book in _books)
- {
- if (book.Title.ToUpper() == userInput)
- {
- PrintBookOptions(book);
- hasValue = true;
- }
- }
- if (hasValue == false)
- {
- 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;
- bool hasValue = false;
- foreach (var book in _books)
- {
- if (book.Author.ToUpper() == userInput)
- {
- PrintBookOptions(book);
- hasValue = true;
- }
- }
- if (hasValue == false)
- {
- 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;
- bool hasValue = false;
- foreach (var book in _books)
- {
- if (book.PublishingYear == year)
- {
- PrintBookOptions(book);
- hasValue = true;
- }
- }
- if (hasValue == false)
- {
- Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
- }
- Console.WriteLine("Press anything ...");
- Console.ReadKey();
- Console.Clear();
- }
- private void PrintBookOptions(Book book)
- {
- Console.Write($"Author {book.Author} - Title: {book.Title} ({book.PublishingYear})\n");
- }
- }
- class Book
- {
- public Book(string title, string author, int publishingYear)
- {
- Title = title;
- Author = author;
- PublishingYear = publishingYear;
- }
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int PublishingYear { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement