Problem. Write a simple program to keep track of videos being borrowed from a video store. Detailed instructions are given below. The task is intended to ensure that you have a good grounding in the basic use of objects in Java.

Program. The program allows customers to list the videos available and to borrow them (ignore returning videos). It records whether a video is on the shelf or on loan, and if it is on loan, the date it is due to be returned. Customers can borrow up to two videos for 3 days. Build the program according to the following recipe:

  • There should be classes describing customers and videos and also a class called VideoStore that contains ArrayLists of customers and videos. There should also be a small public class VideoTest that contains the main() method.
  • The Video class has instance fields for the video title, whether it is on loan and the date it is due to be returned. There are get and set methods for some of these fields and a toString() method.
  • The Customer class has instance fields for the customer name and for the number of videos he/she is borrowing. A method borrowVideo(Video v) checks that the video is available and that the customer is not borrowing more than 2 videos, it then sets the duedate and onloan fields in the Video class. The due date is printed on screen. There is a get method for the customer name.
  • The constructor for the VideoStore class initialises a small number (less than 10 each) of Customer and Video objects and stores them in two separate ArrayLists that are the instance fields for the class. It provides a method listallVideos() for listing all the videos in the store and a method called borrow(String,String) described below.
  • The main method in VideoTest creates a VideoStore, then enters a loop that implements a menu: "Menu: L‐List, B‐Borrow, Q‐Quit". A Scanner picks up the letter entered and causes the desired response. List, lists the titles of all the videos along with whether they are on the shelf or the due date if they are on loan. Quit, drops out of the loop and exits the application. Borrow, asks first the Customer's name, and then the title of the video he/she wishes to borrow and picks up responses with a Scanner. These are then passed to the VideoStore borrow method as borrow(name,title).
  • The VideoStore borrow method checks that the name and title match entries in the VideoStore arrays, and identifies the Customer and Video objects they correspond to. The Customer borrowVideo(Video v) method can then be called.

Hints. The Video and Customer classes are very similar to the Employee class considered in lecture – identify the instance fields and implement these. Leave the toString() and borrowVideo() methods to implement later. For the VideoTest class use a Scanner in a loop. You must test the input against strings “L”,”B” or “Q”. Make sure that you create an instance of the VideoStore in VideoTest – you will only do this once. The VideoStore class does not look quite so similar to the templates you have seen – it may help to think of it as the database of customers and videos (objects) that is held by the shop – the VideoTest class is just the user interface to that database and the Video and Customer classes are like fancy data structures.

Examples of Program Extensions

  • The store rents CDs and games as well as videos. Use inheritance to implement this – for example, CDs have an instance field specifying the number of tracks.
  • The titles of the videos are read in from a file when initialising the VideoStore class.
  • Put a Customer field in the video class to identify who has borrowed the video. The name may be printed when videos are listed.
  • The Customer class can contain references to the videos that he/she is borrowing. The menu system is modified to allow customers to list just the videos that they themselves have on loan.
  • Error conditions and Exceptions, especially arising from misuse of the menus should be treated.
  • Without altering any other classes, change the VideoStore class to use HashMaps rather than ArrayLists – this simplifies the search considerably.

suggested code for the video test class

import java.util.*; import java.text.*; public class VideoTest { public static void main(String[] args) { VideoStore vs = new VideoStore(); Scanner sc = new Scanner(System.in); String menuResponse; do { System.out.println("Menu: L-list, B-borrow, Q-quit"); menuResponse = sc.nextLine(); if(menuResponse.equals("L")) vs.listallvideos(); else if (menuResponse.equals("B")) { System.out.println("What is your name?"); String nameResponse = sc.nextLine(); System.out.println("What is the title of the video you want to borrow?"); String videoResponse = sc.nextLine(); vs.borrow(nameResponse,videoResponse); } } while(!menuResponse.equalsIgnoreCase("Q")); } }
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.