我无法在程序中使用视频类。我的代码运行正常,但根据问题我应该使用视频类
但是我没有使用它就得到了期望的输出
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"));
}
}
答案 0 :(得分:0)
我不会为您提供全部代码,而只是让您入门。
当VideoStore
扩展Video
时,您说VideoStore
是 Video
,这是错误的,您想说的是{{ 1}} 有 VideoStore
个。您需要的是Video
中的Video
列表
所以您的VideoStore
应该看起来像这样:
VideoStore
根据您的询问:
我每次都不知道为什么每次执行后都会重置videoStore数组值
这是正常现象,它们仅在内存中,并且在程序终止后将被清除。