C# Assignment : Getting the Feel of Visitor Patterns: Due Monday 11/17/03 For this assignment you need to download: foods.cs : contains food item classes In addition, the file foodvisitors.cs and aboutfood.cs contain examples of how to define visitors and how to use them. You are to: 1. Append the foods.cs file with another fooditem class - such as beverage, with unique elements compared to fruit, vegetable and meat. Now, if you add the new "visit" declaration to the foodvisitor interface, you would have to change all existing visitors (in foodvisitors.cs) to also account for the new subclass. I suggest you do the following: create a new interface that extend the existing visitor interface: public interface extendedvisitor : foodvisitor Since an extendedvisitor will still be a foodvisitor, the existing visitors will still work, and the "accept" methods will be able to accept your extended visitors. However, you'll need to specify the new class you're going to visit in the extendedvisitor interface, and your extended visitor classes will visit all fooditems as well as all new fooditems. 2. Write an visitor class patterned after the foodvisistors I gave you, which visits each food item in the list and output the data in xml format. That is, the file should conform to the format shown in the sample xml file (fff.xml on homepage) that I gave you. You do, however, need to make up a valid xml format for your new fooditem (, etc ...). Your output must also have reasonable indentations (similar to mine). To open a file and write to it in C#, do: using System.IO; ... FileStream fs = new FileStream(@"filename",FileMode.OpenOrCreate,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.Write("hello\n"); // use sw just like Console sw.WriteLine("world"); ... // when done: sw.Flush(); sw.Close(); To write the " character, use sw.Write("\""); In the aboutfood.cs example I used the xmlwriter class I created myself, which takes a StreamWriter object as a constructor parameter. That's because I may not want to write to just a local file, but also to a network socket or some other IO channel. The word "stream" is another layer of abstraction, like most everything else in computer science. To create a dll from a file, use csc /t:library filename.cs To use a dll when compiling, use csc /r:filename1.dll filename2.cs You can also just name multiple .cs files in one csc command. 3. A food list is "ordered" if all fruits come before vegetables and all vegetables come before meats. Write a visitor class that determines if a food list is ordered, that is, (bool)myFoodList.accept(new OrderCheckerVisitor()) should be true or false. Hint: Here's one possible algorithm: you should have an instance variable that represents the "mode" you're in. That is, whether you are looking for more fruits, vegetables, or meat items. For example, if the current mode is vegetables and you find a meat item, then the mode changes to looking for more meat items. But if the current mode is vegetables and you find a fruit, you should return false immediately. Be aware that a list may also not have any fruits, or vegetables or meats, and can still be ordered. nofood by itself is ordered. Demonstrate the visitor on both ordered and unordered lists. 4. (optional) Write an xml parser for the food data (see students.cs for example).