start #33, test some functions
continuous-integration/drone/push Build is passing Details

pull/35/head
Alexandre AGOSTINHO 2 years ago
parent 0649ddba15
commit e1c1b428c2

@ -0,0 +1,13 @@
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);
}
}

@ -2,7 +2,7 @@
using ConsoleApp; using ConsoleApp;
using Model; using Model;
using System.Text;
Console.WriteLine("Hello, World!\n\n"); Console.WriteLine("Hello, World!\n\n");
@ -14,8 +14,44 @@ Stub stub = new Stub();
List<Recipe> recipes = stub.LoadRecipes(); List<Recipe> recipes = stub.LoadRecipes();
List<RecipeCollection> recipeCollections = stub.LoadRecipeCollection(); List<RecipeCollection> recipeCollections = stub.LoadRecipeCollection();
foreach (Recipe r in recipes) RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("All"));
Console.WriteLine(r); if (allRecipe == null)
throw new ArgumentException("Load AllRecipe in stub: can't find 'All'.");
foreach (RecipeCollection r in recipeCollections)
Console.WriteLine(r); 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());

@ -0,0 +1,71 @@
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;
}
}
}

@ -0,0 +1,13 @@
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);
}
}

@ -1,101 +1,101 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Model
{
/// <summary>
/// Define a Recipe for the preparation of a meal.
/// </summary>
public class Recipe : IEquatable<Recipe>
{
#region Attributes
private string _title = "";
#endregion
#region Properties namespace Model
/// <summary> {
/// The ID of the recipe - allows you to compare and/or get this item in an easier way. /// <summary>
/// </summary> /// Define a Recipe for the preparation of a meal.
public int Id { get; init; } /// </summary>
public class Recipe : IEquatable<Recipe>
/// <summary> {
/// The Title of the recipe. <br/> #region Attributes
/// Set to "No title." when the value passed is null, empty or contain white spaces. private string _title = "";
/// </summary> #endregion
public string Title
{ #region Properties
get => _title; /// <summary>
set /// The ID of the recipe - allows you to compare and/or get this item in an easier way.
{ /// </summary>
if (string.IsNullOrWhiteSpace(value)) public int Id { get; init; }
_title = "No title.";
else /// <summary>
_title = value; /// The Title of the recipe. <br/>
} /// Set to "No title." when the value passed is null, empty or contain white spaces.
} /// </summary>
public string Title
/// <summary> {
/// The steps of the preparation. See: <see cref="PreparationStep"/>. get => _title;
/// </summary> set
public List<PreparationStep> PreparationSteps { get; set; } {
#endregion if (string.IsNullOrWhiteSpace(value))
_title = "No title.";
#region Constructors else
/// <summary> _title = value;
/// Construct a new recipe. }
/// </summary> }
/// <param name="title">The title of the recipe</param>
/// <param name="id">The id of the recipe. If not given, get a new id.</param> /// <summary>
/// <param name="preparationSteps">The steps of the preparation of the meal</param> /// The steps of the preparation. See: <see cref="PreparationStep"/>.
public Recipe(string title = "", int? id = null, /// </summary>
params PreparationStep[] preparationSteps) public List<PreparationStep> PreparationSteps { get; set; }
{ #endregion
Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps); #region Constructors
/// <summary>
/// Construct a new recipe.
/// </summary>
/// <param name="title">The title of the recipe</param>
/// <param name="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string title = "", int? id = null,
params PreparationStep[] preparationSteps)
{
Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps);
if (id == null) if (id == null)
{ {
var randomGenerator = RandomNumberGenerator.Create(); var randomGenerator = RandomNumberGenerator.Create();
byte[] data = new byte[16]; byte[] data = new byte[16];
randomGenerator.GetBytes(data); randomGenerator.GetBytes(data);
Id = Math.Abs(BitConverter.ToInt16(data)); Id = Math.Abs(BitConverter.ToInt16(data));
} }
else Id = (int)id; else Id = (int)id;
} }
#endregion #endregion
#region Methods #region Methods
public virtual bool Equals(Recipe? other) public virtual bool Equals(Recipe? other)
{ {
if (other == null) return false; if (other == null) return false;
if (other == this) return true; if (other == this) return true;
return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps);
} }
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
var item = obj as Recipe; var item = obj as Recipe;
if (item == null) return false; if (item == null) return false;
return Equals(obj); return Equals(obj);
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return Id.GetHashCode(); return Id.GetHashCode();
} }
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n");
foreach (PreparationStep ps in PreparationSteps) foreach (PreparationStep ps in PreparationSteps)
{ {
sb.AppendFormat("\t* {0}\n", ps.ToString()); sb.AppendFormat("\t* {0}\n", ps.ToString());
} }
return sb.ToString(); return sb.ToString();
} }
#endregion #endregion
} }
} }

@ -148,7 +148,12 @@ namespace Model
sb.AppendFormat("\t - {0}\n", r.ToString()); sb.AppendFormat("\t - {0}\n", r.ToString());
} }
return sb.ToString(); return sb.ToString();
} }
public Recipe[] ResearchByName(string str)
{
return _recipes.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray();
}
#endregion #endregion
} }
} }

@ -1,42 +1,42 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290 VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{666C2211-8EBB-4FC8-9484-CB93BC854153}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{666C2211-8EBB-4FC8-9484-CB93BC854153}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{42FF86BD-92F9-4A32-A938-68515905378F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{42FF86BD-92F9-4A32-A938-68515905378F}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model_UnitTests", "Tests\Model_UnitTests\Model_UnitTests.csproj", "{45AB746A-194B-4E43-81EB-83B06F35AA33}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model_UnitTests", "Tests\Model_UnitTests\Model_UnitTests.csproj", "{45AB746A-194B-4E43-81EB-83B06F35AA33}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{08B80CE8-A01D-4D86-8989-AF225D5DA48C}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{08B80CE8-A01D-4D86-8989-AF225D5DA48C}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.Build.0 = Debug|Any CPU {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.Build.0 = Debug|Any CPU
{666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.ActiveCfg = Release|Any CPU {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.ActiveCfg = Release|Any CPU
{666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.Build.0 = Release|Any CPU {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.Build.0 = Release|Any CPU
{42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.Build.0 = Debug|Any CPU {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.ActiveCfg = Release|Any CPU {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.Build.0 = Release|Any CPU {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.Build.0 = Release|Any CPU
{45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.Build.0 = Debug|Any CPU {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.ActiveCfg = Release|Any CPU {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.Build.0 = Release|Any CPU {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{45AB746A-194B-4E43-81EB-83B06F35AA33} = {08B80CE8-A01D-4D86-8989-AF225D5DA48C} {45AB746A-194B-4E43-81EB-83B06F35AA33} = {08B80CE8-A01D-4D86-8989-AF225D5DA48C}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ADEA5603-1EF6-4D43-9493-7D6D9DE7FA3F} SolutionGuid = {ADEA5603-1EF6-4D43-9493-7D6D9DE7FA3F}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

Loading…
Cancel
Save