Respuesta :
Explanation:
import java.util.ArrayList;
import java.util.Scanner;
class TextProcessor {
private StringBuilder text;
private ArrayList<String> textHistory;
public TextProcessor() {
text = new StringBuilder();
textHistory = new ArrayList<>();
}
public void appendText(String newText) {
text.append(newText);
}
public void removeCharacters(String charsToRemove) {
text = new StringBuilder(text.toString().replaceAll(charsToRemove, ""));
}
public void replaceCharacters(String oldChars, String newChars) {
text = new StringBuilder(text.toString().replace(oldChars, newChars));
}
public void reverseText() {
text.reverse();
}
public void convertToUppercase() {
text = new StringBuilder(text.toString().toUpperCase());
}
public void convertToLowercase() {
text = new StringBuilder(text.toString().toLowerCase());
}
public void displayText() {
System.out.println("Current Text: " + text);
}
public void saveToHistory() {
textHistory.add(text.toString());
}
public void displayHistory() {
System.out.println("Text History:");
for (String entry : textHistory) {
System.out.println(entry);
}
}
}
public class Main {
public static void main(String[] args) {
TextProcessor processor = new TextProcessor();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\nChoose an option:");
System.out.println("1. Append text");
System.out.println("2. Remove characters");
System.out.println("3. Replace characters");
System.out.println("4. Reverse text");
System.out.println("5. Convert to uppercase");
System.out.println("6. Convert to lowercase");
System.out.println("7. Display current text");
System.out.println("8. Save text to history");
System.out.println("9. Display text history");
System.out.println("10. Exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.println("Enter text to append:");
String appendText = scanner.nextLine();
processor.appendText(appendText);
break;
case 2:
System.out.println("Enter characters to remove:");
String charsToRemove = scanner.nextLine();
processor.removeCharacters(charsToRemove);
break;
case 3:
System.out.println("Enter characters to replace:");
String oldChars = scanner.nextLine();
System.out.println("Enter new characters:");
String newChars = scanner.nextLine();
processor.replaceCharacters(oldChars, newChars);
break;
case 4:
processor.reverseText();
break;
case 5:
processor.convertToUppercase();
break;
case 6:
processor.convertToLowercase();
break;
case 7:
processor.displayText();
break;
case 8:
processor.saveToHistory();
System.out.println("Text saved to history.");
break;
case 9:
processor.displayHistory();
break;
case 10:
running = false;
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please enter a number between 1 and 10.");
break;
}
}
scanner.close();
}
}