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.Threading.Tasks;
namespace ConsoleApp
namespace ConsoleApp.Menu
{
internal interface IMenuDisplay
{

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

Loading…
Cancel
Save