Day 3 - Java

Problem Statement: Stack Implementation of Text Editor

You are given a simple text editor implemented in Java. The text editor has basic functionalities like inserting text, undoing the last operation, redoing the undone operation, and displaying the current text. Your task is to write a program that takes input commands and operates accordingly.

Class Description:

You are provided with a Java class TextEditor having the following methods:

insertText(String newText): Inserts the given text into the editor.

undo(): Undoes the last operation performed on the text.

redo(): Redoes the last undone operation.

displayText(): Displays the current text in the editor.

You need to implement the following methods:

public static void main(String[] args): This method is the entry point of your program. It should take input commands and perform the corresponding operations on the text editor.

Input Format:

The input consists of several lines, where each line contains an integer followed by additional data if necessary.

For choice 1: An integer 1 followed by a string newText indicating the text to be inserted.

For choice 2: An integer 2 indicating to undo the last operation.

For choice 3: An integer 3 indicating to redo the last undone operation.

For choice 4: An integer 4 indicating to display the current text.

Output Format:

For each choice 4, output the current text after performing the necessary operations.

Test Case 1:

Sample Input:

1

abc

4

3

2

Sample Output:

abc

Cannot redo.

abc

[empty]

Explanation:

Insert "abc" into the editor: The text in the editor becomes "abc".

Display the current text: Prints "abc".

Redo the last undone operation: There are no undone operations, so it prints "Cannot redo.".

Undo the last operation: Since the last operation was an insertion, it's undone. The text reverts back to an empty string.


Test Case 2:

Sample Input:

4

3

2

1

Happy

1

Coding

4

2

1

Learning

4

Sample Output:

[empty]

[empty]

Cannot redo.

[empty]

Cannot undo

HappyCoding

Happy

HappyLearning

Explanation:

Display the current text: There's no text in the editor, so it prints "".

Redo the last undone operation: There are no undone operations, so it prints "" and "Cannot redo.".

Undo the last operation: There are no operations to undo, so it prints "" and "Cannot undo."

Insert "Happy" into the editor: The text in the editor becomes "Happy".

Insert "Coding" into the editor: The text in the editor becomes "HappyCoding".

Display the current text: Prints "HappyCoding".

Undo the last operation: The last operation was insertion of "Coding", so it's undone. The text reverts back to "Happy".

Insert "Learning" into the editor: The text in the editor becomes "HappyLearning".

Display the current text: Prints "HappyLearning".

Constraints:

The length of the inserted text in a single command does not exceed 100 characters.

The number of commands does not exceed 100.


SOLUTION:

import java.util.Scanner;

import java.util.Stack;


public class TextEditor {

    private StringBuilder text;

    private Stack<String> undoStack;

    private Stack<String> redoStack;


    public TextEditor() {

        text = new StringBuilder();

        undoStack = new Stack<>();

        redoStack = new Stack<>();

    }


    public void insertText(String newText) {

        undoStack.push(text.toString());

        text.append(newText);

        redoStack.clear(); 

    }


    public void undo() {

        if (!undoStack.isEmpty()) {

            redoStack.push(text.toString());

            text = new StringBuilder(undoStack.pop());

        } else {

            System.out.println("Cannot undo.");

        }

    }


    public void redo() {

        if (!redoStack.isEmpty()) {

            undoStack.push(text.toString());

            text = new StringBuilder(redoStack.pop());

        } else {

            System.out.println("Cannot redo.");

        }

    }


    public void displayText() {

        System.out.println(text.toString());

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        TextEditor editor = new TextEditor();

        while (true) {

            int choice = scanner.nextInt();

            scanner.nextLine(); 

            if (choice == 1) {

                String newText = scanner.nextLine();

                editor.insertText(newText);

            } else if (choice == 2) {

                editor.undo();

                editor.displayText();

            } else if (choice == 3) {

                editor.redo();

                editor.displayText();

            } else if (choice == 4) {

                editor.displayText();

            }

        }

    }

}


Insights

  • This program uses two stacks, undoStack and redoStack, to store the history of text states. Undo operations pop from undoStack and push to redoStack, while redo operations do the reverse.
  • This program follows the command pattern where each user action (insert, undo, redo) is encapsulated as an object.
  • Here, the program utilizes StringBuilder for efficient manipulation of text. StringBuilder provides mutable string operations, making it suitable for frequent modifications. Explore String vs StringBuilder.
  • We have cleared the redo stack whenever a new text insertion is made after undoing changes. This prevents users from redoing operations that are no longer relevant.
  • Please provide feedback to users when attempting to perform undo or redo operations on empty stacks. This prevents runtime errors and ensures a smoother user experience.
  • The main method contains an infinite loop, ensuring continuous interaction with the user until the program is terminated. This enables users to perform multiple text editing operations in a single session. Try using a choice variable and stop the process. As it is not asked in the test case, not incorporated here, but make sure that the loop is not infinite.
  • This executes text editing operations sequentially based on user input. This ensures that each operation is performed in the order specified by the user, maintaining the integrity of the text editing process.
Keep Exploring more and more! Try finding complications even in simple concept and make your learning process for efficient and interesting! :)

Comments