Advertisement
xellscream

SoftwareAcademy

Mar 25th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.CodeDom.Compiler;
  5. using Microsoft.CSharp;
  6. using System.Reflection;
  7.  
  8. namespace SoftwareAcademy
  9. {
  10. public interface ITeacher
  11. {
  12. string Name { get; set; }
  13. void AddCourse(ICourse course);
  14. string ToString();
  15. }
  16.  
  17. public interface ICourse
  18. {
  19. string Name { get; set; }
  20. ITeacher Teacher { get; set; }
  21. void AddTopic(string topic);
  22. string ToString();
  23. }
  24.  
  25. public interface ILocalCourse : ICourse
  26. {
  27. string Lab { get; set; }
  28. }
  29.  
  30. public interface IOffsiteCourse : ICourse
  31. {
  32. string Town { get; set; }
  33. }
  34.  
  35. public interface ICourseFactory
  36. {
  37. ITeacher CreateTeacher(string name);
  38. ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab);
  39. IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town);
  40. }
  41.  
  42. public class CourseFactory : ICourseFactory
  43. {
  44. public ITeacher CreateTeacher(string name)
  45. {
  46. return new Teacher(name);
  47. }
  48.  
  49. public ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab)
  50. {
  51. return new LocalCourse(name, teacher, lab);
  52. }
  53. public IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town)
  54. {
  55. return new OffsiteCourse(name, teacher, town);
  56. }
  57. }
  58.  
  59. public class SoftwareAcademyCommandExecutor
  60. {
  61. static void Main()
  62. {
  63. string csharpCode = ReadInputCSharpCode();
  64. CompileAndRun(csharpCode);
  65. }
  66.  
  67. private static string ReadInputCSharpCode()
  68. {
  69. StringBuilder result = new StringBuilder();
  70. string line;
  71. while ((line = Console.ReadLine()) != "")
  72. {
  73. result.AppendLine(line);
  74. }
  75. return result.ToString();
  76. }
  77.  
  78. static void CompileAndRun(string csharpCode)
  79. {
  80. // Prepare a C# program for compilation
  81. string[] csharpClass =
  82. {
  83. @"using System;
  84. using SoftwareAcademy;
  85.  
  86. public class RuntimeCompiledClass
  87. {
  88. public static void Main()
  89. {"
  90. + csharpCode + @"
  91. }
  92. }"
  93. };
  94.  
  95. // Compile the C# program
  96. CompilerParameters compilerParams = new CompilerParameters();
  97. compilerParams.GenerateInMemory = true;
  98. compilerParams.TempFiles = new TempFileCollection(".");
  99. compilerParams.ReferencedAssemblies.Add("System.dll");
  100. compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
  101. CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
  102. CompilerResults compile = csharpProvider.CompileAssemblyFromSource(
  103. compilerParams, csharpClass);
  104.  
  105. // Check for compilation errors
  106. if (compile.Errors.HasErrors)
  107. {
  108. string errorMsg = "Compilation error: ";
  109. foreach (CompilerError ce in compile.Errors)
  110. {
  111. errorMsg += "\r\n" + ce.ToString();
  112. }
  113. throw new Exception(errorMsg);
  114. }
  115.  
  116. // Invoke the Main() method of the compiled class
  117. Assembly assembly = compile.CompiledAssembly;
  118. Module module = assembly.GetModules()[0];
  119. Type type = module.GetType("RuntimeCompiledClass");
  120. MethodInfo methInfo = type.GetMethod("Main");
  121. methInfo.Invoke(null, null);
  122. }
  123. }
  124.  
  125. public class Teacher : ITeacher
  126. {
  127. public string Name { get; set; }
  128. public ICourse Course;
  129. public int count = 0;
  130. public ICourse anotherCourse;
  131.  
  132. public Teacher(string name)
  133. {
  134. this.Name = name;
  135. }
  136.  
  137. public void AddCourse(ICourse course)
  138. {
  139. if (count == 0)
  140. {
  141. this.Course = course;
  142. }
  143. else
  144. {
  145. this.anotherCourse = course;
  146. }
  147. count++;
  148. }
  149.  
  150. public override string ToString()
  151. {
  152. StringBuilder theTeacher = new StringBuilder();
  153. theTeacher.Append(this.GetType().Name);
  154. theTeacher.Append(": ");
  155. if (Course == null)
  156. {
  157. theTeacher.Append("Name=");
  158. theTeacher.Append(Name);
  159. }
  160. else
  161. {
  162. throw new ArgumentNullException();
  163. }
  164. if (count != 0)
  165. {
  166. theTeacher.Append(Name);
  167. theTeacher.Append("; ");
  168. theTeacher.Append("Courses=[");
  169. theTeacher.Append(Course.Name + ", " + anotherCourse.Name + "]");
  170. }
  171. else
  172. {
  173. theTeacher.Append(Name);
  174. theTeacher.Append("; ");
  175. theTeacher.Append("Courses=[");
  176. theTeacher.Append(Course.Name + ", " + "]");
  177. }
  178. count = 0;
  179. return theTeacher.ToString();
  180.  
  181. }
  182. }
  183. public class OffsiteCourse : ICourse, IOffsiteCourse
  184. {
  185. public string Name { get; set; }
  186. public ITeacher Teacher { get; set; }
  187. public string Topic;
  188. public string Town { get; set; }
  189.  
  190. public OffsiteCourse(string name, ITeacher teacher, string town)
  191. {
  192. this.Name = name;
  193. this.Teacher = teacher;
  194. this.Town = town;
  195. }
  196.  
  197. public void AddTopic(string topic)
  198. {
  199. this.Topic = topic;
  200. }
  201. public override string ToString()
  202. {
  203. StringBuilder theOffsiteCourse = new StringBuilder();
  204. theOffsiteCourse.Append(this.GetType().Name);
  205.  
  206. if (Teacher != null)
  207. {
  208. theOffsiteCourse.Append("Teacher=");
  209. theOffsiteCourse.Append(Teacher);
  210. theOffsiteCourse.Append("; ");
  211.  
  212. if (Name != null)
  213. {
  214. theOffsiteCourse.Append("Name=");
  215. theOffsiteCourse.Append(Name);
  216. theOffsiteCourse.Append("; ");
  217. }
  218. if (Topic != null)
  219. {
  220. theOffsiteCourse.Append("Topics=[");
  221. theOffsiteCourse.Append(Topic);
  222. theOffsiteCourse.Append("]; ");
  223. }
  224. if (Town != null)
  225. {
  226. theOffsiteCourse.Append("Town=");
  227. theOffsiteCourse.Append(Town);
  228. }
  229. else
  230. {
  231. throw new ArgumentNullException();
  232. }
  233.  
  234. return theOffsiteCourse.ToString();
  235. }
  236. else
  237. {
  238. return null;
  239. }
  240.  
  241. }
  242. }
  243.  
  244. public class LocalCourse : ICourse, ILocalCourse
  245. {
  246. public string Name { get; set; }
  247. public ITeacher Teacher { get; set; }
  248. public string Topic;
  249. public string Lab { get; set; }
  250.  
  251. public LocalCourse(string name, ITeacher teacher, string lab)
  252. {
  253. this.Name = name;
  254. this.Teacher = teacher;
  255. this.Lab = lab;
  256. }
  257.  
  258. public void AddTopic(string topic)
  259. {
  260. this.Topic = topic;
  261. }
  262.  
  263. public override string ToString()
  264. {
  265. StringBuilder theLocalCourse = new StringBuilder();
  266. theLocalCourse.Append(this.GetType().Name);
  267. if (Name != null)
  268. {
  269. theLocalCourse.Append(": Name=" + Name + "; ");
  270. }
  271. if (Teacher != null)
  272. {
  273. theLocalCourse.Append("Teacher=" + Teacher.Name + "; ");
  274. }
  275. if (Topic != null)
  276. {
  277. theLocalCourse.Append("Topics=[" + Topic + "]; ");
  278. }
  279. if (Lab != null)
  280. {
  281. theLocalCourse.Append("Lab=" + Lab);
  282. }
  283. else
  284. {
  285. throw new ArgumentNullException();
  286. }
  287. return theLocalCourse.ToString();
  288. }
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement