Guest User

CVS

a guest
Mar 22nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Clones
  6. {
  7.     public class CloneVersionSystem : ICloneVersionSystem
  8.     {
  9.         public static List<Clone> clones;
  10.  
  11.         public string Execute(string query)
  12.         {
  13.             if (query.Split()[0] == "learn")
  14.             {
  15.                 var newClone = new Clone
  16.                 {
  17.                     CloneID = int.Parse(query.Split()[1]),
  18.                     ProgrammID = query.Split()[2]
  19.                 };
  20.                 clones.Add(newClone);
  21.             }
  22.  
  23.             if (query.Split()[0] == "rollback")
  24.             {
  25.                 foreach (var clone in clones)
  26.                 {
  27.                     if (clone.CloneID == int.Parse(query.Split()[1]))
  28.                     {
  29.                         clone.SuccessfulActionStack.RemoveLast();
  30.                         clone.ProgrammID = clone.SuccessfulActionStack.Last().ProgrammID;
  31.                         clone.FailedActionStack.Push(new ActionInfo("rollback", clone.CloneID, clone.ProgrammID));
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             if (query.Split()[0] == "relearn")
  37.             {
  38.                 foreach (var clone in clones)
  39.                 {
  40.                     if (clone.CloneID == int.Parse(query.Split()[1]))
  41.                     {
  42.                         clone.ProgrammID = clone.FailedActionStack.Last().ProgrammID;
  43.                         clone.FailedActionStack.Pop();
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             if (query.Split()[0] == "clone")
  49.             {
  50.                 var newClone = new Clone() { CloneID = int.Parse(query.Split()[1] + 1) };
  51.                 foreach (var clone in clones)
  52.                     if (clone.CloneID == int.Parse(query.Split()[1]))
  53.                         newClone.ProgrammID = clone.ProgrammID;
  54.                 clones.Add(newClone);
  55.             }
  56.  
  57.             if (query.Split()[0] == "check")
  58.             {
  59.                 foreach (var clone in clones)
  60.                     if (clone.CloneID == int.Parse(query.Split()[1]))
  61.                         return clone.ProgrammID;
  62.             }
  63.  
  64.             return null;
  65.         }
  66.     }
  67.  
  68.     public class Clone
  69.     {
  70.         public int CloneID { get; set; }
  71.         public string ProgrammID { get; set; }
  72.         public LimitedSizeStack<ActionInfo> SuccessfulActionStack { get; set; }
  73.         public LimitedSizeStack<ActionInfo> FailedActionStack { get; set; }
  74.  
  75.         public Clone()
  76.         {
  77.  
  78.         }
  79.     }
  80.  
  81.     public class ActionInfo
  82.     {
  83.         public string ActionName { get; set; }
  84.         public int CloneID { get; set; }
  85.         public string ProgrammID { get; set; }
  86.  
  87.         public ActionInfo(string actionName, int cloneID, string programmID)
  88.         {
  89.             ActionName = actionName;
  90.             CloneID = cloneID;
  91.             ProgrammID = programmID;
  92.         }
  93.     }
  94.  
  95.     public class LimitedSizeStack<T>
  96.     {
  97.         private int StackLimit { get; set; }
  98.         private static LinkedList<T> linkedList;
  99.  
  100.         public LimitedSizeStack(int limit)
  101.         {
  102.             StackLimit = limit;
  103.             linkedList = new LinkedList<T>();
  104.         }
  105.  
  106.         public void Push(T item)
  107.         {
  108.             if (this.Count + 1 > StackLimit)
  109.                 linkedList.RemoveFirst();
  110.             linkedList.AddLast(item);
  111.         }
  112.  
  113.         public T Pop()
  114.         {
  115.             var last = linkedList.Last;
  116.             linkedList.RemoveLast();
  117.             return last.Value;
  118.         }
  119.  
  120.         public void RemoveLast()
  121.         {
  122.             linkedList.RemoveLast();
  123.         }
  124.  
  125.         public int Count
  126.         {
  127.             get { return linkedList.Count; }
  128.         }
  129.  
  130.         public T Last()
  131.         {
  132.             return linkedList.Last.Value;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment