Question

I'm trying to self-study C# and was trying to create a text CD player to train methods and classes. I almost got it working, but apparently I haven't completely understood this.* function and messed the code somehow, so now I'm getting the message about volume twice and I can't find where this comes from.

Edit: After command "Status" program shows Volume status twice.

 namespace Text_CDPlayer
    {
        class CDPlayer
        {
            private int Voice;
            private static int Songs = 16;
            private int CurrentTrack;
            private string Command;
            private static string mode;



            public CDPlayer()
            {
                Voice = 0;
                CurrentTrack = 0;
                Command = "";
            }

            public CDPlayer(int Voice, int CurrentTrack, string Command)
            {
                this.Voice = VolumeStatus(Voice);
                this.CurrentTrack = CurrentTrack;
                this.Command = Command;
            }



            public int WhatStrength()
            {
                return VolumeStatus(Voice);
            }

            public void SetStrength(int Voice)
            {
                this.Voice = VolumeStatus(Voice);
            }






            public int VolumeStatus(int status)
            {

                switch (status)
                {
                    case 0:
                        Console.WriteLine("Mute");
                        break;
                    case 1:
                        Console.WriteLine("Volume set to 10%");
                        break;
                    case 2:
                        Console.WriteLine("Volume set to 20%");
                        break;

                    case 3:
                        Console.WriteLine("Volume set to 30%");
                        break;
                    case 4:
                        Console.WriteLine("Volume set to 40%");
                        break;
                    case 5:
                        Console.WriteLine("Volume set to 50%");
                        break;
                    case 6:
                        Console.WriteLine("Volume set to 60%");
                        break;
                    case 7:
                        Console.WriteLine("Volume set to 70%");
                        break;
                    case 8:
                        Console.WriteLine("Volume set to 80%");
                        break;
                    case 9:
                        Console.WriteLine("Volume set to 90%");
                        break;

                    default:
                        Console.WriteLine("Invalid selection. Please select volume 0 to 9");
                        break;
                }

                return status;
            }

            public string CommandMode(string command)
            {

                switch (command)
                {

                    case "Voice":
                        this.Voice = Keyboard.ReadInt("Set the new volume level (from 0 to 9): ");
                        Console.WriteLine(VolumeStatus(Voice));
                        break;

                    case "Status":
                        Console.WriteLine(this.ToString());
                        break;

                        break;                    
                }
                return command;
            }


            public override string ToString()
            {
                mode = CurrentMode("");
                return "\nCurrent cd-player status is:\n" + VolumeStatus(Voice) + "\ncurrent song is " + CurrentTrack +
                    "\n The player is currently " + mode;
            }
        }



        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Welcome to CD_Player! Press any key to start! \n");

                Console.ReadLine();
            Start:
                int song = Keyboard.ReadInt("Enter a song number you want to play: ");
                if (1 < song && song > 16)
                {
                    Console.WriteLine("There are only 16 songs!");
                    goto Start;
                }


                int Volume = Keyboard.ReadInt("\nSet the volume level (from 0 to 9): ");


                string command = Keyboard.ReadString("\nInput a command for the player (Voice/Stop/Play/Pause/Back/Next/Status), 'Exit' to finish: ");



                CDPlayer player = new CDPlayer(Volume, song, command);

                player.SetCommand(command);

            Player:
                if (command != "Exit")
                {
                    command = Keyboard.ReadString("\nInput a command for the player (Voice/Stop/Play/Pause/Back/Next/Status), 'Exit' to finish: ");
                    player.SetCommand(command);
                    goto Player;
                }
                else
                {
                    Console.ReadLine();
                }



            }
        }
    }
Was it helpful?

Solution

   CDPlayer player = new CDPlayer(Volume, song, command);

   player.SetCommand(command);

When you declare CDPlayer class you use constructor to initialize property see below method.

public CDPlayer (int Voice, int CurrentTrack, string Command)
    {
      this.Voice = VolumeStatus(Voice);
      this.CurrentTrack = CurrentTrack;
      this.Command = Command;
    }

here, remove VolumeStatus use only "this.Voice=Voice" because in VolumeStatus method you use Console.WriteLine to print volume set.

player.SetCommand(command);

this method also do same thing to print volume set.

 case "Voice":
      this.Voice = Keyboard.ReadInt("Set the new volume level (from 0 to 9): ");
      Console.WriteLine(VolumeStatus(Voice));                   
      break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top