Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clones
- {
- public class CloneVersionSystem : ICloneVersionSystem
- {
- public CloneVersionSystem()
- {
- clones = new List<Clone> { new Clone() };
- }
- private List<Clone> clones;
- public string Execute(string query)
- {
- var stringParts = query.Split(' ');
- string command = stringParts[0];
- int cloneId = int.Parse(stringParts[1]);
- switch (command)
- {
- case "learn":
- int programmId = int.Parse(stringParts[2]);
- clones[cloneId - 1].Learn(programmId);
- return null;
- case "rollback":
- clones[cloneId - 1].Rollback();
- return null;
- case "relearn":
- clones[cloneId - 1].Relearn();
- return null;
- case "clone":
- var oldClone = clones[cloneId - 1];
- clones.Add(new Clone(oldClone));
- return null;
- case "check":
- return clones[cloneId - 1].Check();
- default:
- return null;
- }
- }
- }
- public class Clone
- {
- public LinkedStack historyOfRollbacks { get; set; }
- public LinkedStack learnedProgramms { get; set; }
- public Clone()
- {
- historyOfRollbacks = new LinkedStack();
- learnedProgramms = new LinkedStack();
- }
- public void Learn(int commandNumber)
- {
- learnedProgramms.Push(commandNumber);
- }
- public void Rollback()
- {
- historyOfRollbacks.Push(learnedProgramms.Pop());
- }
- public void Relearn()
- {
- learnedProgramms.Push(historyOfRollbacks.Pop());
- }
- public Clone(Clone oldClone)
- {
- historyOfRollbacks = new LinkedStack(oldClone.historyOfRollbacks);
- learnedProgramms = new LinkedStack(oldClone.learnedProgramms);
- }
- public string Check()
- {
- if (IsStackOfLearnedProgrammsEmpty())
- return "basic";
- return learnedProgramms.GetHeadValue().ToString();
- }
- public bool IsStackOfLearnedProgrammsEmpty()
- {
- return learnedProgramms is E
- }
- }
- public class LinkedStackItem
- {
- public int ItemValue { get; set; }
- public LinkedStackItem PreviousItem { get; set; }
- }
- public class LinkedStack
- {
- LinkedStackItem head;
- public LinkedStack() { }
- public LinkedStack(LinkedStack linkedStack)
- {
- head = linkedStack.head;
- }
- public void Push(int commandNumber)
- {
- var newItem = new LinkedStackItem { ItemValue = commandNumber, PreviousItem = head };
- head = newItem;
- }
- public int Pop()
- {
- if (head == null) throw new InvalidOperationException();
- int commandNumber = head.ItemValue;
- head = head.PreviousItem;
- return commandNumber;
- }
- public int GetHeadValue()
- {
- return head.ItemValue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement