♻️ refactor classes

pull/35/head
Alexandre AGOSTINHO 2 years ago
parent e1c1b428c2
commit 5e9a7f6860

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>

@ -0,0 +1,60 @@
using ConsoleApp;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
public class ConsoleMenu : Manager
{
public ConsoleMenu(Manager manager)
{
AllRecipes = manager.AllRecipes;
}
public Recipe? ResearchOnRecipe()
{
StringBuilder sb = new StringBuilder();
SearcherRecipe searcherRecipe = new SearcherRecipe();
RecipeCollection result = AllRecipes.ResearchByName(sb.ToString());
searcherRecipe.UpdateDisplay(sb.ToString(), result);
ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.UpArrow:
searcherRecipe.SelectPrevioustLine();
break;
case ConsoleKey.DownArrow:
searcherRecipe.SelectNextLine();
break;
case ConsoleKey.Backspace:
if (sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1);
result = AllRecipes.ResearchByName(sb.ToString());
}
break;
default:
sb.Append(cki.KeyChar);
result = AllRecipes.ResearchByName(sb.ToString());
break;
}
searcherRecipe.UpdateDisplay(sb.ToString(), result);
} while (cki.Key != ConsoleKey.Enter);
return searcherRecipe.CurrentSelected;
}
}
}

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
public interface IResearchMenu<T>
{
void updateDisplay(string researchStr, T[] researchResult);
}
}

@ -1,57 +1,27 @@
// See https://aka.ms/new-console-template for more information
// See https://aka.ms/new-console-template for more information
using ConsoleApp;
using Model;
using System.Text;
Console.WriteLine("Hello, World!\n\n");
// TESTS:
Stub stub = new Stub();
List<Recipe> recipes = stub.LoadRecipes();
List<RecipeCollection> recipeCollections = stub.LoadRecipeCollection();
RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("All"));
if (allRecipe == null)
throw new ArgumentException("Load AllRecipe in stub: can't find 'All'.");
StringBuilder sb = new StringBuilder();
ResearchMenuRecipe researchMenu = new ResearchMenuRecipe();
Recipe[] result = allRecipe.ResearchByName(sb.ToString());
researchMenu.UpdateDisplay(sb.ToString(), result);
ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.UpArrow:
researchMenu.SelectPrevioustLine();
break;
case ConsoleKey.DownArrow:
researchMenu.SelectNextLine();
break;
case ConsoleKey.Backspace:
if (sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1);
result = allRecipe.ResearchByName(sb.ToString());
}
break;
default:
sb.Append(cki.KeyChar);
result = allRecipe.ResearchByName(sb.ToString());
break;
}
researchMenu.UpdateDisplay(sb.ToString(), result);
} while (cki.Key != ConsoleKey.Enter);
Console.WriteLine(researchMenu.ReturnSelected());
using Model;
using System.Text;
Console.WriteLine("Hello, World!\n\n");
// TESTS:
Stub stub = new Stub();
List<Recipe> recipes = stub.LoadRecipes();
List<RecipeCollection> recipeCollections = stub.LoadRecipeCollection();
RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("All"));
if (allRecipe == null)
throw new ArgumentException("Load AllRecipe in stub: can't find 'All'.");
Manager manager = new Manager(allRecipe);
ConsoleMenu menu = new ConsoleMenu(manager);
Recipe? recipeRetSearchMenu = menu.ResearchOnRecipe();
Console.WriteLine(recipeRetSearchMenu);

