Advertisement
nkostadinski

WorldTour_01

Mar 30th, 2021
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WorldTour_01 {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         String stops = scan.nextLine();
  8.  
  9.         String command = scan.nextLine();
  10.         while (!command.equals("Travel")) {
  11.             String[] tokens = command.split(":");
  12.             String cmd = tokens[0];
  13.             switch (cmd) {
  14.                 case "Add Stop":
  15.                     int indexToInsert = Integer.parseInt(tokens[1]);
  16.                     String stringToInsert = tokens[2];
  17.                     if (indexToInsert <= stops.length()) {
  18.                         StringBuilder newString = new StringBuilder(stops);
  19.                         newString.insert(indexToInsert, stringToInsert);
  20.                         stops = newString.toString();
  21.                     }
  22.                     break;
  23.                 case "Remove Stop":
  24.                     int startIndex = Integer.parseInt(tokens[1]);
  25.                     int endIndex = Integer.parseInt(tokens[2]);
  26.                     if ( startIndex >= 0 && startIndex < endIndex && endIndex < stops.length()) {
  27.                         String stringToRemove = stops.substring(startIndex, endIndex + 1);
  28.                         stops = stops.replaceFirst(stringToRemove, "");
  29.                     }
  30.                     break;
  31.                 case "Switch":
  32.                     String oldString = tokens[1];
  33.                     String newString = tokens[2];
  34.                     if (stops.contains(oldString)) {
  35.                         stops = stops.replaceAll(oldString, newString);
  36.                     }
  37.                     break;
  38.             }
  39.             System.out.println(stops);
  40.  
  41.             command = scan.nextLine();
  42.         }
  43.         System.out.printf("Ready for world tour! Planned stops: %s", stops);
  44.  
  45.     }
  46.  
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement