Advertisement
yanass

SoftUni Course Planning

Jun 20th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Softuni_course_planning
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             List<string> scheduleSoftUni = Console.ReadLine()
  12.                 .Split(", ")
  13.                 .ToList();
  14.  
  15.             while (true)
  16.             {
  17.                 string changes = Console.ReadLine();
  18.  
  19.                 if (changes == "course start")
  20.                     break;
  21.  
  22.                 else
  23.                 {
  24.                     string[] changesInfo = changes.Split(':').ToArray();
  25.                     string lesson = changesInfo[1];
  26.  
  27.                     switch (changesInfo[0])
  28.                     {
  29.                         case "Add":
  30.                             {
  31.                                 AddLesson(scheduleSoftUni, lesson);
  32.  
  33.                                 break;
  34.                             }
  35.  
  36.                         case "Insert":
  37.                             {
  38.                                 int index = int.Parse(changesInfo[2]);
  39.                                 InsertLesson(scheduleSoftUni, lesson, index);
  40.                                 break;
  41.                             }
  42.  
  43.                         case "Remove":
  44.                             {
  45.                                 RemoveLesson(scheduleSoftUni, lesson);
  46.                                 RemoveLesson(scheduleSoftUni, $"{lesson}-Exercise");
  47.  
  48.                                 break;
  49.                             }
  50.  
  51.                         case "Swap":
  52.                             {
  53.                                 string lesson2 = changesInfo[2];
  54.  
  55.                                 if (scheduleSoftUni.Contains(lesson) && scheduleSoftUni.Contains(lesson2))
  56.                                 {
  57.                                     int index = scheduleSoftUni.IndexOf(lesson);
  58.                                     int index2 = scheduleSoftUni.IndexOf(lesson2);
  59.  
  60.                                     scheduleSoftUni[index] = lesson2;
  61.                                     scheduleSoftUni[index2] = lesson;
  62.  
  63.                                     MoveExercise(scheduleSoftUni, lesson, index2);
  64.  
  65.                                     MoveExercise(scheduleSoftUni, lesson2, index);
  66.  
  67.                                 }
  68.  
  69.  
  70.                                 break;
  71.                             }
  72.  
  73.                         case "Exercise":
  74.                         {
  75.                             string exercise = $"{lesson}-Exercise";
  76.                                 if (scheduleSoftUni.Contains(lesson) && !scheduleSoftUni.Contains(exercise))
  77.                                 {
  78.                                     if (scheduleSoftUni.IndexOf(lesson) < scheduleSoftUni.Count - 1)
  79.                                         scheduleSoftUni.Insert(scheduleSoftUni.IndexOf(lesson) + 1, exercise);
  80.                                     else
  81.                                         scheduleSoftUni.Add(exercise);
  82.                                 }
  83.  
  84.                                 else if (!scheduleSoftUni.Contains(lesson) && !scheduleSoftUni.Contains(exercise))
  85.                                 {
  86.                                     scheduleSoftUni.Add(lesson);
  87.                                     scheduleSoftUni.Add(exercise);
  88.                                 }
  89.                                 break;
  90.                             }
  91.                     }
  92.  
  93.                 }
  94.  
  95.  
  96.             }
  97.  
  98.             for (int i = 0; i < scheduleSoftUni.Count; i++)
  99.             {
  100.                 Console.WriteLine($"{i + 1}.{scheduleSoftUni[i]}");
  101.             }
  102.  
  103.         }
  104.  
  105.         static void MoveExercise(List<string> schedule, string course, int position)
  106.         {
  107.             if (schedule.Contains($"{course}-Exercise"))
  108.             {
  109.                 schedule.Remove($"{course}-Exercise");
  110.                 if (position < schedule.Count - 1)
  111.                     schedule.Insert(position + 1, $"{course}-Exercise");
  112.                 else
  113.                     schedule.Add($"{course}-Exercise");
  114.             }
  115.         }
  116.  
  117.         private static void InsertLesson(List<string> schedule, string course, int position)
  118.         {
  119.             if (!DoesExists(schedule, course) && position >= 0 && position < schedule.Count)
  120.             {
  121.                 schedule.Insert(position, course);
  122.             }
  123.         }
  124.         static void AddLesson(List<string> schedule, string course)
  125.         {
  126.             if (!DoesExists(schedule, course))
  127.             {
  128.                 schedule.Add(course);
  129.             }
  130.         }
  131.  
  132.         static void RemoveLesson(List<string> schedule, string course)
  133.         {
  134.             if (DoesExists(schedule, course))
  135.             {
  136.                 schedule.Remove(course);
  137.             }
  138.         }
  139.  
  140.         static bool DoesExists(List<string> schedule, string course)
  141.         {
  142.             return schedule.Contains(course);
  143.         }
  144.  
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement