RGAlex

O1. World Tour / FinalExam - O2

Mar 16th, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package FinalExam.O2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O1WorldTour {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         StringBuilder input = new StringBuilder(scanner.nextLine());
  9.         String command = scanner.nextLine();
  10.  
  11.         while (!command.equals("Travel")) {
  12.             String[] type = command.split(":");
  13.             switch (type[0]) {
  14.                 case "Add Stop":
  15.                     if (Integer.parseInt(type[1]) >= 0 && Integer.parseInt(type[1]) < input.length()) {
  16.                         input.insert(Integer.parseInt(type[1]), type[2]);
  17.                     }
  18.                     System.out.println(input);
  19.                     break;
  20.                 case "Remove Stop":
  21.                     int startIndex = Integer.parseInt(type[1]);
  22.                     int endIndex = Integer.parseInt(type[2]);
  23.  
  24.                     if (startIndex >= 0 && endIndex >= 0 && startIndex < input.length() && endIndex < input.length()) {
  25.                         input.delete(startIndex, endIndex + 1);
  26.                     }
  27.                     System.out.println(input);
  28.                     break;
  29.                 case "Switch":
  30.                     input = new StringBuilder(input.toString().replace(type[1], type[2]));
  31.                     System.out.println(input);
  32.                     break;
  33.             }
  34.             command = scanner.nextLine();
  35.         }
  36.         System.out.println("Ready for world tour! Planned stops: " + input);
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment