Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace SongsQueue
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Read a sequence of strings, separated by a comma and a white space
- string[] songs = Console.ReadLine()
- .Split(", ", StringSplitOptions.RemoveEmptyEntries);
- //Create a song's queue
- Queue<string> playList = new Queue<string>();
- //Add songs in playlist
- foreach (string song in songs)
- {
- playList.Enqueue(song);
- }
- //While receiving the commands, print the proper messages described above
- while (true)
- {
- if (playList.Count == 0)
- {
- Console.WriteLine($"No more songs!");
- break;
- }
- //Read a command from the console
- string command = Console.ReadLine();
- if (command == "Play")
- {
- //plays a song (removes it from the queue)
- playList.Dequeue();
- }
- else if (command == "Show")
- {
- //prints all songs in the queue separated by a comma and a white space
- Console.WriteLine(String.Join(", ", playList));
- }
- else //(Add)
- {
- //Find the index of the first white space in the command
- int index = command.IndexOf(' ');
- //Get substring after the index of white space
- string song = command.Substring(index + 1);
- if (playList.Contains(song))
- {
- //if the song is already in the queue, skip the command
- Console.WriteLine($"{song} is already contained!");
- }
- else
- {
- //adds the song to the queue if it isn’t contained already
- playList.Enqueue(song);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment