Mini project exercise 3 remote control to play CD DVD Tape

topic

Define an interface MediaPlayer, which represents a device of home theater. MediaPlayer
contains three methods: play(), stop(), and open(), which respectively represent the functions of play, stop and open position.
MediaPlayer has three implementation classes, namely: DVDPlayer, which means DVD player; CDPlayer, which
means CD player; TapePlayer, which means sound recorder (playing tape).
The class diagram is as follows:
Insert picture description here

Create a remote controller Controller class. The remote control has three control channels, which can control three
devices separately. Part of the code is as follows:
class Controller{ private MediaPlayer[] players; public Controller(){ //Initialize the players array in the constructor } //Call the play method on the corresponding device public void play(int i){ players[i].play (); } } Requirements:









  1. Complete the code for the MediaPlayer interface and its subclasses.
  2. Complete the Controller, improve its constructor, and add stop (int i) and
    open (int i) methods to it

Code display

Interface MediaPlayer

public interface MediaPlayer {
    
    
     //播放 停止 开仓
    void play();
    void stop();
    void open();
}

TapePlayer class

public class TapePlayer implements MediaPlayer{
    
    
    @Override
    public void play() {
    
    
        System.out.println("Tape开始播放");
    }

    @Override
    public void stop() {
    
    
        System.out.println("Tape停止播放");
    }

    @Override
    public void open() {
    
    
        System.out.println("Tape开仓");
    }
}

DVDPlayer class

public class DVDPlayer implements MediaPlayer{
    
    
    @Override
    public void play() {
    
    
        System.out.println("DVD开始播放");
    }

    @Override
    public void stop() {
    
    
        System.out.println("DVD停止播放");
    }

    @Override
    public void open() {
    
    
        System.out.println("DVD开仓");
    }
}

CDPlayer class

public class CDPlayer implements MediaPlayer{
    
    
    @Override
    public void play() {
    
    
        System.out.println("CD开始播放");
    }

    @Override
    public void stop() {
    
    
        System.out.println("CD停止播放");
    }

    @Override
    public void open() {
    
    
        System.out.println("CD开仓");
    }
}

Controller class

public class Controller {
    
    
        private MediaPlayer[] players;
        //在Controller构造方法中初始化数组,添加三个元素
        public Controller(){
    
    
            players =new MediaPlayer[] {
    
    new TapePlayer(),new DVDPlayer(),new CDPlayer()};
        }

        public void play(int i){
    
    
            players[i].play();
        }
        public void stop(int i){
    
    
            players[i].stop();
        }
        public void open(int i){
    
    
            players[i].open();
        }
}

Test class

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Controller controller = new Controller();
        controller.open(0);
        controller.play(0);
        controller.stop(0);

        controller.open(1);
        controller.play(1);
        controller.stop(1);

        controller.open(2);
        controller.play(2);
        controller.stop(2);
    }
}

Program running results

Insert picture description here

Guess you like

Origin blog.csdn.net/hypertext123/article/details/115373316