This repository was archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotepad.java
More file actions
53 lines (44 loc) · 1.46 KB
/
Notepad.java
File metadata and controls
53 lines (44 loc) · 1.46 KB
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
47
48
49
50
51
52
53
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class Notepad {
private List<State> states;
private int currentState;
private Logger logger;
public Notepad(String loggerFilename){
states = new LinkedList<>();
states.add(new State(""));
currentState = 0;
logger = new Logger(loggerFilename);
}
public void save(String text) throws IOException{
int pos = currentState + 1;
while(pos < states.size()){
this.states.remove(pos);
}
states.add(currentState + 1, new State(text));
currentState++;
logger.log(states.get(currentState - 1).getDifferences(states.get(currentState)));
}
public void undo() throws IOException{
if(currentState > 0) {
currentState--;
logger.log(states.get(currentState + 1).getDifferences(states.get(currentState)));
}
}
public void redo() throws IOException{
if(currentState < states.size() - 1) {
currentState++;
logger.log(states.get(currentState - 1).getDifferences(states.get(currentState)));
}
}
@Override
public String toString() {
return "Current state: " + currentState + "\n"
+ states.get(currentState);
}
public State getCurrentState(){
return states.get(currentState);
}
}