plamenavylcheva

Untitled

Jun 12th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class SoftuniCoursePlanning {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<String> list = Arrays.stream(scanner.nextLine().split(", "))
  12. .collect(Collectors.toList());
  13. String command = scanner.nextLine();
  14. while (!command.equals("course start")) {
  15. if (command.contains("Add")) {
  16. String lessonName = command.split(":")[1];
  17.  
  18. if (!list.contains(lessonName)) {
  19. list.add(lessonName);
  20. }
  21.  
  22. } else if (command.contains("Insert")) {
  23. String insertLesson = command.split(":")[1];
  24. int insertIndex = Integer.parseInt(command.split(":")[2]);
  25. if (!list.contains(insertLesson)) {
  26. list.add(insertIndex, insertLesson);
  27. }
  28.  
  29. } else if (command.contains("Remove")) {
  30. String removeLesson = command.split(":")[1];
  31. if (list.contains(removeLesson)) {
  32. list.remove(removeLesson);
  33. }
  34. } else if (command.contains("Swap")) {
  35. String firstLesson = command.split(":")[1];
  36. String secondLesson = command.split(":")[2];
  37.  
  38. if (list.contains(firstLesson) && list.contains(secondLesson)) {
  39. String temp = firstLesson;
  40. int indexOfFirst = list.indexOf(firstLesson);
  41. int indexOfSecond = list.indexOf(secondLesson);
  42. list.set(indexOfFirst, secondLesson);
  43. list.set(indexOfSecond, temp);
  44.  
  45. String exerciseFirst = firstLesson + "-" + "Exercise";
  46. if (list.contains(exerciseFirst)) {
  47. list.remove(exerciseFirst);
  48. int newIndex = list.indexOf(firstLesson);
  49. if (newIndex < list.size() - 1) {
  50. list.add(newIndex + 1, exerciseFirst);
  51. } else {
  52. list.add(exerciseFirst);
  53. }
  54. }
  55. String exerciseSecond = secondLesson + "-" + "Exercise";
  56. if (list.contains(exerciseSecond)) {
  57. list.remove(exerciseSecond);
  58. int newIndex = list.indexOf(secondLesson);
  59. if (newIndex < list.size() - 1) {
  60. list.add(newIndex + 1, exerciseSecond);
  61. } else {
  62. list.add(exerciseSecond);
  63. }
  64. }
  65. }
  66. } else if (command.contains("Exercise")) {
  67. String lessonTitle = command.split(":")[1];
  68. String exerciseLesson = lessonTitle + "-" + "Exercise";
  69. if (list.contains(lessonTitle)) {
  70. int indexLesson = list.indexOf(lessonTitle);
  71. if (indexLesson < list.size() - 1) {
  72. list.add(indexLesson + 1, exerciseLesson);
  73. } else {
  74. list.add(exerciseLesson);
  75. }
  76. } else {
  77. list.add(lessonTitle);
  78. list.add(exerciseLesson);
  79. }
  80. }
  81. command = scanner.nextLine();
  82. }
  83. for (int i = 1; i <= list.size(); i++) {
  84. System.out.printf("%d.%s%n", i, list.get(i - 1));
  85. }
  86. }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment