Advertisement
Antony_Jekov

Software Academy

Mar 25th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 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. using System.Collections.Generic;
  8.  
  9. // MOVED INTERFACES TO BOTTOM OF DOCUMENT TO GET THEM OUT OF THE WAY.
  10.  
  11. namespace SoftwareAcademy
  12. {
  13.     public abstract class Course : ICourse
  14.     {
  15.         private List<string> courseProgram = new List<string>(); // STORES THE TOPICS AS STRINGS
  16.         public string Name { get; set; }
  17.         public ITeacher Teacher { get; set; }
  18.  
  19.         public void AddTopic(string topic)
  20.         {
  21.             courseProgram.Add(topic);
  22.         }
  23.         // ONLY COLLECTS DATA KNOWN TO ITS ABSTRACTION LEVEL (WITHOUT LOCATION)
  24.         public override string ToString()
  25.         {
  26.             StringBuilder sb = new StringBuilder();
  27.             sb.Append(string.Format("{0}: Name={1}", this.GetType().Name, this.Name));
  28.             if (this.Teacher != null)
  29.             {
  30.                 sb.Append("; ");
  31.                 sb.Append(string.Format("Teacher={0}", this.Teacher.Name));
  32.             }
  33.             if (this.courseProgram.Count > 0)
  34.             {
  35.                 sb.Append("; ");
  36.                 sb.Append("Topics=[");
  37.                 for (int i = 0; i < courseProgram.Count; i++)
  38.                 {
  39.                     sb.Append(string.Format("{0}", courseProgram[i]));
  40.                     if (i != courseProgram.Count - 1)
  41.                     {
  42.                         sb.Append(", ");
  43.                     }
  44.                 }
  45.                 sb.Append("]");
  46.             }
  47.             return sb.ToString();
  48.         }
  49.     }
  50.  
  51.     public class LocalCourse : Course, ILocalCourse
  52.     {
  53.         public string Lab { get; set; }
  54.         // CONSIDERES WITH THE BASE TOSTRING() AND ADDS ITS CORRESPONDING DIFFERENCE
  55.         public override string ToString()
  56.         {
  57.             return string.Format("{0}; Lab={1}", base.ToString(), this.Lab);
  58.         }
  59.     }
  60.  
  61.     public class OffsiteCourse : Course, IOffsiteCourse
  62.     {
  63.         public string Town { get; set; }
  64.         // CONSIDERES WITH THE BASE TOSTRING() AND ADDS ITS CORRESPONDING DIFFERENCE
  65.         public override string ToString()
  66.         {
  67.             return string.Format("{0}; Town={1}", base.ToString(), this.Town);
  68.         }
  69.     }
  70.  
  71.     public class Teacher : ITeacher
  72.     {
  73.         List<ICourse> courses = new List<ICourse>(); // TEACHER REMEMBERS WHAT COURSES ARE ASSIGNED TO HIM
  74.         public string Name { get; set; }
  75.         public void AddCourse(ICourse course)
  76.         {
  77.             courses.Add(course);
  78.         }
  79.         // PUTS THE CONTENTS OF THE TEACHER IN A HUMAN READABLE FORMAT
  80.         public override string ToString()
  81.         {
  82.             StringBuilder sb = new StringBuilder();
  83.             sb.Append(string.Format("Teacher: Name={0}", this.Name));
  84.             if (courses.Count > 0)
  85.             {
  86.                 sb.Append("; Courses=[");
  87.                 for (int i = 0; i < courses.Count; i++)
  88.                 {
  89.                     sb.Append(string.Format("{0}", courses[i].Name));
  90.                     if (i != courses.Count - 1)
  91.                     {
  92.                         sb.Append(", ");
  93.                     }
  94.                 }
  95.                 sb.Append("]");
  96.             }
  97.             return sb.ToString();
  98.         }
  99.     }
  100.     // HANDLES THE CREATION OF ALL THE OBJECTS (FACTORY DESIGN PATTERN)
  101.     public class CourseFactory : ICourseFactory
  102.     {
  103.         // NOT USING CONSTRUCTORS - MUCH LESS CODE THAT WAY
  104.  
  105.         public ITeacher CreateTeacher(string name)
  106.         {
  107.             if (name == null)
  108.             {
  109.                 throw new ArgumentNullException();
  110.             }
  111.             Teacher teacher = new Teacher();
  112.             teacher.Name = name;
  113.             return teacher;
  114.         }
  115.  
  116.         public ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab)
  117.         {
  118.             if (name == null || lab == null)
  119.             {
  120.                 throw new ArgumentNullException();
  121.             }
  122.             LocalCourse courseLocal = new LocalCourse();
  123.             courseLocal.Name = name;
  124.             courseLocal.Teacher = teacher;
  125.             courseLocal.Lab = lab;
  126.             return courseLocal;
  127.         }
  128.  
  129.         public IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town)
  130.         {
  131.             if (name == null || town == null)
  132.             {
  133.                 throw new ArgumentNullException();
  134.             }
  135.             OffsiteCourse courseOffsite = new OffsiteCourse();
  136.             courseOffsite.Name = name;
  137.             courseOffsite.Teacher = teacher;
  138.             courseOffsite.Town = town;
  139.             return courseOffsite;
  140.         }
  141.     }
  142.     // -------------------------------------- FOLLOWING IS PREDEFINED CODE THAT HAS NOT BEEN ALTERED -----------------------------------------------
  143.  
  144.  
  145.  
  146.     public class SoftwareAcademyCommandExecutor
  147.     {
  148.         static void Main()
  149.         {
  150.             string csharpCode = ReadInputCSharpCode();
  151.             CompileAndRun(csharpCode);
  152.         }
  153.  
  154.         private static string ReadInputCSharpCode()
  155.         {
  156.             StringBuilder result = new StringBuilder();
  157.             string line;
  158.             while ((line = Console.ReadLine()) != "")
  159.             {
  160.                 result.AppendLine(line);
  161.             }
  162.             return result.ToString();
  163.         }
  164.  
  165.         static void CompileAndRun(string csharpCode)
  166.         {
  167.             // Prepare a C# program for compilation
  168.             string[] csharpClass =
  169.             {
  170.                 @"using System;
  171.                  using SoftwareAcademy;
  172.  
  173.                  public class RuntimeCompiledClass
  174.                  {
  175.                     public static void Main()
  176.                     {"
  177.                         + csharpCode + @"
  178.                     }
  179.                  }"
  180.             };
  181.  
  182.             // Compile the C# program
  183.             CompilerParameters compilerParams = new CompilerParameters();
  184.             compilerParams.GenerateInMemory = true;
  185.             compilerParams.TempFiles = new TempFileCollection(".");
  186.             compilerParams.ReferencedAssemblies.Add("System.dll");
  187.             compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
  188.             CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
  189.             CompilerResults compile = csharpProvider.CompileAssemblyFromSource(
  190.                 compilerParams, csharpClass);
  191.  
  192.             // Check for compilation errors
  193.             if (compile.Errors.HasErrors)
  194.             {
  195.                 string errorMsg = "Compilation error: ";
  196.                 foreach (CompilerError ce in compile.Errors)
  197.                 {
  198.                     errorMsg += "\r\n" + ce.ToString();
  199.                 }
  200.                 throw new Exception(errorMsg);
  201.             }
  202.  
  203.             // Invoke the Main() method of the compiled class
  204.             Assembly assembly = compile.CompiledAssembly;
  205.             Module module = assembly.GetModules()[0];
  206.             Type type = module.GetType("RuntimeCompiledClass");
  207.             MethodInfo methInfo = type.GetMethod("Main");
  208.             methInfo.Invoke(null, null);
  209.         }
  210.     }
  211.     // THE INTERFACES MOVED FROM TOP TO HERE
  212.     public interface ITeacher
  213.     {
  214.         string Name { get; set; }
  215.         void AddCourse(ICourse course);
  216.         string ToString();
  217.     }
  218.     public interface ICourse
  219.     {
  220.         string Name { get; set; }
  221.         ITeacher Teacher { get; set; }
  222.         void AddTopic(string topic);
  223.         string ToString();
  224.     }
  225.     public interface ILocalCourse : ICourse
  226.     {
  227.         string Lab { get; set; }
  228.     }
  229.     public interface IOffsiteCourse : ICourse
  230.     {
  231.         string Town { get; set; }
  232.     }
  233.     public interface ICourseFactory
  234.     {
  235.         ITeacher CreateTeacher(string name);
  236.         ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab);
  237.         IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town);
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement