change nested class 'ConsoleMenu' to namespace 'Menu'
continuous-integration/drone/push Build is passing Details

pull/35/head
Alexandre AGOSTINHO 2 years ago
parent b2e6217d10
commit 1a2ecceb25

@ -1,62 +0,0 @@
using ConsoleApp;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
/// <summary>
/// Define multiple type of menu
/// <br/>See:
/// <br/>- <see cref="SelectMenu{T}"/>
/// <br/>- <see cref="SearcherRecipe"/>
/// </summary>
public partial class ConsoleMenu : Manager
{
/// <summary>
/// An abstract class that define the components of a selection-type menu.
/// </summary>
/// <typeparam name="T">The return type of the currently selected item</typeparam>
public abstract class SelectMenu<T> : IMenuDisplay
{
protected int _currentLine = 0;
protected int _maxLines = 0;
/// <summary>
/// The currently selected item.
/// </summary>
public T? CurrentSelected { get; protected set; }
/// <summary>
/// The current line selected in the menu.
/// </summary>
public int CurrentLine
{
get => _currentLine;
protected set
{
_currentLine = value;
if (_currentLine > _maxLines)
{
_currentLine = _maxLines;
}
if (_currentLine < 0)
{
_currentLine = 0;
}
return;
}
}
public int SelectNextLine() { return ++CurrentLine; }
public int SelectPrevioustLine() { return --CurrentLine; }
public abstract void UpdateDisplay();
}
}
}

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ConsoleApp namespace ConsoleApp.Menu
{ {
internal interface IMenuDisplay internal interface IMenuDisplay
{ {

@ -7,122 +7,119 @@ using System.Threading.Tasks;
using Model; using Model;
namespace ConsoleApp namespace ConsoleApp.Menu
{ {
public partial class ConsoleMenu : Manager /// <summary>
/// An utility to find a recipe.
/// </summary>
public class SearcherRecipe : SelectMenu<Recipe>
{ {
#region Attribute
private RecipeCollection _recipeOnSearch;
#endregion
#region Properties
/// <summary> /// <summary>
/// An utility to find a recipe. /// A collection of recipe where the title contain the search string
/// </summary> /// </summary>
public class SearcherRecipe : SelectMenu<Recipe> public RecipeCollection SearchResult { get; private set; }
{
#region Attribute
private RecipeCollection _recipeOnSearch;
#endregion
#region Properties /// <summary>
/// <summary> /// The search string
/// A collection of recipe where the title contain the search string /// </summary>
/// </summary> public string ResearchStr { get; set; }
public RecipeCollection SearchResult { get; private set; } #endregion
/// <summary>
/// The search string
/// </summary>
public string ResearchStr { get; set; }
#endregion
/// <summary> /// <summary>
/// Constructor of the SearcherRecipe utility. /// Constructor of the SearcherRecipe utility.
/// </summary> /// </summary>
/// <param name="recipeOnSearch">The collection of recipe where to search</param> /// <param name="recipeOnSearch">The collection of recipe where to search</param>
public SearcherRecipe(RecipeCollection recipeOnSearch) public SearcherRecipe(RecipeCollection recipeOnSearch)
{ {
_recipeOnSearch = recipeOnSearch; _recipeOnSearch = recipeOnSearch;
SearchResult = _recipeOnSearch; SearchResult = _recipeOnSearch;
ResearchStr = ""; ResearchStr = "";
} }
#region Methodes #region Methodes
/// <summary> /// <summary>
/// Launch a search by name request in the collection of Recipe with with a string. /// Launch a search by name request in the collection of Recipe with with a string.
/// </summary> /// </summary>
/// <param name="researchStr">The string for search</param> /// <param name="researchStr">The string for search</param>
/// <returns>True if the result of the search gave at least 1 element. False otherwise.</returns> /// <returns>True if the result of the search gave at least 1 element. False otherwise.</returns>
public bool ComputeSearch(string researchStr = "") public bool ComputeSearch(string researchStr = "")
{ {
ResearchStr = researchStr; ResearchStr = researchStr;
SearchResult = _recipeOnSearch.ResearchByName(ResearchStr.ToLower()); SearchResult = _recipeOnSearch.ResearchByName(ResearchStr.ToLower());
_maxLines = SearchResult.Count - 1; _maxLines = SearchResult.Count - 1;
return SearchResult.Count > 0; return SearchResult.Count > 0;
} }
public override void UpdateDisplay() public override void UpdateDisplay()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------------------------------------------");
sb.AppendLine($" Research: {ResearchStr}");
sb.AppendLine("---------------------------------------------------------");
for (int i = 0; i < SearchResult.Count; i++)
{ {
StringBuilder sb = new StringBuilder(); if (i == CurrentLine)
sb.AppendLine("---------------------------------------------------------");
sb.AppendLine($" Research: {ResearchStr}");
sb.AppendLine("---------------------------------------------------------");
for (int i = 0; i < SearchResult.Count; i++)
{ {
if (i == CurrentLine) CurrentSelected = SearchResult[i];
{ sb.Append($">");
CurrentSelected = SearchResult[i];
sb.Append($">");
}
sb.AppendLine($" [ {SearchResult[i].Id} ]:\t{SearchResult[i].Title} ");
} }
Console.Clear(); sb.AppendLine($" [ {SearchResult[i].Id} ]:\t{SearchResult[i].Title} ");
Console.WriteLine(sb);
} }
Console.Clear();
Console.WriteLine(sb);
}
/// <summary> /// <summary>
/// Launch and pilot the search menu. /// Launch and pilot the search menu.
/// </summary> /// </summary>
/// <param name="recipeOnSearch">The collection of recipe where to search</param> /// <param name="recipeOnSearch">The collection of recipe where to search</param>
/// <returns>The recipe selected</returns> /// <returns>The recipe selected</returns>
public static Recipe? ResearchOn(RecipeCollection recipeOnSearch) public static Recipe? ResearchOn(RecipeCollection recipeOnSearch)
{ {
SearcherRecipe sr = new SearcherRecipe(recipeOnSearch); SearcherRecipe sr = new SearcherRecipe(recipeOnSearch);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sr.ComputeSearch(); sr.ComputeSearch();
sr.UpdateDisplay(); sr.UpdateDisplay();
ConsoleKeyInfo cki; ConsoleKeyInfo cki;
do do
{ {
cki = Console.ReadKey(true); cki = Console.ReadKey(true);
switch (cki.Key) switch (cki.Key)
{ {
case ConsoleKey.UpArrow: case ConsoleKey.UpArrow:
sr.SelectPrevioustLine(); sr.SelectPrevioustLine();
break; break;
case ConsoleKey.DownArrow: case ConsoleKey.DownArrow:
sr.SelectNextLine(); sr.SelectNextLine();
break; break;
case ConsoleKey.Backspace: case ConsoleKey.Backspace:
if (sb.Length > 0) if (sb.Length > 0)
{ {
sb.Remove(sb.Length - 1, 1); sb.Remove(sb.Length - 1, 1);
sr.ComputeSearch(sb.ToString());
}
break;
default:
sb.Append(cki.KeyChar);
sr.ComputeSearch(sb.ToString()); sr.ComputeSearch(sb.ToString());
break; }
} break;
default:
sb.Append(cki.KeyChar);
sr.ComputeSearch(sb.ToString());
break;
}
sr.UpdateDisplay(); sr.UpdateDisplay();
} while (cki.Key != ConsoleKey.Enter); } while (cki.Key != ConsoleKey.Enter);
return sr.CurrentSelected; return sr.CurrentSelected;
}
#endregion
} }
#endregion
} }
} }

@ -0,0 +1,52 @@
using ConsoleApp;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
/// <summary>
/// An abstract class that define the components of a selection-type menu.
/// </summary>
/// <typeparam name="T">The return type of the currently selected item</typeparam>
public abstract class SelectMenu<T> : IMenuDisplay
{
protected int _currentLine = 0;
protected int _maxLines = 0;
/// <summary>
/// The currently selected item.
/// </summary>
public T? CurrentSelected { get; protected set; }
/// <summary>
/// The current line selected in the menu.
/// </summary>
public int CurrentLine
{
get => _currentLine;
protected set
{
_currentLine = value;
if (_currentLine > _maxLines)
{
_currentLine = _maxLines;
}
if (_currentLine < 0)
{
_currentLine = 0;
}
return;
}
}
public int SelectNextLine() { return ++CurrentLine; }
public int SelectPrevioustLine() { return --CurrentLine; }
public abstract void UpdateDisplay();
}
}

@ -1,6 +1,7 @@
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
using ConsoleApp; using ConsoleApp;
using ConsoleApp.Menu;
using Model; using Model;
using System.Text; using System.Text;
@ -21,8 +22,8 @@ if (allRecipe == null)
Manager manager = new Manager(allRecipe); Manager manager = new Manager(allRecipe);
Console.WriteLine(ConsoleMenu.SearcherRecipe.ResearchOn(manager.AllRecipes)); Recipe? ret = SearcherRecipe.ResearchOn(allRecipe);
Console.WriteLine(ret);
// press any key to quit // press any key to quit
Console.ReadKey(); Console.ReadKey();

Loading…
Cancel
Save