Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.CodeDom.Compiler;
- using Microsoft.CSharp;
- using System.Reflection;
- namespace SoftwareAcademy
- {
- public interface ITeacher
- {
- string Name { get; set; }
- void AddCourse(ICourse course);
- string ToString();
- }
- public interface ICourse
- {
- string Name { get; set; }
- ITeacher Teacher { get; set; }
- void AddTopic(string topic);
- string ToString();
- }
- public interface ILocalCourse : ICourse
- {
- string Lab { get; set; }
- }
- public interface IOffsiteCourse : ICourse
- {
- string Town { get; set; }
- }
- public interface ICourseFactory
- {
- ITeacher CreateTeacher(string name);
- ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab);
- IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town);
- }
- public class CourseFactory : ICourseFactory
- {
- public ITeacher CreateTeacher(string name)
- {
- return new Teacher(name);
- }
- public ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab)
- {
- return new LocalCourse(name, teacher, lab);
- }
- public IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town)
- {
- return new OffsiteCourse(name, teacher, town);
- }
- }
- public class SoftwareAcademyCommandExecutor
- {
- static void Main()
- {
- string csharpCode = ReadInputCSharpCode();
- CompileAndRun(csharpCode);
- }
- private static string ReadInputCSharpCode()
- {
- StringBuilder result = new StringBuilder();
- string line;
- while ((line = Console.ReadLine()) != "")
- {
- result.AppendLine(line);
- }
- return result.ToString();
- }
- static void CompileAndRun(string csharpCode)
- {
- // Prepare a C# program for compilation
- string[] csharpClass =
- {
- @"using System;
- using SoftwareAcademy;
- public class RuntimeCompiledClass
- {
- public static void Main()
- {"
- + csharpCode + @"
- }
- }"
- };
- // Compile the C# program
- CompilerParameters compilerParams = new CompilerParameters();
- compilerParams.GenerateInMemory = true;
- compilerParams.TempFiles = new TempFileCollection(".");
- compilerParams.ReferencedAssemblies.Add("System.dll");
- compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
- CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
- CompilerResults compile = csharpProvider.CompileAssemblyFromSource(
- compilerParams, csharpClass);
- // Check for compilation errors
- if (compile.Errors.HasErrors)
- {
- string errorMsg = "Compilation error: ";
- foreach (CompilerError ce in compile.Errors)
- {
- errorMsg += "\r\n" + ce.ToString();
- }
- throw new Exception(errorMsg);
- }
- // Invoke the Main() method of the compiled class
- Assembly assembly = compile.CompiledAssembly;
- Module module = assembly.GetModules()[0];
- Type type = module.GetType("RuntimeCompiledClass");
- MethodInfo methInfo = type.GetMethod("Main");
- methInfo.Invoke(null, null);
- }
- }
- public class Teacher : ITeacher
- {
- public string Name { get; set; }
- public ICourse Course;
- public int count = 0;
- public ICourse anotherCourse;
- public Teacher(string name)
- {
- this.Name = name;
- }
- public void AddCourse(ICourse course)
- {
- if (count == 0)
- {
- this.Course = course;
- }
- else
- {
- this.anotherCourse = course;
- }
- count++;
- }
- public override string ToString()
- {
- StringBuilder theTeacher = new StringBuilder();
- theTeacher.Append(this.GetType().Name);
- theTeacher.Append(": ");
- if (Course == null)
- {
- theTeacher.Append("Name=");
- theTeacher.Append(Name);
- }
- else
- {
- throw new ArgumentNullException();
- }
- if (count != 0)
- {
- theTeacher.Append(Name);
- theTeacher.Append("; ");
- theTeacher.Append("Courses=[");
- theTeacher.Append(Course.Name + ", " + anotherCourse.Name + "]");
- }
- else
- {
- theTeacher.Append(Name);
- theTeacher.Append("; ");
- theTeacher.Append("Courses=[");
- theTeacher.Append(Course.Name + ", " + "]");
- }
- count = 0;
- return theTeacher.ToString();
- }
- }
- public class OffsiteCourse : ICourse, IOffsiteCourse
- {
- public string Name { get; set; }
- public ITeacher Teacher { get; set; }
- public string Topic;
- public string Town { get; set; }
- public OffsiteCourse(string name, ITeacher teacher, string town)
- {
- this.Name = name;
- this.Teacher = teacher;
- this.Town = town;
- }
- public void AddTopic(string topic)
- {
- this.Topic = topic;
- }
- public override string ToString()
- {
- StringBuilder theOffsiteCourse = new StringBuilder();
- theOffsiteCourse.Append(this.GetType().Name);
- if (Teacher != null)
- {
- theOffsiteCourse.Append("Teacher=");
- theOffsiteCourse.Append(Teacher);
- theOffsiteCourse.Append("; ");
- if (Name != null)
- {
- theOffsiteCourse.Append("Name=");
- theOffsiteCourse.Append(Name);
- theOffsiteCourse.Append("; ");
- }
- if (Topic != null)
- {
- theOffsiteCourse.Append("Topics=[");
- theOffsiteCourse.Append(Topic);
- theOffsiteCourse.Append("]; ");
- }
- if (Town != null)
- {
- theOffsiteCourse.Append("Town=");
- theOffsiteCourse.Append(Town);
- }
- else
- {
- throw new ArgumentNullException();
- }
- return theOffsiteCourse.ToString();
- }
- else
- {
- return null;
- }
- }
- }
- public class LocalCourse : ICourse, ILocalCourse
- {
- public string Name { get; set; }
- public ITeacher Teacher { get; set; }
- public string Topic;
- public string Lab { get; set; }
- public LocalCourse(string name, ITeacher teacher, string lab)
- {
- this.Name = name;
- this.Teacher = teacher;
- this.Lab = lab;
- }
- public void AddTopic(string topic)
- {
- this.Topic = topic;
- }
- public override string ToString()
- {
- StringBuilder theLocalCourse = new StringBuilder();
- theLocalCourse.Append(this.GetType().Name);
- if (Name != null)
- {
- theLocalCourse.Append(": Name=" + Name + "; ");
- }
- if (Teacher != null)
- {
- theLocalCourse.Append("Teacher=" + Teacher.Name + "; ");
- }
- if (Topic != null)
- {
- theLocalCourse.Append("Topics=[" + Topic + "]; ");
- }
- if (Lab != null)
- {
- theLocalCourse.Append("Lab=" + Lab);
- }
- else
- {
- throw new ArgumentNullException();
- }
- return theLocalCourse.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement