Advertisement
LardaX

SimpleTextEditor

Jan 23rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Arrays;
  3. import java.util.Deque;
  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.         int operationsCount = Integer.parseInt(scanner.nextLine());
  10.  
  11.         Deque<Character> text = new ArrayDeque<>();
  12.         Deque<String[]> commandsExecuted = new ArrayDeque<>();
  13.         Deque<Character> erasedChars = new ArrayDeque<>();
  14.  
  15.         for (int i = 0; i < operationsCount; i++) {
  16.             String[] commandLine = scanner.nextLine().split(" ");
  17.             int commandType = Integer.parseInt(commandLine[0]);
  18.  
  19.             switch (commandType){
  20.                 case 1:
  21.                     char[] textToAppend = commandLine[1].toCharArray();
  22.                     for (int j = 0; j < textToAppend.length; j++) {
  23.                         text.push(textToAppend[j]);
  24.                     }
  25.                     commandsExecuted.push(commandLine);
  26.                     break;
  27.                 case 2:
  28.                     int eraseCount = Integer.parseInt(commandLine[1]);
  29.                     for (int j = 0; j < eraseCount; j++) {
  30.                         erasedChars.push(text.poll());
  31.                     }
  32.                     commandsExecuted.push(commandLine);
  33.                     break;
  34.                 case 3:
  35.                     int printPosition = Integer.parseInt(commandLine[1]);
  36.                     String[] currentText = text.toString().split("\\[|, |\\]", -1);
  37.  
  38.                     System.out.println(currentText[currentText.length - 1 - printPosition]);
  39.                     break;
  40.                 case 4:
  41.                     String[] previousCommand = commandsExecuted.poll();
  42.                     String previousCommandType = previousCommand[0];
  43.                     switch (previousCommandType){
  44.                         case "1":
  45.                             for (int j = 0; j < previousCommand[1].length(); j++) {
  46.                                 text.poll();
  47.                             }
  48.                             break;
  49.                         case "2":
  50.                             int returnCount = Integer.parseInt(previousCommand[1]);
  51.  
  52.                             for (int j = 0; j < returnCount; j++) {
  53.                                 text.push(erasedChars.poll());
  54.                             }
  55.                             break;
  56.                     }
  57.  
  58.             }
  59.  
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement