dev of the structure as decribed in #17. need some review before pr
continuous-integration/drone/push Build is passing Details

pull/24/head
Alexandre Agostinho 2 years ago
parent cdb2628c5d
commit 9b0015d3c4

@ -1,4 +1,40 @@
// See https://aka.ms/new-console-template for more information
using ;
Console.WriteLine("Hello, World!");
using Model;
Console.WriteLine("Hello, World!\n");
// TESTS:
BaseItem r1 = new Recipe("A recipe...");
r1.DisplayItem();
r1.DisplayId();
r1.DisplayDescription();
Console.WriteLine();
RecipeCollection rc = new RecipeCollection("A recipe collection...");
for (uint i = 0; i < 10; i++)
{
rc += new Recipe($"Recipe number {i} in the collection."); // test overload + operator
}
try // test overload of [] operator
{
rc[1003].DisplayItem();
rc[2003].DisplayItem(); // incorrect index
}
catch (ArgumentNullException)
{
Console.Error.WriteLine("An index are incorrect!\n");
}
foreach (Recipe r in rc.Collection)
{
r.DisplayId();
r.DisplayDescription();
}

@ -0,0 +1,56 @@

using System;
namespace Model
{
/// <summary>
/// Define the base structure of any Items.<br/>
/// An Item can be identifed and have a short description. It can also be displayed.
/// </summary>
public abstract class BaseItem : IDisplayable
{
/// <summary>
/// The identifier of an Item.<br/>
/// The first number correspond to the typs of the Item.
/// </summary>
public uint Id { get; }
/// <summary>
/// A short description of the Item. Useful to know what this Item stand for.
/// </summary>
public string Description { get; set; }
protected BaseItem(uint id, string description = "")
{
Id = id;
Description = description;
}
public override string ToString()
{
return
$"[ Class -BaseItem- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
// IDisplayable Implementation
public void DisplayId()
{
Console.WriteLine($".Id - {Id}");
}
public void DisplayDescription()
{
Console.WriteLine($".Description - {Description}");
}
public void DisplayItem()
{
Console.WriteLine(this.ToString());
}
}
}

@ -0,0 +1,23 @@
namespace Model
{
/// <summary>
/// Define an Item that can be displayed.
/// </summary>
interface IDisplayable
{
/// <summary>
/// Display the Id of the Item
/// </summary>
void DisplayId();
/// <summary>
/// Display the Description of the Item
/// </summary>
void DisplayDescription();
/// <summary>
/// Display the entire Item (Id, Description and other...)
/// </summary>
void DisplayItem();
}
}

@ -1,7 +0,0 @@
namespace Model
{
public class ItemCollection
{
}
}

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
namespace Model
{
/// <summary>
/// A Recipe is a description of step and maybe some techniques, with an ingredient list to make a meal.<br/>
/// It is instantiated with a new unique four digit id, where the first one is 1.
/// </summary>
public class Recipe : BaseItem
{
/// <summary>
/// This static atrribute stand for the creation of a new id. It is incremented each time a new Recipe is instantiated.
/// </summary>
public static uint idIterator = 0;
/// <summary>
/// The title of the recipe.
/// </summary>
public string Title { get; set; }
/// <summary>
/// All the details about the preparation of the meal.
/// </summary>
public string Preparation { get; set; }
public Recipe(string description = "", string title = "", string preparation = "")
: base(idIterator+1000, description)
{
if (idIterator == 1000) throw new OverflowException("id has reach the maximum value.");
Title = title;
Preparation = preparation;
idIterator++;
}
public override string ToString()
{
return
$"[ Class -Recipe- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
}
}

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
namespace Model
{
/// <summary>
/// A Recipe collection is a group of recipe.<br/>
/// It is instantiated with a new unique four digit id, where the first one is 2.
/// </summary>
public class RecipeCollection : BaseItem
{
/// <summary>
/// This static atrribute stand for the creation of a new id. It is incremented each time a new RecipeCollection is instantiated.
/// </summary>
private static uint idIterator = 0;
/// <summary>
/// The main composent of a RecipeCollection. This contain the Recipes included in this collection.
/// </summary>
public List<Recipe> Collection { get; set; }
public RecipeCollection(string description = "", List<Recipe>? collection = null)
: base(idIterator + 2000, description)
{
if (idIterator == 1000) throw new OverflowException("id has reach the maximum value.");
if (collection == null)
Collection = new List<Recipe>();
else
Collection = collection;
idIterator++;
}
public override string ToString()
{
return
$"[ Class -RecipeCollection- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
/// <summary>
/// Overload the [] operator to access the Recipe directly without passing by the Collection.<br/>
/// It use the identifier of the Recipe to get the item.
/// </summary>
/// <param name="id">The id of the Recipe</param>
/// <returns></returns>
public Recipe this[uint id]
{
get
{
foreach (Recipe r in this.Collection)
{
if (r.Id == id) return r;
}
throw new ArgumentNullException("The id given dosen't correspond to any recipes in the collection.");
}
}
/// <summary>
/// Overload of + operator to add a Recipe to a RecipeCollection.
/// </summary>
/// <param name="coll">The recipe collection where the recipe will be added</param>
/// <param name="ri">The recipe to add</param>
/// <returns>The recipe collection where the recipe is added.</returns>
public static RecipeCollection operator + (RecipeCollection coll, Recipe ri)
{
coll.Collection.Add(ri); return coll;
}
/// <summary>
/// Overload of - operator to remove a Recipe to a RecipeCollection.
/// </summary>
/// <param name="coll">The recipe collection where the recipe will be removed</param>
/// <param name="ri">The recipe to remove</param>
/// <returns>The recipe collection where the recipe is removed.</returns>
public static RecipeCollection operator - (RecipeCollection coll, Recipe ri)
{
coll.Collection.Remove(ri); return coll;
}
}
}
Loading…
Cancel
Save