Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class WorldTour {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String stops = scanner.nextLine();
- String command = scanner.nextLine();
- while (!command.equals("Travel")) {
- String[] commandParts = command.split(":");
- String commandName = commandParts[0];
- switch (commandName) {
- case "Add Stop":
- int index = Integer.parseInt(commandParts[1]);
- String stopName = commandParts[2];
- if (isValidIndex(index, stops)) {
- String firstHalf = stops.substring(0, index);
- String secondHalf = stops.substring(index);
- stops = firstHalf + stopName + secondHalf;
- }
- break;
- case "Remove Stop":
- int startIndex = Integer.parseInt(commandParts[1]);
- int endIndex = Integer.parseInt(commandParts[2]);
- if (isValidIndex(startIndex, stops) && isValidIndex(endIndex, stops)) {
- String firstPart = stops.substring(0, startIndex);
- String secondPart = stops.substring(endIndex + 1);
- stops = firstPart + secondPart;
- }
- break;
- case "Switch":
- String oldStop = commandParts[1];
- String newStop = commandParts[2];
- if (stops.contains(oldStop)) {
- stops = stops.replace(oldStop, newStop);
- }
- break;
- }
- System.out.println(stops);
- command = scanner.nextLine();
- }
- System.out.printf("Ready for world tour! Planned stops: %s", stops);
- }
- public static boolean isValidIndex(int index, String stops) {
- return index >= 0 && index < stops.length();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement