Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exercise;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class SoftuniCoursePlanning {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> list = Arrays.stream(scanner.nextLine().split(", "))
- .collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("course start")) {
- if (command.contains("Add")) {
- String lessonName = command.split(":")[1];
- if (!list.contains(lessonName)) {
- list.add(lessonName);
- }
- } else if (command.contains("Insert")) {
- String insertLesson = command.split(":")[1];
- int insertIndex = Integer.parseInt(command.split(":")[2]);
- if (!list.contains(insertLesson)) {
- list.add(insertIndex, insertLesson);
- }
- } else if (command.contains("Remove")) {
- String removeLesson = command.split(":")[1];
- if (list.contains(removeLesson)) {
- list.remove(removeLesson);
- }
- } else if (command.contains("Swap")) {
- String firstLesson = command.split(":")[1];
- String secondLesson = command.split(":")[2];
- if (list.contains(firstLesson) && list.contains(secondLesson)) {
- String temp = firstLesson;
- int indexOfFirst = list.indexOf(firstLesson);
- int indexOfSecond = list.indexOf(secondLesson);
- list.set(indexOfFirst, secondLesson);
- list.set(indexOfSecond, temp);
- String exerciseFirst = firstLesson + "-" + "Exercise";
- if (list.contains(exerciseFirst)) {
- list.remove(exerciseFirst);
- int newIndex = list.indexOf(firstLesson);
- if (newIndex < list.size() - 1) {
- list.add(newIndex + 1, exerciseFirst);
- } else {
- list.add(exerciseFirst);
- }
- }
- String exerciseSecond = secondLesson + "-" + "Exercise";
- if (list.contains(exerciseSecond)) {
- list.remove(exerciseSecond);
- int newIndex = list.indexOf(secondLesson);
- if (newIndex < list.size() - 1) {
- list.add(newIndex + 1, exerciseSecond);
- } else {
- list.add(exerciseSecond);
- }
- }
- }
- } else if (command.contains("Exercise")) {
- String lessonTitle = command.split(":")[1];
- String exerciseLesson = lessonTitle + "-" + "Exercise";
- if (list.contains(lessonTitle)) {
- int indexLesson = list.indexOf(lessonTitle);
- if (indexLesson < list.size() - 1) {
- list.add(indexLesson + 1, exerciseLesson);
- } else {
- list.add(exerciseLesson);
- }
- } else {
- list.add(lessonTitle);
- list.add(exerciseLesson);
- }
- }
- command = scanner.nextLine();
- }
- for (int i = 1; i <= list.size(); i++) {
- System.out.printf("%d.%s%n", i, list.get(i - 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment