desislava_topuzakova

01. World Tour

Nov 26th, 2021
1,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class WorldTour_01 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String stops = scanner.nextLine();
  9.         String command = scanner.nextLine();
  10.         //"Add Stop:{index}:{string}".split(":") ->["Add Stop", "{index}", "{string}"]
  11.         //"Remove Stop:{start_index}:{end_index}".split(":") -> ["Remove Stop", "{start index}", "{end_index}"]
  12.         //"Switch:{old_string}:{new_string}".split(":") ->["Switch", "{old_string}", "{new_string}"]
  13.         while (!command.equals("Travel")) {
  14.             String[] commandParts = command.split(":");
  15.             String commandName = commandParts[0]; // "Add Stop", "Remove Stop", "Switch"
  16.             switch (commandName) {
  17.                 case "Add Stop":
  18.                     int index = Integer.parseInt(commandParts[1]);
  19.                     String stopName = commandParts[2];
  20.                     //проверка дали индекса ми е валиден -> дали индекса е вътре в текста
  21.                     if (isValidIndex(index, stops)) {
  22.                         //разделяме текста на две части: до индекса и след индекса
  23.                         //първа част + stopName + втора част
  24.  
  25.                         //"Hawai::Cyprys-Greece"
  26.                         String firstHalf = stops.substring(0, index); //"Hawai::"
  27.                         String secondHalf = stops.substring(index); //"Cyprys-Greece"
  28.                         stops = firstHalf + stopName + secondHalf;
  29.                     }
  30.                     break;
  31.                 case "Remove Stop":
  32.                     int startIndex = Integer.parseInt(commandParts[1]);
  33.                     int endIndex = Integer.parseInt(commandParts[2]);
  34.                     if (isValidIndex(startIndex, stops) && isValidIndex(endIndex, stops)) {
  35.                         //първа част: 0 дo startIndex
  36.                         //втората част: endIndex до края
  37.                         //"Hawai::RomeCyprys-Greece"
  38.                         String firstPart = stops.substring(0, startIndex);
  39.                         String secondPart = stops.substring(endIndex + 1);
  40.                         stops =  firstPart + secondPart;
  41.                     }
  42.                     break;
  43.                 case "Switch":
  44.                     String oldStop = commandParts[1];
  45.                     String newStop = commandParts[2];
  46.                     if (stops.contains(oldStop)) {
  47.                         stops = stops.replace(oldStop, newStop);
  48.                     }
  49.                     break;
  50.             }
  51.  
  52.             System.out.println(stops);
  53.             command = scanner.nextLine();
  54.         }
  55.  
  56.         System.out.printf("Ready for world tour! Planned stops: %s", stops);
  57.  
  58.     }
  59.  
  60.     public static boolean isValidIndex(int index, String stops) {
  61.         //true -> валиден
  62.         //false -> невалиден
  63.        return index >= 0 && index < stops.length();
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment