YORDAN2347

SoftUniCoursePlaning

Feb 1st, 2021 (edited)
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _10._SoftUni_Course_Planning
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 1. read schedule, seperated by ", "
  12.             List<string> schedule = Console.ReadLine()
  13.                 .Split(", ", StringSplitOptions.RemoveEmptyEntries)
  14.                 .ToList();
  15.  
  16.             List<string> exercises = new List<string>();
  17.  
  18.             // 2. while(input != "course start") ----> read comands
  19.             string input = Console.ReadLine();
  20.             while (input != "course start")
  21.             {
  22.                 string[] commandParts = input
  23.                     .Split(":", StringSplitOptions.RemoveEmptyEntries);
  24.                 switch (commandParts[0])
  25.                 {
  26.                     // 2.1 Add:{lessonTitle} – add the lesson to the end of the schedule, if it does not exist.
  27.                     case "Add":
  28.                         string newLessonTitle = commandParts[1];
  29.                         if (!schedule.Contains(newLessonTitle))
  30.                         {
  31.                             schedule.Add(newLessonTitle);
  32.                         }
  33.                         break;
  34.  
  35.                     // 2.2 Insert:{lessonTitle}:{index} – insert the lesson to the given index, if it does not exist.
  36.                     case "Insert":
  37.                         newLessonTitle = commandParts[1];
  38.                         int index = int.Parse(commandParts[2]);
  39.                         if (!schedule.Contains(newLessonTitle))
  40.                         {
  41.                             schedule.Insert(index, newLessonTitle);
  42.                         }
  43.                         break;
  44.  
  45.                     // 2.3 Remove:{lessonTitle} – remove the lesson, if it exists.
  46.                     case "Remove":
  47.                         newLessonTitle = commandParts[1];
  48.                         if (schedule.Contains(newLessonTitle))
  49.                         {
  50.                             schedule.Remove(newLessonTitle);
  51.                         }
  52.                         break;
  53.  
  54.                     // 2.4 Swap:{lessonTitle}:{lessonTitle} – change the place of the two lessons, if they exist.
  55.                     case "Swap":
  56.                         string lesson1 = commandParts[1];
  57.                         string lesson2 = commandParts[2];
  58.                         Swap(schedule, lesson1, lesson2);
  59.                         break;
  60.  
  61.                     // 2.5 Exercise:{lessonTitle} – add Exercise in the schedule right after the lesson index,
  62.                     case "Exercise":
  63.                         newLessonTitle = commandParts[1];
  64.                         AddExercise(newLessonTitle, schedule, exercises);
  65.                         break;
  66.                 }
  67.                 input = Console.ReadLine();
  68.             }
  69.             Print(schedule, exercises);
  70.         }
  71.  
  72.         private static void AddExercise(string newLessonTitle, List<string> schedule, List<string> exercises)
  73.         {
  74.             // 2.5.1 if the lesson exists and there is no exercise already,"{lessonTitle}-Exercise".
  75.             if (schedule.Contains(newLessonTitle) &&
  76.                 !schedule.Contains($"{newLessonTitle}-Exercise"))
  77.             {
  78.                 exercises.Add($"{newLessonTitle}-Exercise");
  79.             }
  80.             // 2.5.2 If the lesson doesn`t exist, schelude.add(lesson), followed by the Exercise.
  81.             else if (!schedule.Contains(newLessonTitle))
  82.             {
  83.                 schedule.Add(newLessonTitle);
  84.                 exercises.Add($"{newLessonTitle}-Exercise");
  85.             }
  86.         }
  87.  
  88.         private static void Print(List<string> schedule, List<string> exercises)
  89.         {
  90.             int i = 1;
  91.             foreach (var item in schedule)
  92.             {
  93.                 Console.WriteLine($"{i}.{item}");
  94.                 if (exercises.Contains($"{item}-Exercise"))
  95.                 {
  96.                     i++;
  97.                     Console.WriteLine($"{i}.{item}-Exercise");
  98.                 }
  99.                 i++;
  100.             }
  101.         }
  102.  
  103.         private static void Swap(List<string> schedule, string lesson1, string lesson2)
  104.         {
  105.             // 2.4.1 swap Lessons
  106.             if ((schedule.Contains(lesson1)) && (schedule.Contains(lesson2)))
  107.             {
  108.                 string temp = lesson1;
  109.                 int index1 = schedule.IndexOf(lesson1);
  110.                 int index2 = schedule.IndexOf(lesson2);
  111.                 schedule[index1] = lesson2;
  112.                 schedule[index2] = temp;
  113.             }
  114.  
  115.             // 2.4.2 swap Exercise, if they exists
  116.             /*
  117.             MoveExercise(schedule, lesson1);
  118.             MoveExercise(schedule, lesson2);
  119.             */
  120.            
  121.         }
  122.  
  123.         private static void MoveExercise(List<string> schedule, string lesson)
  124.         {
  125.             if (schedule.Contains($"{lesson}-Exercise"))
  126.             {
  127.                 string temp = $"{lesson}-Exercise";
  128.                 schedule.Remove($"{lesson}-Exercise");
  129.                 int lessonIndex = schedule.IndexOf(lesson);
  130.                 schedule.Insert(lessonIndex + 1, temp);
  131.             }
  132.         }
  133.     }
  134. }
  135.  
Add Comment
Please, Sign In to add comment