Java

A Video Library Application in Java

In this article, I will discuss a Video Library Application in Java.

The following application explains the concept of object-oriented programming in Java. It shows how to perform abstraction and encapsulation.

We design the application as follows. There are two entities: CD_Video, and VideoLibrary. Further, the CD_Video represents a Video CD. For simplicity, it has three attributes – vtitile, issued, and rating. The attribute vtitle represents the title of Video CD. Similarly, the attribute issued indicates whether the CD has been issued or not. lastly, the attribute rating contains the rating given by the user.

Likewise, the entity VideoLibrary maintains the collection of Video CDs. Both of these entities are implemented as java classes. Furthermore, the behavior of the CD_Video comprises of six methods. The following list describes these methods. Also, it contains a constructor that accepts the video title as a parameter.

  • getVideoTitle(). In order to retrieve the title of the specified video, we use this method. While, the video title is set by the constructor.
  • issueVideo(). This method sets the issued attribute to true.
  • returnVideo(). Similarly, it sets the issued attribute to false. It indicates that the video has been returned by the user.
  • setRating(). This method sets the rating value of a specific video.
  • getRating(). Similarly, it returns the rating value.
  • getIssued(). It returns the value of the issued attribute.

Likewise, the VideoLibrary class maintains an array of CD_Video objects. Also, it contains methods of adding the CD_Video objects to the array, issue a video, return a video, and display all the elements of the video collection.

The Complete Code for the Video Library Application in Java

In order to execute this application, we need to create a public class that contains a main() method. Hence, we create the class Library. Further, we create an object of the VideoLibrary class. The main() method contains a menu in a while loop that runs forever. In order to terminate the application, it has the option to exit. Whereas the other options are to collect the videos, issue a video, return a video, accept the rating value, and list all videos.

import java.util.*;
class CD_Video
{
	String vtitle;
	boolean issued;
	int rating;

	public CD_Video(String vtitle)
	{
		this.vtitle=vtitle;
	}
	public String getVideoTitle()
	{
		return vtitle;
	}
	public void issueVideo()
	{
		issued =true;
	}
	public void returnVideo()
	{
		issued=false;
	}
	public void setRating(int rating)
	{
		this.rating=rating;
	}
	public int getRating()
	{
		return rating;
	}
	public boolean getIssued()
	{
		return issued;
	}
}
class VideoLibrary
{
	CD_Video[] library;
	int index=0;
	public VideoLibrary()
	{
		library=new CD_Video[100];
	}
	public void collectVideo(String vtitle)
	{
		CD_Video v=new CD_Video(vtitle);
		library[index++]=v;
	}
	public void issueVideo(String vtitle)
	{
		boolean found=false;
		for(CD_Video t: library)
		{  if(t!=null){
			if(t.getVideoTitle().equals(vtitle))
			{
				t.issueVideo();
				System.out.println("Video "+vtitle+" has been issued!");
				found=true;
				break;
			}}
		}
		if(!found)
		{
			System.out.println("Video "+vtitle+" is not available!");
		}
	}
	public void returnVideo(String vtitle)
	{
		boolean found=false;
		for(CD_Video t: library)
		{
		   if(t!=null){
			if(t.getVideoTitle().equals(vtitle))
			{
				t.returnVideo();
				System.out.println("Video "+vtitle+" has been returned to the library!");
				found=true;
				break;
			}}
		}
		if(!found)
		{
			System.out.println("Video "+vtitle+" is not available!");
		}
	}
	public void setRating(String vtitle, int rating)
	{
		boolean found=false;
		for(CD_Video t: library)
		{  if(t!=null){
			if(t.getVideoTitle().equals(vtitle))
			{
				t.setRating(rating);
				System.out.println("Video "+vtitle+" has got the rating = "+rating);
				found=true;
				break;
			}}
		}
		if(!found)
		{
			System.out.println("Video "+vtitle+" is not available!");
		}
	}
	public void showAllVideos()
	{
		System.out.println("--------------------------------------------------------------");
		System.out.println("Video Name\t|\tIssued Status\t|\tRating");
		for(CD_Video t: library)
		{
			if(t!=null)
				System.out.println(t.getVideoTitle()+"\t\t|\t"+t.getIssued()+"\t\t|\t"+t.getRating());
		}
		System.out.println("--------------------------------------------------------------");
	}
}
public class Library
{
	VideoLibrary vs=new VideoLibrary();
	public static void main(String[] args)
	{
		Library lib=new Library();
		while(true)
		{
			System.out.println("1. Collect Videos");
			System.out.println("2. Issue Video");
			System.out.println("3. Return Video");
			System.out.println("4. Set Rating");
			System.out.println("5. Show All");
			System.out.println("6. Exit");
			System.out.println("Enter your choice (1-6): ");

			int choice=(new Scanner(System.in)).nextInt();
			switch(choice)
			{
				case 1: lib.collectVideos(); break;
				case 2: lib.issueVideo(); break;
				case 3: lib.returnVideo(); break;
				case 4: lib.setRating(); break;
				case 5: lib.showAllVideos(); break;
				case 6: System.exit(0); 
				default: System.out.println("invalid choice value!");
			}
		}
	}
	public void collectVideos()
	{
		System.out.println("Enter the name of video you want to add: ");
		String video_name=(new Scanner(System.in)).next();
		vs.collectVideo(video_name);		
	}
	public void issueVideo()
	{
		System.out.println("Enter the name of video you want to issue: ");
		String video_name=(new Scanner(System.in)).next();
		vs.issueVideo(video_name);	
	}
	public void returnVideo()
	{
		System.out.println("Enter the name of video you want to return: ");
		String video_name=(new Scanner(System.in)).next();
		vs.returnVideo(video_name);	
	}
	public void setRating()
	{
		System.out.println("Enter the name of video you want to rate: ");
		String video_name=(new Scanner(System.in)).next();
		System.out.println("Enter the rating for this video: ");
		int rating=(new Scanner(System.in)).nextInt();
		vs.setRating(video_name, rating);
	}
	public void showAllVideos()
	{
		vs.showAllVideos();
	}
}

Output

The following screenshots display the output. Further improvement in the application can be done by adding the exception handling code. So that when user provides input in incorrect format, the application is able to handle it.

  A Video Library Application in Java - Adding Videos to the Library
A Video Library Application in Java – Adding Videos to the Library

Display the List of Videos
Display the List of Videos
Issue a Video from the Library
Issue a Video from the Library
Display the Video List After the Videos have been Issued
Display the Video List After the Videos have been Issued
Set the Rating of Videos
Set the Rating of Videos
List of Videos After Setting the Rating
List of Videos After Setting the Rating
Returning a Video to the Library
Returning a Video to the Library
List of Videos After Returning
List of Videos After Returning
Exiting the Application
Exiting the Application

Further Reading

Java Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *