Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

CooksMate

Rate me:
Please Sign up or sign in to vote.
3.32/5 (8 votes)
21 Jan 2008GPL32 min read 56.2K   1K   23  
A simple program to help get the timing of a roast dinner
using System;
using System.Collections;
using System.Xml;

namespace uk.org.aspellclark.cooksmate.engine
{
	/// <summary>
	/// Summary description for Recipe.
	/// </summary>
	public class Recipe
	{
        #region members

        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        

		private bool deleted = false;
		private string title;
		private DateTime serveTime;
		private ArrayList steps;
		private string notes;

		#endregion

		public DateTime ServeTime
		{
			get{return serveTime;}
			set{serveTime = value;}
		}
		public string Title
		{
			get{return title;}
			set{title = value;}
		}
		public ArrayList Steps
		{
			get{return steps;}
			set{steps = value;}
		}
		public string Notes
		{
			get{return notes;}
			set{notes = value;}
		}
		public bool Deleted
		{
			get{return deleted;}
			set{deleted = value;}
		}

		public ActionStep GetAction(string name)
		{
			foreach (ActionStep stp in this.Steps)
			{
				if (stp.Name.ToLower().Equals(name.ToLower()))
				{
					return stp;
				}
			}
			return null;
		}

		
		public void ActionDone(ActionStep step)
		{
			step.Done = true;
			this.steps.Remove(step);
			this.steps.Add(step);
		}
		
		
		public Recipe()
		{
			steps = new ArrayList();
		}
		public Recipe(string name)
		{
			this.Title = name;
			steps = new ArrayList();
		}

		#region XMLCode

		public Recipe(XmlReader reader)
		{
			steps = new ArrayList();
			
			while (reader.Read())
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.XmlDeclaration:
						break;
					case XmlNodeType.ProcessingInstruction:
						break;
					case XmlNodeType.DocumentType:
						break;
					case XmlNodeType.Comment:
						break;
					case XmlNodeType.EndElement:
						if (reader.Name.Equals(Constants.XML_TAG_RECIPE))
						{
							return;
						}
						break;
					case XmlNodeType.Element:
						if (reader.Name.Equals(Constants.XML_TAG_NAME))
						{
							reader.Read();
							this.Title = reader.Value;
						}
						else if (reader.Name.Equals(Constants.XML_TAG_SERVE_TIME))
						{
							reader.Read();
							this.ServeTime = Convert.ToDateTime(reader.Value);
						}
						else if (reader.Name.Equals(Constants.XML_TAG_NOTES))
						{
							reader.Read();
							this.notes = reader.Value;
						}
						else if (reader.Name.Equals(Constants.XML_TAG_ACTIONS))
						{
							ReadActionsFromXml(reader);
						}
						break;
					case XmlNodeType.Text:
						break;
					case XmlNodeType.Whitespace:
						break;
				}
			}
		}
		
		private void ReadActionsFromXml(XmlReader reader)
		{
			while (reader.Read())
			{
				//log.Info(String.Format("xml tag [{0}], [{1}], ({2})",
				//                 reader.NodeType,
        	    //                 reader.Name,
        	    //                 reader.Value));
				switch (reader.NodeType)
				{
					case XmlNodeType.EndElement:
						if (reader.Name.Equals(Constants.XML_TAG_ACTIONS))
						{
							return;
						}
						break;
					case XmlNodeType.Element:
						if (reader.Name.Equals(Constants.XML_TAG_ACTION))
						{
							ActionStep tmpStep = new ActionStep(reader);
							this.steps.Add(tmpStep);
							log.Info(String.Format("read in step {0} [{1}]", steps.Count, tmpStep.ToString()));
						}
						break;
				}
			}
		}

		public void WriteToXml(XmlWriter xmlWriter)
		{
            xmlWriter.WriteStartElement(Constants.XML_TAG_RECIPE);

            xmlWriter.WriteElementString(Constants.XML_TAG_NAME, this.Title);
            xmlWriter.WriteElementString(Constants.XML_TAG_SERVE_TIME, this.ServeTime.ToString());
            
            xmlWriter.WriteStartElement(Constants.XML_TAG_NOTES);
            xmlWriter.WriteString(this.Notes);
            xmlWriter.WriteEndElement();
            
            xmlWriter.WriteStartElement(Constants.XML_TAG_ACTIONS);
            foreach (ActionStep action in steps)
            {
            	action.WriteToXml(xmlWriter);
            }
            xmlWriter.WriteEndElement();

            xmlWriter.WriteEndElement();
		}		
		
		#endregion

		
		
		public string ToString(DateTime srveTime)
		{
            return String.Format("{0}, {1:d}", 
        	                     this.Title,
        	                     this.ServeTime);
        }

	}//class
}//namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Airbus Defense and Space
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions