Java-Based Video Rental Inventory System

Niraj SahaniNiraj Sahani
6 min read

Introduction

building a Video Rental System is a fun and educational way to learn about software development. In this blog, I’ll take you through my journey of creating a Video Rental System in Java, how I structured the system, the challenges I faced, the lessons I learned, and why I chose to use data structure rather than OOP.

In this project, I used classes to create objects and use them as a user-defined data structure. This project is made using a user-defined data structure where I have used an array and Video (Video class- which stores different parameters like Video Name, Rating, and Check Out).

Here is output:

Project Overview

The Video Rental System I developed is a simple yet functional Java application that allows users to:

  • Add videos to inventory: The system stores the details of available videos.

  • Check out videos: Users can rent videos and mark them as checked out.

  • Return videos: After watching, users can return videos.

  • Rate videos: Users can rate the videos they've watched.

  • List available videos: Users can view the complete list of videos in the store, including their ratings and availability.

Project Structure

Below is a breakdown of the classes and their functionalities:

1. Video Class

This class represents individual video objects. Each video has a name, a checkout status, and a rating. The Video class contains methods for setting the video's data, checking out the video, returning the video, and updating its rating.

  • Attributes:

    • videoName: Name of the video

    • checkOut: Whether the video is checked out or not

    • rating: Rating of the video

  • Key Methods:

    • setData(): Initializes the video details.

    • doCheckOut(): Marks the video as checked out.

    • doReturn(): Returns the video to the store.

    • receiveRating(): Assigns a rating to the video.

2. VideoStore Class

The VideoStore class manages the collection of videos in the store. It handles adding new videos, processing checkouts and returns, and storing the ratings provided by users.

  • Key Features:

    • Add Video: Users can add new videos to the store's inventory.

    • Check Out: Users can rent available videos.

    • Return Video: After watching, users can return videos.

    • Receive Rating: Users can rate the videos they’ve rented.

    • List Inventory: Displays a list of all videos, including their current status and ratings.

3. VideoLauncher Class

The VideoLauncher class is the entry point of the application. It provides a menu-based interface for users to interact with the system, allowing them to choose between adding videos, checking out, returning, rating, or viewing the inventory.

import java.util.Scanner;

public class Video {
    private String videoName = "";
    private boolean checkOut;
    private int rating = 0;

    //constructor of Video class
    public Video(){};

    public void setData(String videoName, boolean checkOut, int rating) {
        this.videoName = videoName;
        this.checkOut = checkOut;
        this.rating = rating;
    }

    //now code funtions of video class
    public String getVideoName() {
        return videoName;
    }

    public boolean getCheckOut() {
        return checkOut;
    }

    public int getRating() {
        return rating;
    }

    public void doCheckOut(boolean f){
        this.checkOut=f;
    }
    //use using doReturn method we just reverse the checkout status
    // checkOut =false means book is available at store

    public void doReturn(boolean flag) {
        checkOut = !flag;
    }

    //now we will write receive rating method to just receive from customer
    public void recieveRating(int rating) {
        this.rating = rating;
    }
}

class videoStore {
    Scanner sc = new Scanner(System.in);

    Video objects[] = new Video[100];
    int objCount = 0;

    String videoName = "";
    boolean checkOut;
    int rating = 0;

    //add Video
    public void addVideo(String videoName) {
        objects[objCount] = new Video();
        this.videoName = videoName;
        objects[objCount].setData(videoName, checkOut, rating);
        objCount++;
    }

    public void doCheckOut(String name) {
        int index = searchVideo(name);
        if (index == -1) {
            System.out.println("Video is Not available!! ");
        } else {
            objects[index].doCheckOut(true);
            System.out.println("Video : " + objects[index].getVideoName() + " successfully check out ");
        }
        System.out.println();
    }

    public void receiveRating(String videoName, int rating) {
        int index = searchVideo(videoName);
        if (index == -1) {
            System.out.println("Video is Not available!! ");
        } else {
            this.rating = rating;
            objects[index].recieveRating(rating);
            System.out.println("Rating " + rating + " mapped to " + objects[index].getVideoName() + " successfully ");
        }
        System.out.println();
    }

    public void doReturn(String videoName) {
        //search for videoname and then doreturn it
        int index = searchVideo(videoName);
        if (index == -1) {
            System.out.println("Video is Not available!! ");
        } else {
            if (objects[index].getCheckOut()) {
                objects[index].doReturn(true);
                System.out.println("Video : " + objects[index].getVideoName() + " Returned ");
            } else {
                System.out.print("Video is not checked out YET");
            }
        }
        System.out.println();
    }

    //listOfInventory shopkeeper have
    public void listInventory() {
        //%-20s ensures that each string is left-aligned and takes up 20 characters of space.
        //The printf() method is used to format the output, aligning the columns consistently.
        System.out.println("-----------------------------------------------------------------------------");
        System.out.printf(" %-20s |  %-20s |  %-20s |\n", "Video Name", "Check Out Status", "Video Rating");
        System.out.println("-----------------------------------------------------------------------------");
        for (int i = 0; i < objCount; i++) {
            System.out.printf(" %-20s |  %-20s |  %-20s |\n",
                    objects[i].getVideoName(),
                    objects[i].getCheckOut(),
                    objects[i].getRating());
        }
        System.out.println("-----------------------------------------------------------------------------");
    }

    public int searchVideo(String videoName) {
        int ind = -1;
        for (int i = 0; i < objCount; i++) {
            if (objects[i].getVideoName().equals(videoName)) {
                ind = i;
                break;
            }
        }
        return ind;
    }
}

public class videoLauncher {
    public static void main(String[] args) {
        //Use choice variable for selecting choices
        Scanner sc = new Scanner(System.in);
        videoStore v = new videoStore();

        boolean loop = true;
        while (loop) {
            System.out.println("MAIN MANU");
            System.out.println("=========");
            System.out.println("1. Add Videos \n2. Check Out Video \n3. Return Video \n4. Receive Rating " +
                    "\n5. List Inventory  \n6. Exit");
            System.out.println("Enter you choice 1..6");
            int choices = sc.nextInt();
            sc.nextLine();

            switch (choices) {
                case 1:
                    System.out.println("Enter movie name want to add");
                    String movieName = sc.nextLine();
                    v.addVideo(movieName);
                    break;
                case 2:
                    System.out.println("Check out this Video : ");
                    System.out.println("Enter video Name");
                    String videoName = sc.nextLine();
                    v.doCheckOut(videoName);
                    break;
                case 3:
                    System.out.println("Enter the video name you want to Return Video : ");
                    String vName = sc.nextLine();
                    v.doReturn(vName);
                    break;
                case 4:
                    System.out.println("Enter the Video name you want to rate out of 10 : ");
                    String vname = sc.nextLine();
                    System.out.println("Enter the rating of this video : ");
                    int rating = sc.nextInt();
                    v.receiveRating(vname, rating);
                    break;
                case 5:
                    v.listInventory();
                    break;
                default:
                    System.out.println("Feels happy to help ");
                    loop = false;
                    break;
            }
            System.out.println();
        }
    }
}

//format text = ctrl+alt+L
  1. Why do I avoid OOP?

I chose to avoid Object-Oriented Programming (OOP) in this project because I found that the problem could be efficiently solved using a data structure-driven approach. By focusing on custom data structures, I was able to streamline the logic and avoid unnecessary abstraction. This allowed me to manage the video inventory, checkout status, and rating system in a direct and efficient manner, ensuring the project remained lightweight and easy to understand.

It's always better to first understand the system before diving into its implementation. A clear understanding of the requirements and functionalities can lead to more effective design choices and ultimately result in a more efficient solution.

  1. Welcome to My GitHub Repository!
    Check out my Video Rental Inventory System project on GitHub here.

    Feel free to explore, and I welcome your commits and contributions!

0
Subscribe to my newsletter

Read articles from Niraj Sahani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Niraj Sahani
Niraj Sahani

I’m Niraj Sahani, a passionate software developer with a keen interest in creating Android apps. My expertise includes Java programming, Data Structures and Algorithms (DSA), and MySQL. I am dedicated to building efficient and user-friendly applications, while continually expanding my knowledge of new technologies. As a quick learner and problem solver, I am eager to contribute to innovative projects and grow in the tech industry.