Your task is to complete the implementation of the Undo class so that thetakeSnapshot and rollback methods work correctly. Keep in mind that the rollback method may be called any time, so make sure that the program does not crash when the rollback method is called.
Memento
Roll back
Report a typo
Write a program in Java 17
class Editor {
private String text = "";
private int selectionStart = 0;
private int selectionEnd = 0;
public void setText(String text) {
select(0, 0);
this.text = text;
}
public void select(int from, int to) {
selectionStart = from;
selectionEnd = to;
}
public String getSelectedText() {
return text.substring(selectionStart, selectionEnd);
}
public EditorState getState() {
return new EditorState(text, selectionStart, selectionEnd);
}
public void setState(EditorState state) {
this.text = state.text;
this.selectionStart = state.selectionStart;
this.selectionEnd = state.selectionEnd;
}
static class EditorState {
private final String text;
private final int selectionStart;
private final int selectionEnd;
private EditorState(String text, int selectionStart, int selectionEnd) {
this.text = text;
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.