Created
September 27, 2022 17:37
-
-
Save lubaochuan/a4934e4518b8997a5339aea66e48a98d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.LinkedList; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.PrintWriter; | |
public class Editor{ | |
LinkedList<String> list; | |
int currentIndex = -1; | |
boolean displayError = false; | |
String lastErrorMessage = null; | |
String fileName = null; | |
public static void main(String[] args){ | |
// test append | |
Editor ed = new Editor(); | |
ed.appendLines(new String[]{"hello", "world"}); | |
System.out.println("expect: world"); | |
System.out.println(" got: "+ed.getCurrentLine()); | |
System.out.println("expect: hello"); | |
System.out.println(" got: "+ed.getLine(1)); | |
System.out.println("expect: 1 hello"); | |
System.out.println(" got: "+ed.getCurrentLineWithNumber()); | |
ed.appendLines(new String[]{"hello", "world"}); | |
System.out.println("expect: 4"); | |
System.out.println(" got: "+ed.getLineCount()); | |
// test insert in the middle | |
ed = new Editor(); | |
ed.appendLines(new String[]{"hello", "world"}); | |
ed.insertLines(new String[]{"hello1", "world1"}); | |
System.out.println("expect: world1"); | |
System.out.println(" got: "+ed.getCurrentLine()); | |
System.out.println("expect: hello"); | |
System.out.println(" got: "+ed.getLine(1)); | |
System.out.println("expect: hello1"); | |
System.out.println(" got: "+ed.getLine(2)); | |
System.out.println("expect: world1"); | |
System.out.println(" got: "+ed.getLine(3)); | |
System.out.println("expect: world"); | |
System.out.println(" got: "+ed.getLine(4)); | |
// test insert at the beginning | |
ed = new Editor(); | |
ed.appendLines(new String[]{"hello", "world"}); | |
ed.getLine(1); | |
ed.insertLines(new String[]{"hello1", "world1"}); | |
System.out.println("expect: world1"); | |
System.out.println(" got: "+ed.getCurrentLine()); | |
System.out.println("expect: hello1"); | |
System.out.println(" got: "+ed.getLine(1)); | |
System.out.println("expect: world1"); | |
System.out.println(" got: "+ed.getLine(2)); | |
System.out.println("expect: hello"); | |
System.out.println(" got: "+ed.getLine(3)); | |
System.out.println("expect: world"); | |
System.out.println(" got: "+ed.getLine(4)); | |
// test delete in the middle | |
ed = new Editor(); | |
ed.appendLines(new String[]{"hello", "world", "again"}); | |
ed.deleteLine(2); | |
System.out.println("expect: again"); | |
System.out.println(" got: "+ed.getCurrentLine()); | |
// test delete last | |
ed.deleteLine(2); | |
System.out.println("expect: hello"); | |
System.out.println(" got: "+ed.getCurrentLine()); | |
// test delete invalid line number | |
ed = new Editor(); | |
ed.appendLines(new String[]{"hello", "world", "again"}); | |
System.out.println("expect: EditorException"); | |
try{ | |
ed.deleteLine(4); | |
}catch (EditorException e){ | |
System.out.println(" got: EditorException"); | |
} | |
} | |
public Editor(){ | |
list = new LinkedList<String>(); | |
} | |
public Editor(String fileName){ | |
list = new LinkedList<String>(); | |
try{ | |
Scanner scan = new Scanner(new File(fileName)); | |
this.fileName = fileName; | |
while(scan.hasNext()){ | |
list.addLast(scan.nextLine()); | |
currentIndex++; | |
} | |
}catch(FileNotFoundException e){ | |
throw new EditorException(fileName+": No such file or directory"); | |
} | |
} | |
public String getCurrentLineWithNumber(){ | |
String result = getCurrentLine(); | |
int lineNumber = currentIndex+1; | |
return lineNumber+" "+result; | |
} | |
public String getCurrentLine(){ | |
String result = null; | |
if(0 <= currentIndex && currentIndex < list.size()){ | |
result = list.get(currentIndex); | |
}else{ | |
lastErrorMessage = "invalid address"; | |
throw new EditorException(lastErrorMessage); | |
} | |
return result; | |
} | |
public String getLine(int lineNumber){ | |
String result; | |
int index = lineNumber-1; | |
if(isValidIndex(index)){ | |
currentIndex = index; | |
result = getCurrentLine(); | |
}else{ | |
lastErrorMessage = "invalid address"; | |
throw new EditorException(lastErrorMessage); | |
} | |
return result; | |
} | |
private boolean isValidIndex(int index){ | |
return 0 <= index && index < list.size(); | |
} | |
public void appendLines(String[] lines){ | |
if(lines.length == 0){ | |
return; | |
} | |
for(String line : lines){ | |
currentIndex++; | |
list.add(currentIndex, line); | |
} | |
} | |
public void insertLines(String[] lines){ | |
if(lines.length == 0){ | |
return; | |
} | |
if(currentIndex == -1){ | |
appendLines(lines); | |
}else{ | |
currentIndex--; | |
appendLines(lines); | |
} | |
} | |
public void deleteCurrentLine(){ | |
list.remove(currentIndex); | |
if(currentIndex > list.size()-1){ | |
currentIndex--; | |
} | |
} | |
public void deleteLine(int num){ | |
int index = num-1; | |
if(isValidIndex(index)){ | |
currentIndex = index; | |
deleteCurrentLine(); | |
}else{ | |
lastErrorMessage = "invalid address"; | |
throw new EditorException(lastErrorMessage); | |
} | |
} | |
public void writeOut(){ | |
if(fileName == null){ | |
lastErrorMessage = "no current filename"; | |
throw new EditorException(lastErrorMessage); | |
}else{ | |
writeOutToFile(fileName); | |
} | |
} | |
public void writeOutToFile(String fileName){ | |
try{ | |
PrintWriter writer = new PrintWriter(new File(fileName)); | |
Iterator<String> it = list.iterator(); | |
while(it.hasNext()){ | |
writer.println(it.next()); | |
} | |
}catch (FileNotFoundException e){ | |
} | |
} | |
public int getLineCount(){ | |
return list.size(); | |
} | |
public int charCount(){ | |
int result = 0; | |
Iterator<String> it = list.iterator(); | |
while(it.hasNext()){ | |
result += it.next().length(); | |
} | |
return result; | |
} | |
public int wordCount(){ | |
int result = 0; | |
Iterator<String> it = list.iterator(); | |
while(it.hasNext()){ | |
String line = it.next(); | |
String[] words = line.split("\\s+"); | |
result += words.length; | |
} | |
return result; | |
} | |
public String toString(){ | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment