Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ListOfStrings
- {
- class Program
- {
- private static readonly List<string> LMainList = new List<string>();
- private static readonly Stack<UserAction> SActionsDone = new Stack<UserAction>();
- private static readonly Stack<UserAction> SUndoneActions = new Stack<UserAction>();
- static void Main()
- {
- while (true)
- {
- Console.Write("Enter command ('A'dd, 'E'dit, 'D'elete, 'U'ndo, 'R'edo, 'Q'uit): ");
- var command = Console.ReadLine();
- string input;
- string strIndex;
- switch (command)
- {
- case "A":
- Console.Write("Enter text to add: ");
- input = Console.ReadLine();
- AddToList(input);
- break;
- case "E":
- Console.Write("Enter index to edit: ");
- strIndex = Console.ReadLine();
- int index;
- Console.Write("Enter text to edit: ");
- input = Console.ReadLine();
- if (int.TryParse(strIndex, out index))
- {
- var usAction2 = new UserAction { ListValue = LMainList.ElementAt(index), Command = EditList, Index = index };
- SActionsDone.Push(usAction2);
- EditList(input, index);
- }
- else
- Console.WriteLine("{0} is not an integer.", strIndex);
- break;
- case "D":
- Console.Write("Enter index to delete: ");
- strIndex = Console.ReadLine();
- if (int.TryParse(strIndex, out index))
- {
- var usAction3 = new UserAction { ListValue = LMainList.ElementAt(index), Index = index, Command = DeleteEntry };
- SActionsDone.Push(usAction3);
- DeleteEntry("", index);
- }
- else
- Console.WriteLine("{0} is not an integer.", strIndex);
- break;
- case "U":
- UndoAction();
- break;
- case "R":
- RedoAction();
- break;
- case "Q":
- return;
- default:
- Console.WriteLine("Not a valid command.");
- break;
- }
- foreach (var entry in LMainList)
- {
- Console.WriteLine(entry);
- }
- }
- }
- private static void RedoAction()
- {
- if (SUndoneActions.Count == 0)
- return;
- var action = SUndoneActions.Pop();
- action.Command(action.ListValue, action.Index);
- }
- private static void UndoAction()
- {
- if (SActionsDone.Count == 0)
- return;
- var action = SActionsDone.Pop();
- if (action.Command == DeleteEntry)
- {
- SUndoneActions.Push(action);
- AddToList(action.ListValue, action.Index);
- }
- else if (action.Command == AddToList)
- {
- SUndoneActions.Push(action);
- DeleteEntry(action.ListValue, action.Index);
- }
- else if (action.Command == EditList)
- {
- var temp = action.ListValue;
- action.ListValue = LMainList[action.Index];
- EditList(temp, action.Index);
- SUndoneActions.Push(action);
- }
- }
- private static void DeleteEntry(string input, int index = 0)
- {
- if (LMainList.Count == 0 || index >= LMainList.Count || index < 0)
- return;
- LMainList.RemoveAt(index);
- }
- private static void EditList(string input, int index = 0)
- {
- if (LMainList.Count == 0 || index >= LMainList.Count || index < 0)
- return;
- LMainList[index] = input;
- }
- private static void AddToList(string input, int index = 0)
- {
- if ( index == 0 )
- index = LMainList.Count;
- var usAction1 = new UserAction { ListValue = input, Command = AddToList, Index = index };
- SActionsDone.Push(usAction1);
- LMainList.Insert(index, input);
- }
- }
- class UserAction
- {
- public string ListValue { get; set; }
- public int Index { get; set; }
- public Action<string,int> Command { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement