Advertisement
Schnuk

Untitled

Feb 16th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6. public class CloneVersionSystem : ICloneVersionSystem
  7. {
  8. public CloneVersionSystem()
  9. {
  10. clones = new List<Clone> { new Clone() };
  11. }
  12.  
  13. private List<Clone> clones;
  14.  
  15. public string Execute(string query)
  16. {
  17. var stringParts = query.Split(' ');
  18.  
  19. string command = stringParts[0];
  20. int cloneId = int.Parse(stringParts[1]);
  21. switch (command)
  22. {
  23. case "learn":
  24. int programmId = int.Parse(stringParts[2]);
  25. clones[cloneId - 1].Learn(programmId);
  26. return null;
  27. case "rollback":
  28. clones[cloneId - 1].Rollback();
  29. return null;
  30. case "relearn":
  31. clones[cloneId - 1].Relearn();
  32. return null;
  33. case "clone":
  34. var oldClone = clones[cloneId - 1];
  35. clones.Add(new Clone(oldClone));
  36. return null;
  37. case "check":
  38. return clones[cloneId - 1].Check();
  39. default:
  40. return null;
  41. }
  42. }
  43. }
  44.  
  45. public class Clone
  46. {
  47. public LinkedStack historyOfRollbacks { get; set; }
  48. public LinkedStack learnedProgramms { get; set; }
  49.  
  50. public Clone()
  51. {
  52. historyOfRollbacks = new LinkedStack();
  53. learnedProgramms = new LinkedStack();
  54. }
  55.  
  56. public void Learn(int commandNumber)
  57. {
  58. learnedProgramms.Push(commandNumber);
  59. }
  60.  
  61. public void Rollback()
  62. {
  63. historyOfRollbacks.Push(learnedProgramms.Pop());
  64. }
  65.  
  66. public void Relearn()
  67. {
  68. learnedProgramms.Push(historyOfRollbacks.Pop());
  69. }
  70.  
  71. public Clone(Clone oldClone)
  72. {
  73. historyOfRollbacks = new LinkedStack(oldClone.historyOfRollbacks);
  74. learnedProgramms = new LinkedStack(oldClone.learnedProgramms);
  75. }
  76.  
  77. public string Check()
  78. {
  79. if (IsStackOfLearnedProgrammsEmpty())
  80. return "basic";
  81. return learnedProgramms.GetHeadValue().ToString();
  82. }
  83.  
  84. public bool IsStackOfLearnedProgrammsEmpty()
  85. {
  86. return learnedProgramms is E
  87. }
  88. }
  89.  
  90. public class LinkedStackItem
  91. {
  92. public int ItemValue { get; set; }
  93. public LinkedStackItem PreviousItem { get; set; }
  94. }
  95.  
  96. public class LinkedStack
  97. {
  98. LinkedStackItem head;
  99.  
  100. public LinkedStack() { }
  101.  
  102. public LinkedStack(LinkedStack linkedStack)
  103. {
  104. head = linkedStack.head;
  105. }
  106.  
  107. public void Push(int commandNumber)
  108. {
  109. var newItem = new LinkedStackItem { ItemValue = commandNumber, PreviousItem = head };
  110. head = newItem;
  111. }
  112.  
  113. public int Pop()
  114. {
  115. if (head == null) throw new InvalidOperationException();
  116. int commandNumber = head.ItemValue;
  117. head = head.PreviousItem;
  118. return commandNumber;
  119. }
  120.  
  121. public int GetHeadValue()
  122. {
  123. return head.ItemValue;
  124. }
  125. }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement