Viktomirova

SongsQueue

Jan 5th, 2022
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SongsQueue
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //Read a sequence of strings, separated by a comma and a white space
  11.             string[] songs = Console.ReadLine()
  12.                                     .Split(", ", StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             //Create a song's queue
  15.             Queue<string> playList = new Queue<string>();
  16.  
  17.             //Add songs in playlist
  18.             foreach (string song in songs)
  19.             {
  20.                 playList.Enqueue(song);
  21.             }
  22.  
  23.             //While receiving the commands, print the proper messages described above
  24.             while (true)
  25.             {
  26.                 if (playList.Count == 0)
  27.                 {
  28.                     Console.WriteLine($"No more songs!");
  29.                     break;
  30.                 }
  31.  
  32.                 //Read a command from the console
  33.                 string command = Console.ReadLine();
  34.  
  35.                 if (command == "Play")
  36.                 {
  37.                     //plays a song (removes it from the queue)
  38.                     playList.Dequeue();
  39.                 }
  40.                 else if (command == "Show")
  41.                 {
  42.                     //prints all songs in the queue separated by a comma and a white space
  43.                     Console.WriteLine(String.Join(", ", playList));
  44.                 }
  45.                 else //(Add)
  46.                 {
  47.                     //Find the index of the first white space in the command
  48.                     int index = command.IndexOf(' ');
  49.  
  50.                     //Get substring after the index of white space
  51.                     string song = command.Substring(index + 1);
  52.  
  53.                     if (playList.Contains(song))
  54.                     {
  55.                         //if the song is already in the queue, skip the command
  56.                         Console.WriteLine($"{song} is already contained!");
  57.                     }
  58.                     else
  59.                     {
  60.                         //adds the song to the queue if it isn’t contained already
  61.                         playList.Enqueue(song);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment