Didart

Simple Text Editor

Dec 29th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package StacksAndQueues1;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class SimpleTextEditor {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         ArrayDeque<String> wordStates = new ArrayDeque<>();
  11.  
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.  
  14.         StringBuilder currentSymbol = new StringBuilder();
  15.  
  16.         for (int count = 1; count <= n; count++) {
  17.             String command = scanner.nextLine();
  18.            
  19.             String commandNumber = command.split("\\s+")[0];
  20.             switch (commandNumber) {
  21.                 case "1":
  22.                     String textToAdd = command.split("\\s+")[1];
  23.                     currentSymbol.append(textToAdd);
  24.                     wordStates.push(currentSymbol.toString());
  25.                     break;
  26.                 case "2":
  27.                     int countElements = Integer.parseInt(command.split("\\s+")[1]);
  28.                     int startIndex = currentSymbol.length() - countElements;
  29.                     currentSymbol.delete(startIndex, startIndex + countElements);
  30.                     wordStates.push(currentSymbol.toString());
  31.                     break;
  32.                 case "3":
  33.                     int index = Integer.parseInt(command.split("\\s+")[1]);
  34.                     System.out.println(currentSymbol.charAt(index - 1));
  35.                     break;
  36.                 case "4":
  37.                     if (wordStates.size() > 1) {
  38.                         wordStates.pop();
  39.                         currentSymbol = new StringBuilder(wordStates.peek());
  40.                     } else {
  41.                         currentSymbol = new StringBuilder();
  42.                     }
  43.                     break;
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment