Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Solution {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(", ");
- // Create a list to store the schedule
- List<String> schedule = new ArrayList<>();
- // Add each lesson to the list
- for (String lesson : input) {
- schedule.add(lesson);
- }
- String command = scanner.nextLine();
- while (!command.equals("course start")) {
- String[] tokens = command.split(":");
- String action = tokens[0];
- String lessonTitle = tokens[1];
- switch (action) {
- case "Add":
- if (!schedule.contains(lessonTitle)) {
- schedule.add(lessonTitle);
- }
- break;
- case "Insert":
- int index = Integer.parseInt(tokens[2]);
- if (!schedule.contains(lessonTitle)) {
- schedule.add(index, lessonTitle);
- }
- break;
- case "Remove":
- // Remove the lesson, if it exists
- if (schedule.contains(lessonTitle)) {
- schedule.remove(lessonTitle);
- // Remove the exercise, if it exists
- String exercise = lessonTitle + "-Exercise";
- if (schedule.contains(exercise)) {
- schedule.remove(exercise);
- }
- }
- break;
- case "Swap":
- // Change the place of the two lessons, if they exist
- String otherLessonTitle = tokens[2];
- if (schedule.contains(lessonTitle) && schedule.contains(otherLessonTitle)) {
- // Get the indices of the lessons
- int firstIndex = schedule.indexOf(lessonTitle);
- int secondIndex = schedule.indexOf(otherLessonTitle);
- // Swap the lessons
- schedule.set(firstIndex, otherLessonTitle);
- schedule.set(secondIndex, lessonTitle);
- // Swap the exercises, if they exist
- String firstExercise = lessonTitle + "-Exercise";
- String secondExercise = otherLessonTitle + "-Exercise";
- // Check if the first lesson has an exercise
- boolean hasFirstExercise = schedule.contains(firstExercise);
- // Check if the second lesson has an exercise
- boolean hasSecondExercise = schedule.contains(secondExercise);
- if (hasFirstExercise) {
- // Remove the first exercise
- schedule.remove(firstExercise);
- // Insert the first exercise after the second lesson
- schedule.add(secondIndex + 1, firstExercise);
- }
- if (hasSecondExercise) {
- // Remove the second exercise
- schedule.remove(secondExercise);
- // Insert the second exercise after the first lesson
- schedule.add(firstIndex + 1, secondExercise);
- }
- }
- break;
- case "Exercise":
- String exercise = lessonTitle + "-Exercise";
- if (schedule.contains(lessonTitle) && !schedule.contains(exercise)) {
- int lessonIndex = schedule.indexOf(lessonTitle);
- schedule.add(lessonIndex + 1, exercise);
- } else if (!schedule.contains(lessonTitle)) {
- // If the lesson doesn't exist, add the lesson at
- // the end of the course schedule, followed by the Exercise.
- schedule.add(lessonTitle);
- schedule.add(exercise);
- }
- break;
- }
- // Read the next command
- command = scanner.nextLine();
- }
- scanner.close();
- // Print the whole course schedule
- for (int i = 0; i < schedule.size(); i++) {
- System.out.println((i + 1) + "." + schedule.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement