-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextEditor.cs
46 lines (38 loc) · 1.05 KB
/
TextEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace CommandDesignPattern;
public class TextEditor
{
public string currentText = "";
// To Store Undo Vars
string _pastText;
private List<ICommand> _usedCommands = new List<ICommand>();
public TextEditor(string currentText)
{
this.currentText = currentText;
}
public void Undo()
{
if (_usedCommands.Count >= 1)
{
currentText = _usedCommands[_usedCommands.Count - 1].TextToUndo(currentText);
_usedCommands.RemoveAt(_usedCommands.Count - 1);
}
else
{
Console.WriteLine("No more actions exist!! \n");
}
}
public void DisplayCurrentText()
{
Console.WriteLine($"\nCurrent text is >> {currentText}\n");
}
public void ExecuteICommand(ICommand textAction)
{
_pastText = currentText;
currentText = textAction.TextToExecute(currentText);
_usedCommands.Add(textAction);
}
public void DisplayProcessed()
{
Console.WriteLine($"\nProcessed Text >>{currentText}\n");
}
}