Advertisement
JohnByte

CloneVersionSystem

Oct 19th, 2017
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Clones
  6. {
  7.     public class CloneVersionSystem : ICloneVersionSystem
  8.     {
  9.         static List<Clone> clones = new List<Clone>() { new Clone() };
  10.  
  11.         class Clone
  12.         {
  13.             List<string> programs;
  14.             List<string> undos;
  15.             int number;
  16.  
  17.             internal Clone()
  18.             {
  19.                 programs = new List<string>();
  20.                 programs.Add("basic");
  21.                 undos = new List<string>();
  22.                 number = 1;
  23.             }
  24.  
  25.             private Clone(int num)
  26.             {
  27.                 for (int i = 0; i < clones.Count; i++)
  28.                 {
  29.                     if (clones[i].number == num)
  30.                     {
  31.                         programs = new List<string>();
  32.                         for (int j = 0; j < clones[i].programs.Count; j++)
  33.                         {
  34.                             programs.Add(clones[i].programs[j]);
  35.                         }
  36.  
  37.                         undos = new List<string>();
  38.                         for (int j = 0; j < clones[i].undos.Count; j++)
  39.                         {
  40.                             undos.Add(clones[i].undos[j]);
  41.                         }
  42.  
  43.                         number = clones.Count + 1;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             internal void Learn(string pi)
  50.             {
  51.                 programs.Add(pi);
  52.                 while (undos.Count > 0)
  53.                 {
  54.                     undos.RemoveAt(undos.Count - 1);
  55.                 }
  56.             }
  57.  
  58.             internal void Rollback()
  59.             {
  60.                 var prg = programs[programs.Count - 1];
  61.                 if (prg != "basic")
  62.                 {
  63.                     undos.Add(prg);
  64.                     programs.RemoveAt(programs.Count - 1);
  65.                 }
  66.             }
  67.  
  68.             internal void Relearn()
  69.             {
  70.                 if (undos.Count > 0)
  71.                 {
  72.                     programs.Add(undos[undos.Count - 1]);
  73.                     undos.RemoveAt(undos.Count - 1);
  74.                 }
  75.             }
  76.  
  77.             internal void Clonefy()
  78.             {
  79.                 Clone clone = new Clone(number);
  80.                 clones.Add(clone);
  81.             }
  82.  
  83.             internal string Check()
  84.             {
  85.                 return programs[programs.Count - 1];
  86.             }
  87.         }
  88.  
  89.         public string Execute(string query)
  90.         {
  91.             string[] tokens = query.Split(' ');
  92.             int numClone = int.Parse(tokens[1]) - 1;
  93.             switch (tokens[0])
  94.             {
  95.                 case "learn":
  96.                     clones[numClone].Learn(tokens[2]);
  97.                     break;
  98.                 case "rollback":
  99.                     clones[numClone].Rollback();
  100.                     break;
  101.                 case "relearn":
  102.                     clones[numClone].Relearn();
  103.                     break;
  104.                 case "clone":
  105.                     clones[numClone].Clonefy();
  106.                     break;
  107.                 case "check":
  108.                     return clones[numClone].Check();
  109.             }
  110.  
  111.             return null;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement