Files
oops/src/main/java/oving1/LineEditor.java
T
2026-01-30 01:44:23 +01:00

65 lines
1.1 KiB
Java

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;
}
}