Advanced OOP and patterns · 15 / 15 Exercise Hard

Editor with undo (Command)

Objective

In the Command pattern each action is an object with a method to execute it and another to undo it. By storing the executed commands in a stack, undoing is popping the last one and reverting it.

Instructions

  • The text starts empty. The first line has the number n of commands: append word (adds at the end), delete k (deletes the last k characters), undo (reverts the last append or delete command, if any) and show (shows the text).
Input
5
append hola
append mundo
delete 5
show
undo
Output
hola

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise