Advertisement
Guest User

[4/3/2012] Challenge #35 [difficult]

a guest
Apr 4th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace ListOfStrings
  7. {
  8.     class Program
  9.     {
  10.         private static readonly List<string> LMainList = new List<string>();
  11.         private static readonly Stack<UserAction> SActionsDone = new Stack<UserAction>();
  12.         private static readonly Stack<UserAction> SUndoneActions = new Stack<UserAction>();
  13.  
  14.         static void Main()
  15.         {
  16.             while (true)
  17.             {
  18.                 Console.Write("Enter command ('A'dd, 'E'dit, 'D'elete, 'U'ndo, 'R'edo, 'Q'uit): ");
  19.                 var command = Console.ReadLine();
  20.                 string input;
  21.                 string strIndex;
  22.  
  23.                 switch (command)
  24.                 {
  25.                     case "A":
  26.                         Console.Write("Enter text to add: ");
  27.                         input = Console.ReadLine();
  28.  
  29.                         AddToList(input);
  30.                         break;
  31.  
  32.                     case "E":
  33.                         Console.Write("Enter index to edit: ");
  34.                         strIndex = Console.ReadLine();
  35.                         int index;
  36.                         Console.Write("Enter text to edit: ");
  37.                         input = Console.ReadLine();
  38.  
  39.                         if (int.TryParse(strIndex, out index))
  40.                         {
  41.                             var usAction2 = new UserAction { ListValue = LMainList.ElementAt(index), Command = EditList, Index = index };
  42.                             SActionsDone.Push(usAction2);
  43.  
  44.                             EditList(input, index);
  45.                         }
  46.                         else
  47.                             Console.WriteLine("{0} is not an integer.", strIndex);
  48.                         break;
  49.  
  50.                     case "D":
  51.                         Console.Write("Enter index to delete: ");
  52.                         strIndex = Console.ReadLine();
  53.  
  54.                         if (int.TryParse(strIndex, out index))
  55.                         {
  56.                             var usAction3 = new UserAction { ListValue = LMainList.ElementAt(index), Index = index, Command = DeleteEntry };
  57.                             SActionsDone.Push(usAction3);
  58.                             DeleteEntry("", index);
  59.                         }
  60.                         else
  61.                             Console.WriteLine("{0} is not an integer.", strIndex);
  62.                         break;
  63.  
  64.                     case "U":
  65.                         UndoAction();
  66.                         break;
  67.  
  68.                     case "R":
  69.                         RedoAction();
  70.                         break;
  71.  
  72.                     case "Q":
  73.                         return;
  74.  
  75.                     default:
  76.                         Console.WriteLine("Not a valid command.");
  77.                         break;
  78.                 }
  79.    
  80.                 foreach (var entry in LMainList)
  81.                 {
  82.                     Console.WriteLine(entry);
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private static void RedoAction()
  88.         {
  89.             if (SUndoneActions.Count == 0)
  90.                 return;
  91.  
  92.             var action = SUndoneActions.Pop();
  93.  
  94.             action.Command(action.ListValue, action.Index);
  95.         }
  96.  
  97.         private static void UndoAction()
  98.         {
  99.             if (SActionsDone.Count == 0)
  100.                 return;
  101.  
  102.             var action = SActionsDone.Pop();
  103.  
  104.             if (action.Command == DeleteEntry)
  105.             {
  106.                 SUndoneActions.Push(action);
  107.                 AddToList(action.ListValue, action.Index);
  108.             }
  109.             else if (action.Command == AddToList)
  110.             {
  111.                 SUndoneActions.Push(action);
  112.                 DeleteEntry(action.ListValue, action.Index);
  113.             }
  114.             else if (action.Command == EditList)
  115.             {
  116.                 var temp = action.ListValue;
  117.                 action.ListValue = LMainList[action.Index];
  118.                 EditList(temp, action.Index);
  119.                 SUndoneActions.Push(action);
  120.             }
  121.         }
  122.  
  123.         private static void DeleteEntry(string input, int index = 0)
  124.         {
  125.             if (LMainList.Count == 0 || index >= LMainList.Count || index <  0)
  126.                 return;
  127.  
  128.             LMainList.RemoveAt(index);
  129.         }
  130.  
  131.         private static void EditList(string input, int index = 0)
  132.         {
  133.             if (LMainList.Count == 0 || index >= LMainList.Count || index < 0)
  134.                 return;
  135.  
  136.             LMainList[index] = input;
  137.         }
  138.  
  139.         private static void AddToList(string input, int index = 0)
  140.         {
  141.             if ( index == 0 )
  142.                 index = LMainList.Count;
  143.  
  144.             var usAction1 = new UserAction { ListValue = input, Command = AddToList, Index = index };
  145.             SActionsDone.Push(usAction1);
  146.  
  147.             LMainList.Insert(index, input);
  148.         }
  149.     }
  150.  
  151.     class UserAction
  152.     {
  153.         public string ListValue { get; set; }
  154.         public int Index { get; set; }
  155.         public Action<string,int> Command { get; set; }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement