Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7.  
  8. public class SoftUni_Course_Planning_P10 {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         String[] inputCoursesArray = reader.readLine().split("\\,+");
  12.         List<String> inputCoursesList = new ArrayList<>();
  13.         for (int i = 0; i < inputCoursesArray.length; i++) {
  14.             inputCoursesList.add(inputCoursesArray[i].trim());
  15.         }
  16.  
  17.         String commands = "";
  18.         while (!"course start".equals(commands = reader.readLine())) {
  19.             String[] keyCommands = commands.split("\\:+");
  20.  
  21.  
  22.             switch (keyCommands[0]) {
  23.                 case "Add":
  24.                     String courseToAdd = keyCommands[1];
  25.                     if (!inputCoursesList.contains(courseToAdd)) {
  26.                         inputCoursesList.add(courseToAdd);
  27.                     }
  28.                     break;
  29.                 case "Insert":
  30.                     String courseToInsert = keyCommands[1];
  31.                     int indexInsert = Integer.parseInt(keyCommands[2]);
  32.                     if (!inputCoursesList.contains(courseToInsert) && (indexInsert >= 0 && indexInsert < inputCoursesList.size())) {
  33.                         inputCoursesList.add(indexInsert, courseToInsert);
  34.                     }
  35.                     break;
  36.                 case "Remove":
  37.                     String courseToRemove = keyCommands[1];
  38.                     String courseExerciseToRemove = keyCommands[1] + "-Exercise";
  39.                     if (inputCoursesList.contains(courseToRemove) && inputCoursesList.contains(courseExerciseToRemove)) {
  40.                         inputCoursesList.remove(courseToRemove);
  41.                         inputCoursesList.remove(courseExerciseToRemove);
  42.                     }
  43.                     if (inputCoursesList.contains(courseExerciseToRemove)) {
  44.                         inputCoursesList.remove(courseExerciseToRemove);
  45.                     }
  46.                     if (inputCoursesList.contains(courseToRemove)) {
  47.                         inputCoursesList.remove(courseToRemove);
  48.                     }
  49.  
  50.                     break;
  51.                 case "Swap":
  52.                     String firstCourse = keyCommands[1];
  53.                     int firstIndex = 0;
  54.                     String secondCourse = keyCommands[2];
  55.                     int secondIndex = 0;
  56.  
  57.                     if ((inputCoursesList.contains(firstCourse) && inputCoursesList.contains(firstCourse + "-Exercise"))
  58.                             && inputCoursesList.contains(secondCourse) && inputCoursesList.contains(secondCourse + "-Exercise")) {
  59.  
  60.                         firstIndex = inputCoursesList.indexOf(firstCourse);
  61.                         secondIndex = inputCoursesList.indexOf(secondCourse);
  62.  
  63.                         Collections.swap(inputCoursesList, firstIndex, secondIndex);
  64.                         Collections.swap(inputCoursesList, firstIndex + 1, secondIndex + 1);
  65.                     }
  66.                     if ((inputCoursesList.contains(firstCourse) && inputCoursesList.contains(firstCourse + "-Exercise")
  67.                             && inputCoursesList.contains(secondCourse))) {
  68.  
  69.                         firstIndex = inputCoursesList.indexOf(firstCourse);
  70.                         secondIndex = inputCoursesList.indexOf(secondCourse);
  71.  
  72.                         inputCoursesList.remove(firstIndex + 1);
  73.                         Collections.swap(inputCoursesList, firstIndex, secondIndex);
  74.                         inputCoursesList.add(secondIndex + 1, firstCourse + "-Exercise");
  75.                     }
  76.  
  77.  
  78.                     if ((inputCoursesList.contains(secondCourse) && inputCoursesList.contains(secondCourse + "-Exercise"))
  79.                             && (inputCoursesList.contains(firstCourse) && inputCoursesList.contains(firstCourse + "-Exercise"))) {
  80.  
  81.                         secondIndex = inputCoursesList.indexOf(secondCourse);
  82.                         firstIndex = inputCoursesList.indexOf(firstCourse);
  83.  
  84.                         Collections.swap(inputCoursesList, firstIndex, secondIndex);
  85.                         Collections.swap(inputCoursesList, firstIndex + 1, secondIndex + 1);
  86.                     }
  87.                     if ((inputCoursesList.contains(secondCourse) && inputCoursesList.contains(secondCourse + "-Exercise"))
  88.                             && (inputCoursesList.contains(firstCourse))) {
  89.  
  90.                         secondIndex = inputCoursesList.indexOf(secondCourse);
  91.                         firstIndex = inputCoursesList.indexOf(firstCourse);
  92.  
  93.                         inputCoursesList.remove(secondIndex + 1);
  94.                         Collections.swap(inputCoursesList, firstIndex, secondIndex);
  95.                         inputCoursesList.add(firstIndex + 1, secondCourse + "-Exercise");
  96.                     }
  97.  
  98.  
  99.                     if ((inputCoursesList.contains(firstCourse) && inputCoursesList.contains(secondCourse))
  100.                             && (!inputCoursesList.contains(firstCourse + "-Exercise") && !inputCoursesList.contains(secondCourse + "-Exercise"))) {
  101.  
  102.                         firstIndex = inputCoursesList.indexOf(firstCourse);
  103.                         secondIndex = inputCoursesList.indexOf(secondCourse);
  104.  
  105.                         inputCoursesList.set(firstIndex, secondCourse);
  106.                         inputCoursesList.set(secondIndex, firstCourse);
  107.                     }
  108.                     break;
  109.                 case "Exercise":
  110.                     String onlyTheCourse = keyCommands[1];
  111.                     int mainCourseIndex = 0;
  112.                     String courseExercise = keyCommands[1] + "-Exercise";
  113.  
  114.                     if (inputCoursesList.contains(onlyTheCourse) && !inputCoursesList.contains(courseExercise)){
  115.                         inputCoursesList.add(inputCoursesList.indexOf(onlyTheCourse) + 1, courseExercise);
  116.                     }else if (!inputCoursesList.contains(courseExercise)){
  117.                         inputCoursesList.add(onlyTheCourse);
  118.                         inputCoursesList.add(courseExercise);
  119.                     }
  120.  
  121.                     break;
  122.                 default:
  123.  
  124.                     break;
  125.             }
  126.         }
  127.  
  128.         String output = "";
  129.         for (
  130.                 int i = 0; i < inputCoursesList.size(); i++) {
  131.             output += i + 1 + "." + inputCoursesList.get(i) + "\n";
  132.         }
  133.         System.out.println(output);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement