Thursday, October 13, 2016

CD player simulator using c++

Problem

Design a simple CD player simulator. The CD player has the following buttons: "POWER", "EJECT", "PLAY" and "STOP", and a tray where a CD is loaded. User can load a CD only if the tray is open. The tray can be opened only if the power is turned on (regard it as something like a motor-driven tray installed on a PC). If the power is turned off with the tray open, the tray will keep open and the power is turned off.
Implement the following actions of the CD player as methods of class CDPlayer.
  • pushPower() -- simulates pushing POWER(on/off) button.
    • if the power is turned off, turn on the power, and display "Power turned on."
    • if the power is turned on,
      • if playing, stop it.
turn off the power, and display "Power turned off."
  • pushEject() -- simulates pushing EJECT(tray open/close) button
    • if the power is not turned on, display "Power not turned on."
    • if the power is turned on,
      • if the tray is not open, open the tray and display "Tray opened."
      • if the tray is open, close the tray and display "Tray closed."
  • pushPlay() -- simulates pushing PLAY button
    • if the power is turned on,
      • if the media is loaded and tray is closed, begin to play and display "Now playing..."
      • otherwise, display "No CD loaded or tray is open."
  • pushStop() -- simulates pushing STOP button
    • if the power is turned on,
      • if it's playing, stop playing and display "Playing stopped."
  • loadUnloadMedia() -- simulates loading/unloading media to/from the tray (tray must be open when loading/unloading)
    • if the tray is open,
      • if the media is loaded, unload the media and display "Media unloaded."
      • if the media is not loaded, load the media and display "Media loaded."
    • if the tray is not open,
      • display "Tray is not open."

Code

#include<iostream>
#include<conio.h>
using namespace std;


class CDplayer
{
      private:
              int power;
              int eject;
              int tray;
              int media;
              int play;
              
      public:
      CDplayer():power(0),eject(0),tray(0),media(0),play(0)
      {
      }
      
      void pushPower()
      {
           cout<<"\n\nNOTE: Press one(1) to turn on and zero(0) to turn off.";
           cout<<"\nWant to turn on the power? ";
           cin>>power;
           
           if(power == 1)
                cout<<"Power is ON.";
           else if (power == 0)
                cout<<"Power is OFF.";
           else
               cout<<"Invalid key";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushEject()
      {
           if(power == 1)
           {
                if(tray == 0)
                {
                         tray = 1;
                         cout<<"Tray opened.";
                }
                else if(tray == 1)
                {
                     tray = 0;
                     cout<<"Tray closed.";
                }
           }
           else if(power == 0)
                cout<<"Power not turned on.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushPlay() 
      {
           if(power == 1)
           {
                if(media == 1 && tray == 0)
                {
                    play = 1;
                    cout<<"Now playing...";
                }
                else
                    cout<<"No CD loaded or tray is open.";
           }
           else if(power == 0)
                cout<<"Power not turned on.";
                
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushStop() 
      {
           if(power == 1)
           {
                if(play == 1)
                {
                        play = 0;
                        cout<<"Playing stopped.";
                }
           }
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void loadUnloadMedia()
      {
           if(tray == 1)
           {
                   if(media == 1)
                   {
                            media = 0;
                            cout<<"Media unloaded.";
                   }
                   else if(media == 0)
                   {
                        media = 1;
                        cout<<"Media loaded.";
                   }
           }
           else if(tray == 0)
                cout<<"Tray is not open.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
};

int main()
{
    CDplayer c1;
    int s;
    while(1)
    {
            system ("cls");
            cout<<"\n\nPlease, Press one of the following buttons: ";
            cout<<"\n1. POWER(on/off)";
            cout<<"\n2. EJECT(tray open/close)";
            cout<<"\n3. PLAY";
            cout<<"\n4. STOP";
            cout<<"\n5. Load/Unload media to/from the tray(tray will be opened)";
            cout<<"\n6. Close CDplayer";
            
            cout<<"\n\nEnter your choice: ";
            cin>>s;
            
            switch(s)
            {
                     case 1:
                          system ("cls");
                          c1.pushPower();
                          break;
            
                     case 2:
                          system ("cls");
                          c1.pushEject();
                          break;
            
                     case 3:
                          system ("cls");
                          c1.pushPlay();
                          break;
            
                     case 4:
                          system ("cls");
                          c1.pushStop();
                          break;
            
                     case 5:
                          system ("cls");
                          c1.loadUnloadMedia();
                          break;
            
                     case 6:
                          exit(0);
            }
    }
    
    getch();
    return 0;
}

Output

CD player simulator using c++
CD player simulator using c++

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact