elena1234

SoftUni Course Planning

Sep 28th, 2020 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SoftUniCourse
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             List<string> scheduleList = Console.ReadLine().Split(", ").ToList();
  12.             string command = Console.ReadLine();
  13.             while (command != "course start")
  14.             {
  15.                 string[] commandArray = command.Split(':').ToArray();
  16.  
  17.                 if (commandArray[0] == "Add")
  18.                 {
  19.                     Add(scheduleList, commandArray);
  20.                 }
  21.  
  22.                 else if (commandArray[0] == "Insert")
  23.                 {
  24.                     Insert(scheduleList, commandArray);
  25.  
  26.                 }
  27.  
  28.                 else if (commandArray[0] == "Remove")
  29.                 {
  30.                     Remove(scheduleList, commandArray);
  31.  
  32.                 }
  33.  
  34.                 else if (commandArray[0] == "Swap")
  35.                 {
  36.                     string firstLesson = commandArray[1];
  37.                     string secondLesson = commandArray[2];
  38.                     if (scheduleList.Exists(x => x == firstLesson) && scheduleList.Exists(x => x == secondLesson))
  39.                     {
  40.                         Swap(scheduleList, firstLesson, secondLesson);
  41.                     }
  42.  
  43.                     else
  44.                     {
  45.                         continue;
  46.                     }
  47.                 }
  48.  
  49.                 else if (commandArray[0] == "Exercise")
  50.                 {
  51.                     string lessonTitle = commandArray[1];
  52.                     ExerciseCommand(scheduleList, lessonTitle);
  53.  
  54.                 }
  55.  
  56.                 command = Console.ReadLine();
  57.             }
  58.  
  59.             int counter = 1;
  60.             foreach (var item in scheduleList)
  61.             {
  62.                 Console.WriteLine($"{counter}.{item}");
  63.                 counter++;
  64.             }
  65.         }
  66.  
  67.         private static void ExerciseCommand(List<string> scheduleList, string lessonTitle)
  68.         {
  69.             if (scheduleList.Contains(lessonTitle) && (scheduleList.Contains($"{lessonTitle}-Exercise") == false))
  70.             {
  71.                 int indexOfLessonTitle = scheduleList.IndexOf(lessonTitle);
  72.                 scheduleList.Insert(indexOfLessonTitle + 1, $"{lessonTitle}-Exercise");
  73.             }
  74.  
  75.             else if (scheduleList.Contains(lessonTitle) == false)
  76.             {
  77.                 scheduleList.Add(lessonTitle);
  78.                 scheduleList.Add($"{lessonTitle}-Exercise");
  79.             }
  80.         }
  81.  
  82.         private static void Swap(List<string> scheduleList, string firstLesson, string secondLesson)
  83.         {
  84.             int firstIndex = scheduleList.IndexOf(firstLesson);
  85.             int secondIndex = scheduleList.IndexOf(secondLesson);
  86.             scheduleList[firstIndex] = secondLesson;
  87.             scheduleList[secondIndex] = firstLesson;
  88.  
  89.             if (scheduleList.Contains($"{firstLesson}-Exercise"))
  90.             {
  91.                 scheduleList.Remove($"{firstLesson}-Exercise");
  92.                 int indexOfFirstLesson = scheduleList.IndexOf(firstLesson);
  93.                 scheduleList.Insert(indexOfFirstLesson + 1, $"{firstLesson}-Exercise");
  94.             }
  95.  
  96.             if (scheduleList.Contains($"{secondLesson}-Exercise"))
  97.             {
  98.                 scheduleList.Remove($"{secondLesson}-Exercise");
  99.                 int indexOfSecondLesson = scheduleList.IndexOf(secondLesson);
  100.                 scheduleList.Insert(indexOfSecondLesson + 1, $"{secondLesson}-Exercise");
  101.             }
  102.         }
  103.  
  104.         private static void Remove(List<string> scheduleList, string[] commandArray)
  105.         {
  106.             string lessonTitle = commandArray[1];
  107.             if (scheduleList.Contains(lessonTitle))
  108.             {
  109.                 scheduleList.Remove(lessonTitle);
  110.             }
  111.             if (scheduleList.Contains($"{lessonTitle}-Exercise"))
  112.             {
  113.                 scheduleList.Remove($"{lessonTitle}-Exercise");
  114.             }
  115.         }
  116.  
  117.         private static void Insert(List<string> scheduleList, string[] commandArray)
  118.         {
  119.             string lessonTitle = commandArray[1];
  120.             int index = int.Parse(commandArray[2]);
  121.  
  122.             if (scheduleList.Contains(lessonTitle) == false && index >= 0 && index < scheduleList.Count)
  123.             {
  124.                 scheduleList.Insert(index, lessonTitle);
  125.             }
  126.         }
  127.  
  128.         private static void Add(List<string> scheduleList, string[] commandArray)
  129.         {
  130.             string lessonTitle = commandArray[1];
  131.  
  132.             if (scheduleList.Contains(lessonTitle) == false)
  133.             {
  134.                 scheduleList.Add(lessonTitle);
  135.             }
  136.         }
  137.     }
  138. }
  139.  
  140.  
Add Comment
Please, Sign In to add comment