partial commit (bf change of RecipeCollection)

pull/38/head
Alexandre AGOSTINHO 2 years ago
parent d5adeff27a
commit 352e38421f

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal interface IMenuDisplay
{
/// <summary>
/// Update the menu display in the Console.
/// </summary>
void UpdateDisplay();
/// <summary>
/// Select the next line in the menu
/// </summary>
/// <returns>The current number of the new selected line</returns>
int SelectNextLine();
/// <summary>
/// Select the previous line in the menu
/// </summary>
/// <returns>The current number of the new selected line</returns>
int SelectPrevioustLine();
}
}

@ -9,30 +9,53 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
internal abstract class Menu<T> : IMenu internal abstract class Menu<T> : IMenu
where T : notnull
{ {
protected StringBuilder _screenDisplay; protected StringBuilder _screenDisplay;
protected readonly List<Selector<T>> _selectList; protected readonly List<Selector<T>>? _selectList;
public string Title { get; private set; } public string Title { get; private set; }
public StringBuilder InputStr { get; set; } public StringBuilder InputStr { get; set; }
public int CurrentLine { get; private set; } public int CurrentLine { get; private set; }
public T CurrentSelected { get; private set; } public T? CurrentSelected { get; private set; }
public bool WriteMode { get; set; } public bool WriteMode { get; set; }
public Menu(string title, params Selector<T>[] selections ) private Menu(string title)
{ {
if (selections == null || selections.Length == 0)
throw new ArgumentException("Error: a menu must contain at least 1 selection");
Title = title; Title = title;
CurrentLine = 0; CurrentLine = 0;
CurrentSelected = selections[0].Item;
WriteMode = false; WriteMode = false;
_selectList = new List<Selector<T>>(selections);
_screenDisplay = new StringBuilder(); _screenDisplay = new StringBuilder();
InputStr = new StringBuilder(); InputStr = new StringBuilder();
} }
public Menu(string title, params Selector<T>[] selections ) : this(title)
{
if (selections == null || selections.Length == 0)
throw new ArgumentException("Error: a menu must contain at least 1 selection");
_selectList = selections.ToList();
CurrentSelected = _selectList[0].Item;
}
public Menu(string title, Dictionary<T, string> lines) : this(title, CreateSelection(lines))
{ }
public static Selector<T>[] CreateSelection(Dictionary<T, string> lines)
{
if (lines is null)
{
throw new ArgumentNullException(nameof(lines));
}
Selector<T>[] selections = new Selector<T>[lines.Count];
foreach (KeyValuePair<T, string> line in lines)
{
selections.Append(new Selector<T>(line.Key, line.Value));
}
return selections;
}
public virtual void Display() public virtual void Display()
{ {
Console.WriteLine(_screenDisplay); Console.WriteLine(_screenDisplay);
@ -61,7 +84,7 @@ namespace ConsoleApp.Menu
public void SelectPrevious() public void SelectPrevious()
{ {
CurrentSelected = _selectList[--CurrentLine].Item CurrentSelected = _selectList[--CurrentLine].Item;
} }
public void EnableWriteMode() public void EnableWriteMode()

@ -14,10 +14,30 @@ namespace ConsoleApp.Menu
/// </summary> /// </summary>
internal class SearcherRecipe : Menu<Recipe> internal class SearcherRecipe : Menu<Recipe>
{ {
public SearcherRecipe(string title, params Selector<Recipe>[] selections) private SearcherRecipe(string title, params Selector<Recipe>[] selections)
: base(title, selections) : base(title, selections)
{ } { }
private SearcherRecipe(string title, Dictionary<Recipe, string> lines)
: base(title, lines)
{ }
public SearcherRecipe(RecipeCollection recipeCollection)
: this("Recipe search", recipeCollection.ToDictionary<int, Recipe>(recipe => recipe.GetHashCode()))
{
}
public static SearcherRecipe SearchInRecipeCollection(string title, RecipeCollection recipeCollection)
{
Dictionary<Recipe, string> recipesSelectors = new Dictionary<Recipe, string>();
foreach (Recipe recipe in recipeCollection)
{
recipesSelectors.Add(recipe, recipe.Title);
}
return new SearcherRecipe(title, CreateSelection(recipesSelectors));
}
#region Methodes #region Methodes
public override void Return() public override void Return()
{ {

@ -1,52 +0,0 @@
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,5 +1,7 @@
using System; using Model;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Reflection.Emit; using System.Reflection.Emit;
using System.Text; using System.Text;
@ -7,7 +9,8 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
internal class Selector<T> internal class Selector<T> : IEquatable<Selector<T>>
where T : notnull
{ {
private string _line = ""; private string _line = "";
@ -28,5 +31,24 @@ namespace ConsoleApp.Menu
Line = line; Line = line;
Item = item; Item = item;
} }
public virtual bool Equals(Selector<T>? other)
{
if (other == null) return false;
if (other == this) return true;
return other.Line.Equals(this.Line);
}
public override bool Equals(object? obj)
{
var item = obj as Selector<T>;
if (item == null) return false;
return Equals(obj);
}
public override int GetHashCode()
{
return Line.GetHashCode();
}
} }
} }

@ -22,8 +22,7 @@ if (allRecipe == null)
DataManager manager = new DataManager(allRecipe); DataManager manager = new DataManager(allRecipe);
Recipe? ret = SearcherRecipe.ResearchOn(allRecipe); MenuManager menuManager = new MenuManager(manager, ConsoleApp.Menu.SearcherRecipe.SearchInRecipeCollection("search recipe", manager.AllRecipes));
Console.WriteLine(ret);
// press any key to quit // press any key to quit
Console.ReadKey(); Console.ReadKey();

Loading…
Cancel
Save