/// <summary> /// Adds new shape sequence to the shapeList list. /// </summary> /// <param name="seq">New sequence that is to be added. </param> public void AddNewSequence(string seq) { string[] sequence = seq.Split(' '); List <Shapes> tempShapeList = new List <Shapes>(); for (int i = 0; i < sequence.Length; i++) { if (sequence[i] == "s") { Shapes newShape = new Square((i + 1) * this.defaultSize); tempShapeList.Add(newShape); } else if (sequence[i] == "c") { Shapes newShape = new Circle((i + 1) * this.defaultSize); tempShapeList.Add(newShape); } else if (sequence[i] == "r") { Shapes newShape = new Rectangle((i + 1) * this.defaultSize); tempShapeList.Add(newShape); } else if (sequence[i] == "t") { Shapes newShape = new Trapezium((i + 1) * this.defaultSize); tempShapeList.Add(newShape); } else if (sequence[i] == "p") { Shapes newShape = new Pentagon((i + 1) * this.defaultSize); tempShapeList.Add(newShape); } } this.shapeList.Add(tempShapeList); string newSeq = string.Join(" ", seq); this.SeqList.Add(newSeq); // adding the new sequence to the seqList list of Shapes class. }
/// <summary> /// Modifies or removes the sequence in dictionary and updates sequence list. /// </summary> /// <param name="seqNum">Sequence number of sequence that needs to be modified. </param> /// <param name="seq">New sequence. </param> /// <param name="ind">Used only in partial modification, contains the index of sequence which is to be modified.</param> public void ModifySequence(int seqNum, string seq, int ind) { // modify the seqeunce at seqNum by replacing shape with replaceShape(user input, passed as parameter) at mentioned index(user input, passed as parameter). string[] sequence = this.seqList[seqNum - 1].Split(' '); sequence[ind - 1] = seq; // updating sequence list this.seqList[seqNum - 1] = string.Join(" ", sequence); Shapes newShape = null; // get the shape's default size. double defSize = this.shapeList[seqNum - 1][ind - 1].Size(); if (seq == "c") { newShape = new Circle(defSize); } else if (seq == "r") { newShape = new Rectangle(defSize); } else if (seq == "s") { newShape = new Square(defSize); } else if (seq == "p") { newShape = new Pentagon(defSize); } else if (seq == "t") { newShape = new Trapezium(defSize); } this.shapeList[seqNum - 1][ind - 1] = newShape; }
/// <summary> /// Loads from an xml file. /// </summary> public void Load() { // setting the initial values. this.defaultSize = 10; this.shapeList.Clear(); this.seqList.Clear(); this.undoStack.Clear(); this.redoStack.Clear(); XmlDocument document = new XmlDocument(); document.Load("output.xml"); // looping to the nesting level of tree and making the sequence with the values encountered. foreach (XmlNode node in document.DocumentElement) { List <Shapes> shapeListTemp = new List <Shapes>(); List <string> seqListTemp = new List <string>(); for (var i = 0; i < node.ChildNodes.Count; i++) { Shapes newShape = null; double shapeSize = 0; for (var j = 0; j < node.ChildNodes[i].ChildNodes.Count; j++) { if (node.ChildNodes[i].ChildNodes[j].Name == "ShapeType") { if (node.ChildNodes[i].ChildNodes[j].InnerText == "rectangle") { newShape = new Rectangle(shapeSize); seqListTemp.Add("r"); } else if (node.ChildNodes[i].ChildNodes[j].InnerText == "square") { newShape = new Square(shapeSize); seqListTemp.Add("s"); } else if (node.ChildNodes[i].ChildNodes[j].InnerText == "circle") { newShape = new Circle(shapeSize); seqListTemp.Add("c"); } else if (node.ChildNodes[i].ChildNodes[j].InnerText == "pentagon") { newShape = new Pentagon(shapeSize); seqListTemp.Add("p"); } else if (node.ChildNodes[i].ChildNodes[j].InnerText == "trapezium") { newShape = new Trapezium(shapeSize); seqListTemp.Add("t"); } } else if (node.ChildNodes[i].ChildNodes[j].Name == "Size") { shapeSize = Convert.ToDouble(node.ChildNodes[i].ChildNodes[j].InnerText); } else if (node.ChildNodes[i].ChildNodes[j].Name == "Color") { newShape.Color = node.ChildNodes[i].ChildNodes[j].InnerText; } else if (node.ChildNodes[i].ChildNodes[j].Name == "Thickness") { newShape.Thickness = Convert.ToDouble(node.ChildNodes[i].ChildNodes[j].InnerText); } else if (node.ChildNodes[i].ChildNodes[j].Name == "Pattern") { newShape.Pattern = node.ChildNodes[i].ChildNodes[j].InnerText; } } shapeListTemp.Add(newShape); } this.seqList.Add(string.Join(" ", seqListTemp)); this.shapeList.Add(shapeListTemp); seqListTemp = null; shapeListTemp = null; } Console.WriteLine("Loaded successfully"); }