From 9b0015d3c4455dc1adb30d559c1e0b5bf6067917 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Fri, 7 Apr 2023 21:13:49 +0200 Subject: [PATCH] dev of the structure as decribed in #17. need some review before pr --- MCTG/MCTGApp/Program.cs | 40 ++++++++++++++- MCTG/MCTGLib/BaseItem.cs | 56 +++++++++++++++++++++ MCTG/MCTGLib/IDisplayable.cs | 23 +++++++++ MCTG/MCTGLib/ItemCollection.cs | 7 --- MCTG/MCTGLib/Recipe.cs | 47 +++++++++++++++++ MCTG/MCTGLib/RecipeCollection.cs | 86 ++++++++++++++++++++++++++++++++ 6 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 MCTG/MCTGLib/BaseItem.cs create mode 100644 MCTG/MCTGLib/IDisplayable.cs delete mode 100644 MCTG/MCTGLib/ItemCollection.cs create mode 100644 MCTG/MCTGLib/Recipe.cs create mode 100644 MCTG/MCTGLib/RecipeCollection.cs diff --git a/MCTG/MCTGApp/Program.cs b/MCTG/MCTGApp/Program.cs index 275a069..eb783ba 100644 --- a/MCTG/MCTGApp/Program.cs +++ b/MCTG/MCTGApp/Program.cs @@ -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(); +} diff --git a/MCTG/MCTGLib/BaseItem.cs b/MCTG/MCTGLib/BaseItem.cs new file mode 100644 index 0000000..36a653c --- /dev/null +++ b/MCTG/MCTGLib/BaseItem.cs @@ -0,0 +1,56 @@ + +using System; + +namespace Model +{ + /// + /// Define the base structure of any Items.
+ /// An Item can be identifed and have a short description. It can also be displayed. + ///
+ public abstract class BaseItem : IDisplayable + { + /// + /// The identifier of an Item.
+ /// The first number correspond to the typs of the Item. + ///
+ public uint Id { get; } + + /// + /// A short description of the Item. Useful to know what this Item stand for. + /// + 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()); + } + } +} diff --git a/MCTG/MCTGLib/IDisplayable.cs b/MCTG/MCTGLib/IDisplayable.cs new file mode 100644 index 0000000..fed294c --- /dev/null +++ b/MCTG/MCTGLib/IDisplayable.cs @@ -0,0 +1,23 @@ +namespace Model +{ + /// + /// Define an Item that can be displayed. + /// + interface IDisplayable + { + /// + /// Display the Id of the Item + /// + void DisplayId(); + + /// + /// Display the Description of the Item + /// + void DisplayDescription(); + + /// + /// Display the entire Item (Id, Description and other...) + /// + void DisplayItem(); + } +} diff --git a/MCTG/MCTGLib/ItemCollection.cs b/MCTG/MCTGLib/ItemCollection.cs deleted file mode 100644 index a3c7d4e..0000000 --- a/MCTG/MCTGLib/ItemCollection.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Model -{ - public class ItemCollection - { - - } -} \ No newline at end of file diff --git a/MCTG/MCTGLib/Recipe.cs b/MCTG/MCTGLib/Recipe.cs new file mode 100644 index 0000000..0e6611f --- /dev/null +++ b/MCTG/MCTGLib/Recipe.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; + +namespace Model +{ + /// + /// A Recipe is a description of step and maybe some techniques, with an ingredient list to make a meal.
+ /// It is instantiated with a new unique four digit id, where the first one is 1. + ///
+ public class Recipe : BaseItem + { + /// + /// This static atrribute stand for the creation of a new id. It is incremented each time a new Recipe is instantiated. + /// + public static uint idIterator = 0; + + /// + /// The title of the recipe. + /// + public string Title { get; set; } + + /// + /// All the details about the preparation of the meal. + /// + 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"; + } + } +} diff --git a/MCTG/MCTGLib/RecipeCollection.cs b/MCTG/MCTGLib/RecipeCollection.cs new file mode 100644 index 0000000..594ae91 --- /dev/null +++ b/MCTG/MCTGLib/RecipeCollection.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; + +namespace Model +{ + /// + /// A Recipe collection is a group of recipe.
+ /// It is instantiated with a new unique four digit id, where the first one is 2. + ///
+ public class RecipeCollection : BaseItem + { + /// + /// This static atrribute stand for the creation of a new id. It is incremented each time a new RecipeCollection is instantiated. + /// + private static uint idIterator = 0; + + /// + /// The main composent of a RecipeCollection. This contain the Recipes included in this collection. + /// + public List Collection { get; set; } + + + public RecipeCollection(string description = "", List? collection = null) + : base(idIterator + 2000, description) + { + if (idIterator == 1000) throw new OverflowException("id has reach the maximum value."); + + if (collection == null) + Collection = new List(); + else + Collection = collection; + + idIterator++; + } + + + public override string ToString() + { + return + $"[ Class -RecipeCollection- ]\n\n" + + $"\t.Id - {Id}\n" + + $"\t.Description - {Description}\n" + + $"______\n\n"; + } + + /// + /// Overload the [] operator to access the Recipe directly without passing by the Collection.
+ /// It use the identifier of the Recipe to get the item. + ///
+ /// The id of the Recipe + /// + 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."); + } + } + + /// + /// Overload of + operator to add a Recipe to a RecipeCollection. + /// + /// The recipe collection where the recipe will be added + /// The recipe to add + /// The recipe collection where the recipe is added. + public static RecipeCollection operator + (RecipeCollection coll, Recipe ri) + { + coll.Collection.Add(ri); return coll; + } + + /// + /// Overload of - operator to remove a Recipe to a RecipeCollection. + /// + /// The recipe collection where the recipe will be removed + /// The recipe to remove + /// The recipe collection where the recipe is removed. + public static RecipeCollection operator - (RecipeCollection coll, Recipe ri) + { + coll.Collection.Remove(ri); return coll; + } + } +}