@ -1,71 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace ConsoleApp
{
internal class ResearchMenuRecipe
{
internal string displayText = "";
public int CurrentLine
{
get => currentLine;
set
{
currentLine = value;
if (currentLine > maxLines)
{
currentLine = maxLines;
}
if (currentLine < 0)
{
currentLine = 0;
}
return;
}
}
private int currentLine = 0;
private int maxLines = 0;
private Recipe? currentSelected;
public void UpdateDisplay(string researchStr, Recipe[] researchResult)
{
maxLines = researchResult.Length - 1;
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------------------------------------------");
sb.AppendLine($" Research: {researchStr}");
sb.AppendLine("---------------------------------------------------------");
for (int i = 0; i < researchResult.Length; i++)
{
if (i == CurrentLine)
{
currentSelected = researchResult[i];
sb.Append($">");
}
sb.AppendLine($" [ {researchResult[i].Id} ]: {researchResult[i].Title} ");
}
Console.Clear();
Console.WriteLine(sb);
}
public int SelectNextLine()
{
return ++CurrentLine;
}
public int SelectPrevioustLine()
{
return --CurrentLine;
}
public Recipe? ReturnSelected()
{
return currentSelected;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace ConsoleApp
{
internal class SearcherRecipe
{
public int CurrentLine
{
get => _currentLine;
set
{
_currentLine = value;
if (_currentLine > _maxLines)
{
_currentLine = _maxLines;
}
if (_currentLine < 0)
{
_currentLine = 0;
}
return;
}
}
public Recipe? CurrentSelected { get; private set; }
private int _currentLine = 0;
private int _maxLines = 0;
public void UpdateDisplay(string researchStr, RecipeCollection researchResult)
{
_maxLines = researchResult.Count - 1;
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------------------------------------------");
sb.AppendLine($" Research: {researchStr}");
sb.AppendLine("---------------------------------------------------------");
for (int i = 0; i < researchResult.Count; i++)
{
if (i == CurrentLine)
{
CurrentSelected = researchResult[i];
sb.Append($">");
}
sb.AppendLine($" [ {researchResult[i].Id} ]: {researchResult[i].Title} ");
}
Console.Clear();
Console.WriteLine(sb);
}
public int SelectNextLine()
{
return ++CurrentLine;
}
public int SelectPrevioustLine()
{
return --CurrentLine;
}
}
}

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public interface IReasearch<T>
{
public T ResearchByName(string str);
}
}

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public partial class Manager
{
public RecipeCollection AllRecipes { get; protected set; }
public Manager()
{
AllRecipes = new RecipeCollection(description: "All Recipes");
}
public Manager(RecipeCollection allRecipes)
{
AllRecipes = new RecipeCollection(
description: "All Recipes",
recipes: allRecipes.ToArray());
}
}
}

@ -1,159 +1,161 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Model
{
/// <summary>
/// Define a collection of <see cref="Recipe"/>.
/// <br/>This class implement <see cref="IList"/> and <see cref="IEquatable{T}"/>.
/// </summary>
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
{
#region Attributes
private readonly List<Recipe> _recipes;
private string _description = "";
#endregion
#region Properties
/// <summary>
/// A short description of what this collection contain. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string Description
{
get => _description;
set
{
if (string.IsNullOrWhiteSpace(value))
_description = "No description.";
else
_description = value;
}
}
#region IList Prperties
public int Count => _recipes.Count;
public bool IsReadOnly => false;
public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; }
#endregion
#endregion
#region Constructors
/// <summary>
/// Construct a new collection of _recipes.
/// </summary>
/// <param name="description">A short description of what this list will contain</param>
/// <param name="recipes">Recipes to add in this new collection</param>
public RecipeCollection(string description, params Recipe[] recipes)
{
_recipes = new List<Recipe>(recipes);
Description = description;
}
#endregion
#region Methods
/// <summary>
/// Find a recipe in this list by giving the id.
/// </summary>
/// <param name="id">The id of the list we are looking for</param>
/// <returns>The recipe of the id given</returns>
/// <exception cref="ArgumentException"/>
public Recipe? GetRecipeById(int id)
{
Recipe? recipe = _recipes.Find(r => r.Id == id);
if (recipe == null) throw new ArgumentException("No _recipes match the given id.");
return recipe;
}
#region IList Methods
public int IndexOf(Recipe item)
{
return _recipes.IndexOf(item);
}
public void Insert(int index, Recipe item)
{
_recipes.Insert(index, item);
}
public void RemoveAt(int index)
{
_recipes.RemoveAt(index);
}
public void Add(Recipe item)
{
_recipes.Add(item);
}
public void Clear()
{
_recipes.Clear();
}
public bool Contains(Recipe item)
{
return _recipes.Contains(item);
}
public void CopyTo(Recipe[] array, int arrayIndex)
{
_recipes.CopyTo(array, arrayIndex);
}
public bool Remove(Recipe item)
{
return _recipes.Remove(item);
}
public IEnumerator<Recipe> GetEnumerator()
{
return _recipes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _recipes.GetEnumerator();
}
#endregion
public virtual bool Equals(RecipeCollection? other)
{
if (other == null) return false;
if (other == this) return true;
return _description.Equals(other.Description) && _recipes.Equals(other._recipes);
}
public override bool Equals(object? obj)
{
var item = obj as RecipeCollection;
if (item == null) return false;
return Equals(obj);
}
public override int GetHashCode()
{
return _recipes.GetHashCode();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n");
foreach (Recipe r in _recipes)
{
sb.AppendFormat("\t - {0}\n", r.ToString());
}
return sb.ToString();
}
public Recipe[] ResearchByName(string str)
{
return _recipes.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray();
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Model
{
/// <summary>
/// Define a collection of <see cref="Recipe"/>.
/// <br/>This class implement <see cref="IList"/> and <see cref="IEquatable{T}"/>.
/// </summary>
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
{
#region Attributes
private readonly List<Recipe> _recipes;
private string _description = "";
#endregion
#region Properties
/// <summary>
/// A short description of what this collection contain. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string Description
{
get => _description;
set
{
if (string.IsNullOrWhiteSpace(value))
_description = "No description.";
else
_description = value;
}
}
#region IList Prperties
public int Count => _recipes.Count;
public bool IsReadOnly => false;
public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; }
#endregion
#endregion
#region Constructors
/// <summary>
/// Construct a new collection of _recipes.
/// </summary>
/// <param name="description">A short description of what this list will contain</param>
/// <param name="recipes">Recipes to add in this new collection</param>
public RecipeCollection(string description, params Recipe[] recipes)
{
_recipes = new List<Recipe>(recipes);
Description = description;
}
#endregion
#region Methods
/// <summary>
/// Find a recipe in this list by giving the id.
/// </summary>
/// <param name="id">The id of the list we are looking for</param>
/// <returns>The recipe of the id given</returns>
/// <exception cref="ArgumentException"/>
public Recipe? GetRecipeById(int id)
{
Recipe? recipe = _recipes.Find(r => r.Id == id);
if (recipe == null) throw new ArgumentException("No _recipes match the given id.");
return recipe;
}
public RecipeCollection ResearchByName(string str)
{
return new RecipeCollection(
description: $"Results of the research: {str}",
recipes: _recipes.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray());
}
#region IList Methods
public int IndexOf(Recipe item)
{
return _recipes.IndexOf(item);
}
public void Insert(int index, Recipe item)
{
_recipes.Insert(index, item);
}
public void RemoveAt(int index)
{
_recipes.RemoveAt(index);
}
public void Add(Recipe item)
{
_recipes.Add(item);
}
public void Clear()
{
_recipes.Clear();
}
public bool Contains(Recipe item)
{
return _recipes.Contains(item);
}
public void CopyTo(Recipe[] array, int arrayIndex)
{
_recipes.CopyTo(array, arrayIndex);
}
public bool Remove(Recipe item)
{
return _recipes.Remove(item);
}
public IEnumerator<Recipe> GetEnumerator()
{
return _recipes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _recipes.GetEnumerator();
}
#endregion
public virtual bool Equals(RecipeCollection? other)
{
if (other == null) return false;
if (other == this) return true;
return _description.Equals(other.Description) && _recipes.Equals(other._recipes);
}
public override bool Equals(object? obj)
{
var item = obj as RecipeCollection;
if (item == null) return false;
return Equals(obj);
}
public override int GetHashCode()
{
return _recipes.GetHashCode();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n");
foreach (Recipe r in _recipes)
{
sb.AppendFormat("\t - {0}\n", r.ToString());
}
return sb.ToString();
}
#endregion
}
}

Loading…
Cancel
Save