initial solutions
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package oving1;
|
||||
|
||||
public class LineEditor {
|
||||
String text = "";
|
||||
int insertionIndex = 0;
|
||||
|
||||
LineEditor() {
|
||||
}
|
||||
|
||||
void left() {
|
||||
insertionIndex -= 1;
|
||||
}
|
||||
|
||||
void right() {
|
||||
insertionIndex += 1;
|
||||
}
|
||||
|
||||
void insertString(String s) {
|
||||
String a = text.substring(0, insertionIndex - 1);
|
||||
String b = text.substring(insertionIndex, text.length());
|
||||
text = a + s + b;
|
||||
}
|
||||
|
||||
void deleteLeft() {
|
||||
if (insertionIndex < 1) {
|
||||
return;
|
||||
}
|
||||
String a = text.substring(0, insertionIndex - 2);
|
||||
String b = text.substring(insertionIndex, text.length());
|
||||
text = a + b;
|
||||
}
|
||||
|
||||
void deleteRight() {
|
||||
if (insertionIndex >= text.length() - 1) {
|
||||
return;
|
||||
}
|
||||
String a = text.substring(0, insertionIndex - 1);
|
||||
String b = text.substring(insertionIndex + 1, text.length());
|
||||
text = a + b;
|
||||
}
|
||||
|
||||
String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
void setText(String s) {
|
||||
text = s;
|
||||
}
|
||||
|
||||
int getInsertionIndex() {
|
||||
return insertionIndex;
|
||||
}
|
||||
|
||||
void setInsertionIndex(int i) {
|
||||
insertionIndex = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String a = text.substring(0, insertionIndex - 1);
|
||||
String b = text.substring(insertionIndex, text.length());
|
||||
return a + "|" + b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user