Advertisement
nbzhk

10. SoftUni Course Planning

Apr 6th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package Lists.Exercise;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class P10SoftUniCoursePlanning {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> schedule = Arrays
  11.                 .stream(scanner.nextLine().split(", "))
  12.                 .collect(Collectors.toList());
  13.  
  14.         String command = scanner.nextLine();
  15.  
  16.         while (!command.equals("course start")) {
  17.             String[] commandElements = command.split(":");
  18.  
  19.             if (command.contains("Add")) {
  20.                 String lessonTitle = commandElements[1];
  21.                 if (!alreadyExists(schedule, lessonTitle)) {
  22.                     schedule.add(lessonTitle);
  23.                 }
  24.  
  25.             } else if (command.contains("Insert")) {
  26.                 String lessonTitle = commandElements[1];
  27.                 int index = Integer.parseInt(commandElements[2]);
  28.                 if (!alreadyExists(schedule, lessonTitle)) {
  29.                     schedule.add(index, lessonTitle);
  30.                 }
  31.             } else if (command.contains("Remove")) {
  32.                 String lessonTitle = commandElements[1];
  33.                 if (alreadyExists(schedule, lessonTitle)) {
  34.                     schedule.remove(lessonTitle);
  35.                 }
  36.             } else if (command.contains("Swap")) {
  37.                 String firstLessonTitle = commandElements[1];
  38.                 String secondLessonTitle = commandElements[2];
  39.  
  40.                 if (alreadyExists(schedule, firstLessonTitle) && alreadyExists(schedule, secondLessonTitle)) {
  41.                     int firstIndex = findIndex(schedule, firstLessonTitle);
  42.                     int secondIndex = findIndex(schedule, secondLessonTitle);
  43.  
  44.                     Collections.swap(schedule, firstIndex, secondIndex);
  45.  
  46.                     if (exerciseExist(schedule, schedule.get(secondIndex))) {
  47.                         int exerciseIndex = findIndex(schedule, schedule.get(secondIndex) + "-Exercise") + 1;
  48.                         schedule.add(secondIndex + 1, schedule.get(secondIndex) + "-Exercise");
  49.                         schedule.remove(exerciseIndex);
  50.                     }
  51.                     if (exerciseExist(schedule, schedule.get(firstIndex))) {
  52.                         int oldExerciseIndex = findIndex(schedule, schedule.get(firstIndex) + "-Exercise") + 1;
  53.                         schedule.add(firstIndex + 1, schedule.get(firstIndex) + "-Exercise");
  54.                         schedule.remove(oldExerciseIndex);
  55.                     }
  56.                 }
  57.             } else if (command.contains("Exercise")) {
  58.                     String lessonTitle = commandElements[1];
  59.                     if (alreadyExists(schedule,lessonTitle) && !exerciseExist(schedule,lessonTitle)) {
  60.                         int indexToAdd = findIndex(schedule,lessonTitle) + 1;
  61.                         schedule.add(indexToAdd, lessonTitle + "-Exercise");
  62.                     } else if (!alreadyExists(schedule,lessonTitle)) {
  63.                         schedule.add(lessonTitle);
  64.                         schedule.add(lessonTitle + "-Exercise");
  65.                     }
  66.             }
  67.  
  68.             command = scanner.nextLine();
  69.         }
  70.  
  71.         for (int i = 0; i < schedule.size(); i++) {
  72.             System.out.printf(i + 1 + ".%s%n", schedule.get(i));
  73.         }
  74.     }
  75.  
  76.     private static boolean alreadyExists(List<String> schedule, String lessonTitle) {
  77.         for (int i = 0; i < schedule.size(); i++) {
  78.             if (schedule.get(i).equals(lessonTitle)) {
  79.                 return true;
  80.             }
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     private static int findIndex(List<String> schedule, String lesson) {
  86.         int index = 0;
  87.         for (int i = 0; i < schedule.size(); i++) {
  88.             if (lesson.equals(schedule.get(i))) {
  89.                 index = schedule.indexOf(schedule.get(i));
  90.                 break;
  91.             }
  92.         }
  93.         return index;
  94.     }
  95.     private static boolean exerciseExist(List<String> schedule, String lesson) {
  96.         String lessonExercise = lesson + "-Exercise";
  97.         for (int i = 0; i < schedule.size(); i++) {
  98.             if (lessonExercise.equals(schedule.get(i))) {
  99.                 return true;
  100.             }
  101.         }
  102.         return false;
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement