有什么办法可以在我的代码中使用video类

时间:2019-03-14 05:00:05

标签: java arrays

我无法在程序中使用视频类。我的代码运行正常,但根据问题我应该使用视频类

但是我没有使用它就得到了期望的输出


import java.util.Scanner;  
class Video{
     String videoName;
     boolean checkout;
     int rating;
     Video(String name){
         videoName=name;
     }
     String getName(){
         return videoName;
     }
     void doCheckout(){

     }
     void doReturn(){


         }
     void receiveRating(int rating){
         this.rating=rating;
     }
     int getRating(){
         return rating;
     }
     boolean getCheckout(){
         return checkout;
     }
 }

videoStore扩展了视频类,但是没有视频类方法,每次执行后我都不知道为什么videoStore数组值会重置

class VideoStore extends Video{
    String videoStore[]=new String[100];
    int rating[]=new int[100];
    boolean status[]=new boolean[100];
    int i=0,ind=0,len=-1;
    VideoStore(String name) {
        super(name);
    }
    void addVideo(String name){
        videoStore[i]=name;
        status[i]=false;
        i++;
        len++;


    }
    void doCheckout(String name){
        if(len>=0){
        int index=index(name);
        status[index]=true;
        }
        else{
            System.out.println("Nothig in inventory");
        }

    }
    void doReturn(String name){
        if(len>=0){
        int index=index(name);
        status[index]=false;
        }
        else{
            System.out.println("Nothing in inventory");
        }

    }
    void receiveRating(String name,int rating){
        if(len>=0){
        int index=index(name);
        this.rating[index]=rating;
        }
        else{
            System.out.println("Nothing in inventory");
        }
    }
    void listInventory(){
        if(len!=-1){
        for(int p=0;p<=len;p++){
            System.out.println(videoStore[p]+"\t"+status[p]+"\t"+rating[p]);
        }
        }
        else{
            System.out.println("nothing in inventory");
        }

    }
    int index(String name){
        for (int i = 0; i < videoStore.length; i++) {
            if(videoStore[i].equals(name)){
                return i;
            }
        }
        return -1;
    }

 }

这是主要方法

public class VideoLauncher   {


    public static void main(String[] args) {
        String yn="y";
        int option=0;
        String  videoName="";
        VideoStore vs=new VideoStore(videoName);
        Scanner sc=new Scanner(System.in);
        do{
        System.out.println("1.Add video");
        System.out.println("2.Checkout video");
        System.out.println("3.Return video");
        System.out.println("4.recieve rating");
        System.out.println("5.List inventory");
        System.out.println("6.Exit");
        System.out.println("please enter the option from 1..6");
        option=sc.nextInt();

            switch(option){
            case 1: 
                System.out.println("please enter the video name");
                        videoName=sc.next();
                        Video v=new Video(videoName);
                        vs.addVideo(videoName);
                        System.out.println("video added successfully");
                        System.out.println("do you want to continue(y) else (n)");
                        yn=sc.next();
                        break;
            case 2:
                System.out.println("please enter the name of the video to checkout");
                videoName=sc.next();
                vs.doCheckout(videoName);
                System.out.println("Video "+videoName+" checkedout successfully");
                System.out.println("do you want to continue(y) else (n)");
                yn=sc.next();
                break;
            case 3:
                System.out.println("please enter the name of the video to return");
                videoName=sc.next();
                vs.doReturn(videoName);
                System.out.println("Video "+videoName+" returned successfully");
                System.out.println("do you want to continue(y) else (n)");
                yn=sc.next();
                break;
            case 4:
                System.out.println("enter the videoname");
                videoName=sc.next();
                System.out.println("enter the rating");
                int rating=sc.nextInt();
                vs.receiveRating(videoName,rating);
                System.out.println("rating "+rating+" for video"+videoName+" recorded");
                System.out.println("do you want to continue(y) else (n)");
                yn=sc.next();
                break;
            case 5:

                vs.listInventory();
                System.out.println("do you want to continue(y) else (n)");
                yn=sc.next();
                break;
            case 6:
                yn="n";
                System.out.println("exiting");
                break;
            default :
                System.out.println("please enter proper option");

            }

        }while(yn.equals("y"));


    }

}

1 个答案:

答案 0 :(得分:0)

我不会为您提供全部代码,而只是让您入门。

VideoStore扩展Video时,您说VideoStore Video,这是错误的,您想说的是{{ 1}} VideoStore个。您需要的是Video中的Video列表

所以您的VideoStore应该看起来像这样:

VideoStore

根据您的询问:

  

我每次都不知道为什么每次执行后都会重置videoStore数组值

这是正常现象,它们仅在内存中,并且在程序终止后将被清除。

相关问题