Advertisement
Didart

World Tour

Nov 26th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WorldTour {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String stops = scanner.nextLine();
  8.         String command = scanner.nextLine();
  9.  
  10.         while (!command.equals("Travel")) {
  11.             String[] commandParts = command.split(":");
  12.             String commandName = commandParts[0];
  13.             switch (commandName) {
  14.                 case "Add Stop":
  15.                     int index = Integer.parseInt(commandParts[1]);
  16.                     String stopName = commandParts[2];
  17.                     if (isValidIndex(index, stops)) {
  18.  
  19.                         String firstHalf = stops.substring(0, index);
  20.                         String secondHalf = stops.substring(index);
  21.                         stops = firstHalf + stopName + secondHalf;
  22.                     }
  23.  
  24.                     break;
  25.                 case "Remove Stop":
  26.                     int startIndex = Integer.parseInt(commandParts[1]);
  27.                     int endIndex = Integer.parseInt(commandParts[2]);
  28.                     if (isValidIndex(startIndex, stops) && isValidIndex(endIndex, stops)) {
  29.                         String firstPart = stops.substring(0, startIndex);
  30.                         String secondPart = stops.substring(endIndex + 1);
  31.                         stops = firstPart + secondPart;
  32.                     }
  33.                     break;
  34.                 case "Switch":
  35.                     String oldStop = commandParts[1];
  36.                     String newStop = commandParts[2];
  37.                     if (stops.contains(oldStop)) {
  38.                         stops = stops.replace(oldStop, newStop);
  39.                     }
  40.                     break;
  41.             }
  42.             System.out.println(stops);
  43.             command = scanner.nextLine();
  44.         }
  45.         System.out.printf("Ready for world tour! Planned stops: %s", stops);
  46.     }
  47.  
  48.     public static boolean isValidIndex(int index, String stops) {
  49.         return index >= 0 && index < stops.length();
  50.  
  51.     }
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement