Advertisement
ZhongNi

Book storage

Apr 23rd, 2024 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Classes
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             ConsoleInputHandler controller = new ConsoleInputHandler();
  12.             BookStorage bookStorage = new BookStorage();
  13.             Console.CursorVisible = false;
  14.             controller.ShowMenu(bookStorage);
  15.         }
  16.     }
  17. }
  18.  
  19. abstract class ConsoleHandler
  20. {
  21.     protected const ConsoleKey MenuUp = ConsoleKey.UpArrow;
  22.     protected const ConsoleKey MenuDown = ConsoleKey.DownArrow;
  23.     protected const ConsoleKey MenuSelect = ConsoleKey.Enter;
  24.  
  25.     protected const int CommandAdd = 1;
  26.     protected const int CommandRemove = 2;
  27.     protected const int CommandShow = 3;
  28.     protected const int CommandExit = 4;
  29.  
  30.     protected const int CommandFindAll = 1;
  31.     protected const int CommandFindByTitle = 2;
  32.     protected const int CommandFindByAuthor = 3;
  33.     protected const int CommandFindByPublishingYear = 4;
  34. }
  35.  
  36. class ConsoleInputHandler : ConsoleHandler
  37. {
  38.     public void ShowMenu(BookStorage bookStorage)
  39.     {
  40.         bool isRunning = true;
  41.  
  42.         while (isRunning)
  43.         {
  44.             switch (SetSelection())
  45.             {
  46.                 case CommandAdd:
  47.                     bookStorage.AddBook();
  48.                     break;
  49.  
  50.                 case CommandRemove:
  51.                     bookStorage.RemoveBook();
  52.                     break;
  53.  
  54.                 case CommandShow:
  55.                     FindMenu(bookStorage);
  56.                     break;
  57.  
  58.                 case CommandExit:
  59.                     isRunning = false;
  60.                     break;
  61.             }
  62.         }
  63.     }
  64.  
  65.     public void FindMenu(BookStorage bookStorage)
  66.     {
  67.         switch (SetShowSelection())
  68.         {
  69.             case CommandFindAll:
  70.                 bookStorage.FindAll();
  71.                 break;
  72.  
  73.             case CommandFindByTitle:
  74.                 bookStorage.FindByTitle();
  75.                 break;
  76.  
  77.             case CommandFindByAuthor:
  78.                 bookStorage.FindByAuthor();
  79.                 break;
  80.  
  81.             case CommandFindByPublishingYear:
  82.                 bookStorage.FindByPublishingYear();
  83.                 break;
  84.         }
  85.     }
  86.  
  87.     private int SetSelection()
  88.     {
  89.         bool isSelected = false;
  90.         int selectOption = 1;
  91.  
  92.         while (!isSelected)
  93.         {
  94.             int leftPosition = 0;
  95.             int topPosition = 1;
  96.             string colorSet = "\u001b[32m";
  97.             string colorReset = "\u001b[0m";
  98.  
  99.             Console.SetCursorPosition(leftPosition, topPosition);
  100.  
  101.             Console.WriteLine($"" +
  102.                 $"{colorReset}" +
  103.                 $"Use \"UpArrow\" and \"DownArrow\" to select, \"Enter\" to confirm\n" +
  104.                 $"\n{(selectOption == CommandAdd ? colorSet + CommandAdd : colorReset + ' ')} Add book" +
  105.                 $"\n{(selectOption == CommandRemove ? colorSet + CommandRemove : colorReset + ' ')} Remove book" +
  106.                 $"\n{(selectOption == CommandShow ? colorSet + CommandShow : colorReset + ' ')} Menu ShowBy" +
  107.                 $"\n{(selectOption == CommandExit ? colorSet + CommandExit : colorReset + ' ')} Exit" +
  108.                 $"{colorReset}");
  109.  
  110.             selectOption = Select(selectOption, CommandAdd, CommandExit, ref isSelected);
  111.         }
  112.  
  113.         return selectOption;
  114.     }
  115.  
  116.     private int SetShowSelection()
  117.     {
  118.         bool isSelected = false;
  119.         int selectOption = 1;
  120.        
  121.         while (!isSelected)
  122.         {
  123.             int leftPosition = 0;
  124.             int topPosition = 1;
  125.             string colorSet = "\u001b[32m";
  126.             string colorReset = "\u001b[0m";
  127.  
  128.             Console.SetCursorPosition(leftPosition, topPosition);
  129.  
  130.             Console.WriteLine($"" +
  131.                 $"{colorReset}" +
  132.                 $"Use \"UpArrow\" and \"DownArrow\" to select, \"Enter\" to confirm\n" +
  133.                 $"\n{(selectOption == CommandFindAll ? colorSet + CommandFindAll : colorReset + ' ')} Show All book" +
  134.                 $"\n{(selectOption == CommandFindByTitle ? colorSet + CommandFindByTitle : colorReset + ' ')} Find By Title" +
  135.                 $"\n{(selectOption == CommandFindByAuthor ? colorSet + CommandFindByAuthor : colorReset + ' ')} Find By Author" +
  136.                 $"\n{(selectOption == CommandFindByPublishingYear ? colorSet + CommandFindByPublishingYear : colorReset + ' ')} Find By Publishing Year" +
  137.                 $"{colorReset}");
  138.  
  139.             selectOption = Select(selectOption, CommandFindAll, CommandFindByPublishingYear, ref isSelected);
  140.         }
  141.  
  142.         return selectOption;
  143.     }
  144.  
  145.     private int Select(int selectOption, int minMenuValue, int maxMenuValue, ref bool isSelected)
  146.     {
  147.         ConsoleKeyInfo key = Console.ReadKey(true);
  148.  
  149.         switch (key.Key)
  150.         {
  151.             case MenuUp:
  152.                 selectOption = (selectOption <= minMenuValue ? maxMenuValue : selectOption - 1);
  153.                 break;
  154.  
  155.             case MenuDown:
  156.                 selectOption = (selectOption >= maxMenuValue ? minMenuValue : selectOption + 1);
  157.                 break;
  158.  
  159.             case MenuSelect:
  160.                 isSelected = true;
  161.                 break;
  162.         }
  163.  
  164.         return selectOption;
  165.     }
  166. }
  167.  
  168. class BookStorage
  169. {
  170.     private List<Book> _book = new List<Book>();
  171.  
  172.     public void AddBook()
  173.     {
  174.         List<int> publishingYear = new List<int>();
  175.  
  176.         Console.CursorVisible = true;
  177.         Console.Write("\nEnter author: ");
  178.         string author = Console.ReadLine();
  179.         Console.Write("\nEnter the title: ");
  180.         string title = Console.ReadLine();
  181.         Console.Write("\nEnter publishing year: ");
  182.         int.TryParse(Console.ReadLine(), out int year);
  183.         Console.CursorVisible = false;
  184.  
  185.         publishingYear.Add(year);
  186.  
  187.         if (TryGetBook2(title, author, year, out Book book) && !book.PublishingYear.Contains(year))
  188.         {
  189.             book.PublishingYear.Add(year);
  190.             book.PublishingYear.Sort();
  191.             Console.WriteLine($"\u001b[34mBook added: {book.Author} {book.Title} ({year})\u001b[0m");
  192.         }
  193.         else if (!TryGetBook1(title, author, year, out Book book1))
  194.         {
  195.             if (title != "" && author != "" && year != 0)
  196.             {
  197.                 _book.Add(new Book(title, author, publishingYear));
  198.                 Console.WriteLine($"\u001b[34mBook added: {author} {title} ({year})\u001b[0m");
  199.             }
  200.             else
  201.             {
  202.                 Console.WriteLine("\u001b[34mAll fields must be non - empty\u001b[0m");
  203.             }
  204.         }
  205.         else
  206.         {
  207.             Console.WriteLine("\u001b[34m Already have this book \u001b[0m");
  208.             Console.ReadKey();
  209.         }
  210.  
  211.         Console.WriteLine("Press anything ...");
  212.         Console.ReadKey();
  213.         Console.Clear();
  214.     }
  215.  
  216.     public void RemoveBook()
  217.     {
  218.         if (Count != 0)
  219.         {
  220.             Console.CursorVisible = true;
  221.             Console.WriteLine("\nWhich book needs to be removed?");
  222.             Console.Write("\nEnter author: ");
  223.             string author = Console.ReadLine();
  224.             Console.Write("\nEnter the title: ");
  225.             string title = Console.ReadLine();
  226.             Console.Write("\nEnter publishing year: ");
  227.             int.TryParse(Console.ReadLine(), out int year);
  228.             Console.CursorVisible = false;
  229.  
  230.             if (TryGetBook1(title, author, year, out Book book) && book.PublishingYear.Count > 1)
  231.             {
  232.                 book.PublishingYear.Remove(year);
  233.                 Console.WriteLine($"\u001b[34mBook removed: {author} {title} ({year})\u001b[0m");
  234.             }
  235.             else if (TryGetBook1(title, author, year, out Book book1) && book.PublishingYear.Count == 1)
  236.             {
  237.                 _book.Remove(book1);
  238.                 Console.WriteLine($"\u001b[34mBook removed: {author} {title} ({year})\u001b[0m");
  239.             }
  240.             else
  241.             {
  242.                 Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
  243.             }
  244.  
  245.             Console.WriteLine("Press anything ...");
  246.             Console.ReadKey();
  247.             Console.Clear();
  248.         }
  249.     }
  250.  
  251.     public void FindAll()
  252.     {
  253.         if (_book.Count() != 0)
  254.         {
  255.             for (int i = 0; i < Count; i++)
  256.             {
  257.                 Console.Write($"{i + 1}. {_book[i].Author} {_book[i].Title} " +
  258.                     $"(");
  259.  
  260.                 for (int j = 0; j < _book[i].PublishingYear.Count; j++)
  261.                 {
  262.                     Console.Write($"{_book[i].PublishingYear[j]}");
  263.  
  264.                     if (j < _book[i].PublishingYear.Count - 1)
  265.                         Console.Write(", ");
  266.                 }
  267.  
  268.                 Console.WriteLine(")");
  269.             }
  270.         }
  271.         else
  272.         {
  273.             Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
  274.         }
  275.  
  276.         Console.WriteLine("Press anything ...");
  277.         Console.ReadKey();
  278.         Console.Clear();
  279.     }
  280.  
  281.     public void FindByTitle()
  282.     {
  283.         Console.CursorVisible = true;
  284.         Console.Write("Enter the title of the book you want to find: ");
  285.         string userInput = Console.ReadLine().ToUpper();
  286.         Console.CursorVisible = false;
  287.  
  288.         var selectedBook = _book.Where(books => books.Title.ToUpper() == userInput);
  289.  
  290.         if (selectedBook.Count() != 0)
  291.         {
  292.             foreach (var book in selectedBook)
  293.             {
  294.                 Console.Write($"{book.Author} {book.Title} " +
  295.                     $"(");
  296.  
  297.                 for (int j = 0; j < book.PublishingYear.Count; j++)
  298.                 {
  299.                     Console.Write($"{book.PublishingYear[j]}");
  300.  
  301.                     if (j < book.PublishingYear.Count - 1)
  302.                         Console.Write(", ");
  303.                 }
  304.  
  305.                 Console.WriteLine(")");
  306.             }
  307.         }
  308.         else
  309.         {
  310.             Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
  311.         }
  312.  
  313.         Console.WriteLine("Press anything ...");
  314.         Console.ReadKey();
  315.         Console.Clear();
  316.     }
  317.  
  318.     public void FindByAuthor()
  319.     {
  320.         Console.CursorVisible = true;
  321.         Console.Write("Enter the author of the book you want to find: ");
  322.         string userInput = Console.ReadLine().ToUpper();
  323.         Console.CursorVisible = false;
  324.  
  325.         var selectedBook = _book.Where(books => books.Author.ToUpper() == userInput);
  326.  
  327.         if (selectedBook.Count() != 0)
  328.         {
  329.             foreach (var book in selectedBook)
  330.             {
  331.                 Console.Write($"{book.Author} {book.Title} " +
  332.                     $"(");
  333.  
  334.                 for (int j = 0; j < book.PublishingYear.Count; j++)
  335.                 {
  336.                     Console.Write($"{book.PublishingYear[j]}");
  337.  
  338.                     if (j < book.PublishingYear.Count - 1)
  339.                         Console.Write(", ");
  340.                 }
  341.  
  342.                 Console.WriteLine(")");
  343.             }
  344.         }
  345.         else
  346.         {
  347.             Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
  348.         }
  349.  
  350.         Console.WriteLine("Press anything ...");
  351.         Console.ReadKey();
  352.         Console.Clear();
  353.     }
  354.  
  355.     public void FindByPublishingYear()
  356.     {
  357.         Console.CursorVisible = true;
  358.         Console.Write("Enter the year of the book you want to find: ");
  359.         int.TryParse(Console.ReadLine(), out int year);
  360.         Console.CursorVisible = false;
  361.  
  362.         var selectedBook = _book.Where(books => books.PublishingYear.Contains(year));
  363.  
  364.         if (selectedBook.Count() != 0)
  365.         {
  366.             foreach (var book in selectedBook)
  367.             {
  368.                 Console.Write($"{book.Author} {book.Title} ({year})\n");
  369.             }
  370.         }
  371.         else
  372.         {
  373.             Console.WriteLine("\u001b[34m Nothing found\u001b[0m");
  374.         }
  375.  
  376.         Console.WriteLine("Press anything ...");
  377.         Console.ReadKey();
  378.         Console.Clear();
  379.     }
  380.  
  381.     private int Count => _book.Count;
  382.  
  383.     private bool TryGetBook1(string title, string author, int year, out Book book)
  384.     {
  385.         book = null;
  386.  
  387.         if (title != "" && author != "" && year != 0)
  388.         {
  389.             var selectedAuthor = _book.Where(books => books.Author.ToUpper() == author.ToUpper());
  390.             var selectedTitle = selectedAuthor.Where(books => books.Title.ToUpper() == title.ToUpper());
  391.             var selectedYear = selectedTitle.Where(books => books.PublishingYear.Contains(year));
  392.  
  393.             foreach (var books in selectedYear)
  394.             {
  395.                 book = books;
  396.             }
  397.         }
  398.  
  399.         if (book != null)
  400.         {
  401.             return true;
  402.         }
  403.  
  404.         return false;
  405.     }
  406.  
  407.     private bool TryGetBook2(string title, string author, int year, out Book book)
  408.     {
  409.         book = null;
  410.  
  411.         if (title != "" && author != "" && year != 0)
  412.         {
  413.             var selectedAuthor = _book.Where(books => books.Author.ToUpper() == author.ToUpper());
  414.             var selectedTitle = selectedAuthor.Where(books => books.Title.ToUpper() == title.ToUpper());
  415.  
  416.             foreach (var books in selectedTitle)
  417.             {
  418.                 book = books;
  419.             }
  420.         }
  421.  
  422.         if (book != null)
  423.         {
  424.             return true;
  425.         }
  426.  
  427.         return false;
  428.     }
  429. }
  430.  
  431. class Book
  432. {
  433.     public Book(string title, string author, List<int> publishingYear)
  434.     {
  435.         Title = title;
  436.         Author = author;
  437.         PublishingYear = publishingYear;
  438.     }
  439.  
  440.     public string Title { get; private set; }
  441.     public string Author { get; private set; }
  442.     public List<int> PublishingYear { get; private set; }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement