File tree Expand file tree Collapse file tree 1 file changed +24
-24
lines changed
Expand file tree Collapse file tree 1 file changed +24
-24
lines changed Original file line number Diff line number Diff line change @@ -28,41 +28,41 @@ public class Star {
2828When the state changes, we add the change to the memento list.
2929
3030``` java
31- public void setState(StarState state) {
32- this . state = state;
33- this . memento. add(this . state);
34- }
31+ public void setState(StarState state) {
32+ this . state = state;
33+ this . memento. add(this . state);
34+ }
3535```
3636
3737Then, we just need to implement the methods to restore, undo and redo:
3838
3939``` java
40- public void restoreState(int index) {
41- if (this . memento. isEmpty()) {
42- return ;
43- }
44-
45- if (index < 0 || index > this . memento. size() - 1 ) {
46- throw new IllegalArgumentException (" Invalid index" );
47- }
48-
49- this . state = this . memento. get(index);
40+ public void restoreState(int index) {
41+ if (this . memento. isEmpty()) {
42+ return ;
43+ }
5044
45+ if (index < 0 || index > this . memento. size() - 1 ) {
46+ throw new IllegalArgumentException (" Invalid index" );
5147 }
5248
53- public void undo() {
54- if (this . memento. isEmpty()) {
55- return ;
56- }
49+ this . state = this . memento. get(index);
5750
58- int current = this . memento . indexOf( this . state);
51+ }
5952
60- if (current == 0 ) {
61- return ;
62- }
53+ public void undo() {
54+ if (this . memento. isEmpty()) {
55+ return ;
56+ }
6357
64- this . state = this . memento. get(current - 1 );
58+ int current = this . memento. indexOf( this . state );
6559
60+ if (current == 0 ) {
61+ return ;
6662 }
6763
68- ```
64+ this . state = this . memento. get(current - 1 );
65+
66+ }
67+
68+ ```
You can’t perform that action at this time.
0 commit comments