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;
- using System.Collections.Generic;
- // MOVED INTERFACES TO BOTTOM OF DOCUMENT TO GET THEM OUT OF THE WAY.
- namespace SoftwareAcademy
- {
- public abstract class Course : ICourse
- {
- private List<string> courseProgram = new List<string>(); // STORES THE TOPICS AS STRINGS
- public string Name { get; set; }
- public ITeacher Teacher { get; set; }
- public void AddTopic(string topic)
- {
- courseProgram.Add(topic);
- }
- // ONLY COLLECTS DATA KNOWN TO ITS ABSTRACTION LEVEL (WITHOUT LOCATION)
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append(string.Format("{0}: Name={1}", this.GetType().Name, this.Name));
- if (this.Teacher != null)
- {
- sb.Append("; ");
- sb.Append(string.Format("Teacher={0}", this.Teacher.Name));
- }
- if (this.courseProgram.Count > 0)
- {
- sb.Append("; ");
- sb.Append("Topics=[");
- for (int i = 0; i < courseProgram.Count; i++)
- {
- sb.Append(string.Format("{0}", courseProgram[i]));
- if (i != courseProgram.Count - 1)
- {
- sb.Append(", ");
- }
- }
- sb.Append("]");
- }
- return sb.ToString();
- }
- }
- public class LocalCourse : Course, ILocalCourse
- {
- public string Lab { get; set; }
- // CONSIDERES WITH THE BASE TOSTRING() AND ADDS ITS CORRESPONDING DIFFERENCE
- public override string ToString()
- {
- return string.Format("{0}; Lab={1}", base.ToString(), this.Lab);
- }
- }
- public class OffsiteCourse : Course, IOffsiteCourse
- {
- public string Town { get; set; }
- // CONSIDERES WITH THE BASE TOSTRING() AND ADDS ITS CORRESPONDING DIFFERENCE
- public override string ToString()
- {
- return string.Format("{0}; Town={1}", base.ToString(), this.Town);
- }
- }
- public class Teacher : ITeacher
- {
- List<ICourse> courses = new List<ICourse>(); // TEACHER REMEMBERS WHAT COURSES ARE ASSIGNED TO HIM
- public string Name { get; set; }
- public void AddCourse(ICourse course)
- {
- courses.Add(course);
- }
- // PUTS THE CONTENTS OF THE TEACHER IN A HUMAN READABLE FORMAT
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append(string.Format("Teacher: Name={0}", this.Name));
- if (courses.Count > 0)
- {
- sb.Append("; Courses=[");
- for (int i = 0; i < courses.Count; i++)
- {
- sb.Append(string.Format("{0}", courses[i].Name));
- if (i != courses.Count - 1)
- {
- sb.Append(", ");
- }
- }
- sb.Append("]");
- }
- return sb.ToString();
- }
- }
- // HANDLES THE CREATION OF ALL THE OBJECTS (FACTORY DESIGN PATTERN)
- public class CourseFactory : ICourseFactory
- {
- // NOT USING CONSTRUCTORS - MUCH LESS CODE THAT WAY
- public ITeacher CreateTeacher(string name)
- {
- if (name == null)
- {
- throw new ArgumentNullException();
- }
- Teacher teacher = new Teacher();
- teacher.Name = name;
- return teacher;
- }
- public ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab)
- {
- if (name == null || lab == null)
- {
- throw new ArgumentNullException();
- }
- LocalCourse courseLocal = new LocalCourse();
- courseLocal.Name = name;
- courseLocal.Teacher = teacher;
- courseLocal.Lab = lab;
- return courseLocal;
- }
- public IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town)
- {
- if (name == null || town == null)
- {
- throw new ArgumentNullException();
- }
- OffsiteCourse courseOffsite = new OffsiteCourse();
- courseOffsite.Name = name;
- courseOffsite.Teacher = teacher;
- courseOffsite.Town = town;
- return courseOffsite;
- }
- }
- // -------------------------------------- FOLLOWING IS PREDEFINED CODE THAT HAS NOT BEEN ALTERED -----------------------------------------------
- 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);
- }
- }
- // THE INTERFACES MOVED FROM TOP TO HERE
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement