javaarraysclassobjectflow-control

Array is cleared when adding a new element


I've implemented a Video rental service.

VideoStore class maintains an array of Video objects.

When am trying to add a new video to the inventory it inserts the new element successfully. But all elements that were previously added are no longer accessible.

Why does it happen?

public class Video {

    String videoName;
    boolean checkout;
    int rating;
    
    Video (String name){
        this.videoName=name;
    }
    
    String getName() {
        return videoName;
    }
    void doCheckout() {
        this.checkout=true;
    }
    void doReturn() {
        this.checkout=false;
    }
    void receiveRating(int rating) {
        this.rating=rating;
    }
    int getRating() {
        return rating;
    }
    boolean getCheckout() {
        return checkout;
    }
}
public class VideoStore {

    Video[] store=null;
    
    int checkVideo(String name) {
        int count =0;
        if(store==null) {
            count =0;
        }else
            for(Video video:store) {
                if(video!=null) {
                if(video.getName().equals(name))
                    count=1;
            }}
            return count;
    }
    
    void addVideo(String name) {
        int s;
        
        Video video = new Video(name);
        if(store==null)
            s=0;
        else {
            s=store.length;
        }
        
        Video[] store2 = new Video[s+1];
        store2[s]=video;
        store=store2;
    }
    
    void doCheckout(String name) {
        if(store == null || store.length==0) {
            System.out.println("Store is empty");
            return;
        }else
            for(Video video:store) {
                if(video!=null) {
                if(video.getName().equals(name))
                    video.doCheckout();
            }}
            
    }
    
    void doReturn(String name) {
        if(store == null || store.length==0) {
            System.out.println("Store is empty");
            return;
        }else
        
            for(Video video:store) {
                if(video!=null) {
                if(video.getName().equals(name))
                    video.doReturn();
            }}
    }
    void receiveRating(String name, int rating) {
        if(store == null || store.length==0) {
            System.out.println("Store is empty");
            return;
        } else
        
            for(Video video:store) {
                if(video!=null) {
                if(video.getName().equals(name))
                    video.receiveRating(rating);
            }}
        
    }
    void listInventory() {
        if(store == null || store.length==0) {
            System.out.println("Store is empty");
            return;
        }else
        
            for(Video video:store) {
                if(video!=null) {
                System.out.print("---------------------------------------------------------------------------");
                System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", "Video Name","Checkout Status","Rating");
                System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", video.getName(), video.getCheckout(),video.getRating());
                System.out.println("---------------------------------------------------------------------------");
            }
            }
    }
    
}
import java.util.Scanner;
public class VideoLauncher {
    
    public static void main(String[] args) {
        
        Scanner sc =new Scanner(System.in);
        int input=0;
        
        VideoStore vStore =new VideoStore();
        
        while(true) {
            System.out.println("\n1. Add Videos : "+"\n"+"2. Check Out Video : "+"\n"+"3. Return Video : "+
                       "\n"+"4. Receive Rating : "+"\n"+"5. List Inventory : "+"\n"+"6. Exit : ");
            System.out.print("Enter Your Choice (1..6) : ");
            if(!sc.hasNextInt()) {
                sc.next();
                input=0;
            } 
            String name;
            input =sc.nextInt();
            sc.nextLine();
            
            switch(input) {
            case 1:
                System.out.print("Enter the name of the video you want to add : ");
                name =sc.nextLine();
                
                vStore.addVideo(name);
                System.out.println("Video '" +name+"' added successfully.");
                
                break;
                
            case 2:
                System.out.print("Enter the name of the video you want to check out : ");
                name = sc.nextLine();if(vStore.checkVideo(name)==0) {
                    System.out.println("Video does not exist.");
                }else {
                
                    vStore.doCheckout(name);
                System.out.println("Video '"+name+"' checked out successfully.");
                }
                break;
                
            case 3:
                System.out.print("Enter the name of the video you want to return : ");
                name = sc.nextLine();
                if(vStore.checkVideo(name)==0) {
                    System.out.println("Video does not exist.");
                }else {
                    vStore.doReturn(name);
                System.out.println("Video '"+name+"' returned successfully.");
                }
                break;
                
            case 4:
                System.out.print("Enter the name of the video you want to rate : ");
                name = sc.nextLine();
                if(vStore.checkVideo(name)==0) {
                    System.out.println("Video does not exist.");
                }else {
            
                    System.out.print("Enter the rating for this video : ");
                    int rating = sc.nextInt();
                    vStore.receiveRating(name, rating);
                    System.out.println("Rating '"+rating+"' has been mapped to the Video '"+name+"'.");
                }
                break;
                
            case 5:
                vStore.listInventory();
                break;
            case 6:
                sc.close();
                System.out.print("Exiting...!! Thanks for using the application.");
                return;
            default:
                System.out.println("Please enter correct choice.");
    }}
    }
}

Solution

  • Take a look at the addVideo method:

    void addVideo(String name) {
        int s;
        
        Video video = new Video(name);
        if(store==null)
            s=0;
        else {
            s=store.length;
        }
        
        Video[] store2 = new Video[s+1];
    
        // This array only has a single item in it: the new video is at the end of the
        // array, and the rest of the array is empty.
        // Be sure to preserve the original content.
        //
        // Copying the entire array every time you add a video is not
        // very efficient, though, because it means
        // that adding a new video will always be an O(n) operation, so you may want to
        // consider an alternative implementation here.
        store2[s]=video;
    
        // You replace the entire array here
        store=store2;
    }
    

    In this method, you replace the original array every time with an array that contains only the video you just added.

    Also, as I mentioned in the code comments, an array is a somewhat suboptimal data structure here because it means that adding or checking out videos are both O(n) operations. You may want to consider a hash table instead.