Advertisement
P_Donchev

SoftUni Problems - World Tour

Aug 9th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WorldTour {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String destinations = scan.nextLine();
  7.         String input = scan.nextLine();
  8.  
  9.         while (!"Travel".equals(input)) {
  10.             String[] tokens = input.split(":");
  11.             String command = tokens[0];
  12.  
  13.             switch (command) {
  14.                 case "Add Stop": {
  15.                     int index = Integer.parseInt(tokens[1]);
  16.                     String toInsert = tokens[2];
  17.                    
  18.                     if (validIndex(destinations, index)) {
  19.                         destinations = addStop(destinations, index, toInsert);
  20.                     }
  21.  
  22.                     System.out.println(destinations);
  23.  
  24.                     break;
  25.                 }
  26.                 case "Remove Stop": {
  27.                     int startIndex = Integer.parseInt(tokens[1]);
  28.                     int endIndex = Integer.parseInt(tokens[2]);
  29.  
  30.                     if (validIndex(destinations, startIndex) && validIndex(destinations, endIndex)) {
  31.                         destinations = removeStop(destinations, startIndex, endIndex);
  32.                     }
  33.  
  34.                     System.out.println(destinations);
  35.  
  36.                     break;
  37.                 }
  38.                 case "Switch": {
  39.                     String oldString = tokens[1];
  40.                     String newString = tokens[2];
  41.  
  42.                     destinations = destinations.replace(oldString, newString);
  43.                     System.out.println(destinations);
  44.  
  45.                     break;
  46.                 }
  47.             }
  48.  
  49.             input = scan.nextLine();
  50.         }
  51.  
  52.         System.out.printf("Ready for world tour! Planned stops: %s", destinations);
  53.     }
  54.  
  55.     private static String removeStop(String destinations, int startIndex, int endIndex) {
  56.         StringBuilder sb = new StringBuilder();
  57.  
  58.         for (int i = 0; i < destinations.length(); i++) {
  59.             if (i >= startIndex && i <= endIndex)
  60.                 continue;
  61.  
  62.             sb.append(destinations.charAt(i));
  63.         }
  64.  
  65.         return sb.toString();
  66.     }
  67.  
  68.     private static String addStop(String destinations, int index, String toInsert) {
  69.         StringBuilder sb = new StringBuilder();
  70.  
  71.         for (int i = 0; i < index; i++) {
  72.             sb.append(destinations.charAt(i));
  73.         }
  74.  
  75.         for (int i = 0; i < toInsert.length(); i++) {
  76.             sb.append(toInsert.charAt(i));
  77.         }
  78.  
  79.         for (int i = index; i < destinations.length(); i++) {
  80.             sb.append(destinations.charAt(i));
  81.         }
  82.  
  83.         return sb.toString();
  84.     }
  85.  
  86.     private static boolean validIndex(String destinations, int index) {
  87.         return index >= 0 && index < destinations.length();
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement