For a family or an individual, a favorite place to go on weekends or holidays is
ID: 3780572 • Letter: F
Question
For a family or an individual, a favorite place to go on weekends or holidays is to a DVD store to rent movies. A new DVD store in your neighborhood is about to open. However, it does not have a program to keep track of its DVDs and customers. The store managers want someone to write a program for their system so that the DVD store can function. The program should be able to perform the following operations:
Rent a DVD; that is, check out a DVD.
Return, or check in, a DVD.
Create a list of DVDs owned by the store.
Show the details of a particular DVD.
Print a list of all of the DVDs in the store.
Check whether a particular DVD is in the store.
Maintain a customer database.
Print a list of all of the DVDs rented by each customer.
Let us write a program for the DVD store. This example further illustrates the object-oriented design methodology and, in particular, inheritance and overloading.
The programming requirement tells us that the DVD store has two major components: DVDs and customers. We will describe these two components in detail. We also need to maintain the following lists:
A list of all of the DVDs in the store
A list of all of the store’s customers
Lists of the DVDs currently rented by the customers
We will develop the program in two parts. In Part 1, we design, implement, and test the DVD component. In Part 2, we design and implement the customer component, which is then added to the DVD component developed in Part 1. That is, after completing Parts 1 and 2, we can perform all of the operations listed previously.
Part 1: DVD Component
DVD Object
This is the first stage, wherein we discuss the DVD component. The common things associated with a DVD are:
Name of the movie
Names of the stars
Name of the producer
Name of the director
Name of the production company
Number of copies in the store
From this list, we see that some of the operations to be performed on a DVD object are:
Set the DVD information—that is, the title, stars, production company, and so on.
Show the details of a particular DVD.
Check the number of copies in the store.
Check out (that is, rent) the DVD. In other words, if the number of copies is greater than zero, decrement the number of copies by one.
Check in (that is, return) the DVD. To check in a DVD, first we must check whether the store owns such a DVD and, if it does, increment the number of copies by one.
Check whether a particular DVD is available—that is, check whether the number of copies currently in the store is greater than zero.
The deletion of a DVD from the DVD list requires that the list be searched for the DVD to be deleted. Thus, we need to check the title of a DVD to find out which DVD is to be deleted from the list. For simplicity, we assume that two DVDs are the same if they have the same title.
Part 2: Customer Component
Customer Object
The customer object stores information about a customer, such as the first name, last name, account number, and a list of DVDs rented by the customer.
Every customer is a person. We have already designed the class personType in Example 10-10 (Chapter 10) and described the necessary operations on the name of a person. Therefore, we can derive the class customerType from the classpersonType and add the additional members that we need. First, however, we must redefine the class personType to take advantage of the new features of object-oriented design that you have learned, such as operator overloading, and then derive the class customerType.
Recall that the basic operations on an object of type personType are:
Print the name.
Set the name.
Show the first name.
Show the last name.
Similarly, the basic operations on an object of type customerType are:
Print the name, account number, and the list of rented DVDs.
Set the name and the account number.
Rent a DVD; that is, add the rented DVD to the list.
Return a DVD; that is, delete the rented DVD from the list.
Show the account number.
The details of implementing the customer component are left as an exercise for you. (See Programming Exercise 14 at the end of this chapter.)
Main Program
We will now write the main program to test the DVD object. We assume that the necessary data for the DVDs are stored in a file. We will open the file and create the list of DVDs owned by the DVD store. The data in the input file is in the following form:
We will write a function, createDVDList, to read the data from the input file and create the list of DVDs. We will also write a function, displayMenu, to show the different choices—such as check in a movie or check out a movie—that the user can make. The algorithm of the function main is:
Open the input file.
If the input file does not exist, exit the program.
Create the list of DVDs (createDVDList).
Show the menu (displayMenu).
While not done
Perform various operations.
Opening the input file is straightforward. Let us describe Steps 2 and 3, which are accomplished by writing two separate functions: createDVDList and displayMenu.
createDVDList
This function reads the data from the input file and creates a linked list of DVDs. Because the data will be read from a file and the input file was opened in the function main, we pass the input file pointer to this function. We also pass the DVD list pointer, declared in the function main, to this function. Both parameters are reference parameters. Next, we read the data for each DVD and then insert the DVD in the list. The general algorithm is:
Read the data and store it in a DVD object.
Insert the DVD in the list.
Repeat steps a and b for each DVD’s data in the file.
displayMenu
This function informs the user what to do. It contains the following output statements:
Select one of the following:
1.
To check whether the store carries a particular DVD
2.
To check out a DVD
3.
To check in a DVD
4.
To check whether a particular DVD is in stock
5.
To print only the titles of all the DVDs
6.
To print a list of all the DVDs
9.
To exit
Explanation / Answer
PROGRAM CODE:
DVD.java
package movie;
public class DVD {
private String movieTitle;
private String movieStars[];
private String movieDirector;
private String movieProducer;
private String movieProductionCo;
private int numberOfCopies;
DVD(String title, String[] stars, String director, String producer, String production, int copies)
{
movieTitle = title;
movieStars = stars;
movieDirector = director;
movieProducer = producer;
movieProductionCo = production;
numberOfCopies = copies;
}
public String getMovieTitle() {
return movieTitle;
}
public void setMovieTitle(String movieTitle) {
this.movieTitle = movieTitle;
}
public String[] getMovieStars() {
return movieStars;
}
public void setMovieStars(String[] movieStars) {
this.movieStars = movieStars;
}
public String getMovieDirector() {
return movieDirector;
}
public void setMovieDirector(String movieDirector) {
this.movieDirector = movieDirector;
}
public String getMovieProducer() {
return movieProducer;
}
public void setMovieProducer(String movieProducer) {
this.movieProducer = movieProducer;
}
public String getMovieProductionCo() {
return movieProductionCo;
}
public void setMovieProductionCo(String movieProductionCo) {
this.movieProductionCo = movieProductionCo;
}
public int getNumberOfCopies() {
return numberOfCopies;
}
public void setNumberOfCopies(int numberOfCopies) {
this.numberOfCopies = numberOfCopies;
}
public void incrementCopies()
{
numberOfCopies++;
}
public boolean checkOut()
{
if(numberOfCopies>0)
{
numberOfCopies--;
return true;
}
else return false;
}
public void checkIn()
{
numberOfCopies++;
}
@Override
public String toString() {
return "Movie: " + movieTitle + ", Stars: " + movieStars[0] + " and " + movieStars[1] + ", Director: " + movieDirector +
", Producer: " + movieProducer + ", Production Company: "+movieProductionCo;
}
}
PersonType.java
package movie;
public class PersonType {
private String firstName;
private String lastName;
PersonType(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Customer.java
package movie;
import java.util.ArrayList;
import java.util.List;
public class Customer extends PersonType{
List<DVD> rentedDVDs;
List<Integer> rentalCount;
int accountNumber;
private static int counter = 1001;
Customer(String firstName, String lastName) {
super(firstName, lastName);
rentedDVDs = new ArrayList<DVD>();
rentalCount = new ArrayList<Integer>();
accountNumber = counter++;
}
public int getAccountNumber()
{
return accountNumber;
}
public String getName()
{
return getFirstName() + " " + getLastName();
}
public void setName(String firstName, String lastName)
{
setFirstName(firstName);
setLastName(lastName);
}
public void rentDVD(DVD dvd)
{
for(int i=0; i<rentedDVDs.size(); i++)
{
if(rentedDVDs.get(i).getMovieTitle().equals(dvd.getMovieTitle()))
{
rentalCount.set(i, rentalCount.get(i)+1);
return;
}
}
rentedDVDs.add(dvd);
rentalCount.add(1);
}
public void returnDVD(DVD dvd)
{
for(int i=0; i<rentedDVDs.size(); i++)
{
if(rentedDVDs.get(i).getMovieTitle().equals(dvd.getMovieTitle()))
{
if(rentalCount.get(i)>1)
rentalCount.set(i, rentalCount.get(i)-1);
else if(rentalCount.get(i)==1)
{
rentalCount.remove(i);
rentedDVDs.remove(i);
}
return;
}
}
System.out.println("The DVD was not rented to the customer !");
}
public int rentalCount()
{
int total = 0;
for(int i: rentalCount)
total += i;
return total;
}
@Override
public String toString() {
return "Customer Name : " + getName() + "Account Number: " + accountNumber + "Total rental Count: " + rentalCount();
}
}
DVDStore.java
package movie;
import java.util.ArrayList;
import java.util.List;
public class DVDStore {
List<DVD> listOfDVDs;
List<Customer> customers;
DVDStore()
{
listOfDVDs = new ArrayList<DVD>();
customers = new ArrayList<Customer>();
}
public void addDVD(DVD newdvd)
{
DVD dvd = searchDVD(newdvd.getMovieTitle());
if(dvd == null)
{
listOfDVDs.add(newdvd);
}
else
listOfDVDs.get(listOfDVDs.indexOf(newdvd)).incrementCopies();
}
public void addCustomer(Customer customer)
{
customers.add(customer);
}
public DVD searchDVD(String title)
{
DVD dvd = null;
for(DVD currentDVD: listOfDVDs)
{
if(currentDVD.getMovieTitle().equals(title))
{
dvd = currentDVD;
}
}
return dvd;
}
private int findCustomer(int accountNumber)
{
for(int i=0; i<customers.size(); i++)
{
if(customers.get(i).getAccountNumber() == accountNumber)
{
return i;
}
}
return -1;
}
public boolean rentDVD(String title, int accountNumber)
{
DVD dvd = searchDVD(title);
boolean rentalStatus = false;
if(dvd != null)
{
int DVDIndex = listOfDVDs.indexOf(dvd);
int CustomerIndex = findCustomer(accountNumber);
if(CustomerIndex != -1)
{
dvd.checkOut();
listOfDVDs.set(DVDIndex, dvd);
rentalStatus = true;
customers.get(CustomerIndex).rentDVD(dvd);
}
else
System.out.println("Customer not found in the database !");
}
return rentalStatus;
}
public boolean returnDVD(String title, int accountNumber)
{
DVD dvd = searchDVD(title);
boolean returnStatus = false;
if(dvd != null)
{
int DVDIndex = listOfDVDs.indexOf(dvd);
int CustomerIndex = findCustomer(accountNumber);
if(CustomerIndex != -1)
{
dvd.checkIn();
listOfDVDs.set(DVDIndex, dvd);
returnStatus = true;
customers.get(CustomerIndex).returnDVD(dvd);
}
else
System.out.println("Customer not found in the database !");
}
return returnStatus;
}
public void printAllDVDs()
{
for(DVD dvd: listOfDVDs)
{
System.out.println(dvd);
}
}
public void printMovieTitles()
{
for(DVD dvd: listOfDVDs)
{
System.out.println(dvd.getMovieTitle());
}
}
}
DVDDriver.java
package movie;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DVDDriver {
public static void menu(DVDStore store)
{
System.out.println("1. Search a DVD");
System.out.println("2. Check out a DVD");
System.out.println("3. Check in a DVD");
System.out.println("4. Check whether a DVD is in stock");
System.out.println("5. Print all titles of DVD");
System.out.println("6. Print all the DvDs");
System.out.println("9. Exit");
System.out.println("Enter your choice: ");
Scanner consoleInt = new Scanner(System.in);
Scanner consoleString = new Scanner(System.in);
int choice = consoleInt.nextInt();
int accNumber;
DVD dvd = null;
String title = "";
if(choice != 9 && choice != 5 && choice != 6)
{
System.out.println("Enter the movie title: ");
title = consoleString.nextLine();
}
switch(choice)
{
case 1:
dvd = store.searchDVD(title);
if(dvd != null)
{
System.out.println(dvd);
}
menu(store);
break;
case 2: System.out.println("Enter account number of Customer: ");
accNumber = consoleInt.nextInt();
if(store.rentDVD(title, accNumber))
System.out.println("Movie rented !");
menu(store);
break;
case 3: System.out.println("Enter account number of Customer: ");
accNumber = consoleInt.nextInt();
store.returnDVD(title, accNumber);
System.out.println("Movie returned !");
menu(store);
break;
case 4: dvd = store.searchDVD(title);
if(dvd != null)
{
if(dvd.getNumberOfCopies()>0)
System.out.println(title + " is in stock");
}
menu(store);
break;
case 5:store.printMovieTitles();
menu(store);
break;
case 6: store.printAllDVDs();
menu(store);
break;
case 9: return;
}
}
public static void main(String[] args) {
DVDStore store = new DVDStore();
try {
Scanner filereader = new Scanner(new File("movies.txt"));
while(filereader.hasNextLine())
{
String title = filereader.nextLine().replace("DVD ", "");
String star1 = filereader.nextLine().replace("movie ", "");
String star2 = filereader.nextLine().replace("movie ", "");
String producer = filereader.nextLine().replace("movie ", "");
String director = filereader.nextLine().replace("movie ", "");
String production = filereader.nextLine().replace("movie ", "");
String copies = filereader.nextLine();
store.addDVD(new DVD(title, new String[]{star1, star2}, director, producer, production, Integer.valueOf(copies)));
}
} catch (FileNotFoundException e) {
System.out.println("File format is wrong !");
e.printStackTrace();
}
System.out.println("Enter a customer first name: ");
Scanner consoleReader = new Scanner(System.in);
String fName = consoleReader.next();
System.out.println("Enter a customer last name: ");
String lname = consoleReader.next();
Customer customer = new Customer(fName, lname);
store.addCustomer(customer);
menu(store);
}
}
OUTPUT:
Enter a customer first name:
Andrew
Enter a customer last name:
Garfield
1. Search a DVD
2. Check out a DVD
3. Check in a DVD
4. Check whether a DVD is in stock
5. Print all titles of DVD
6. Print all the DvDs
9. Exit
Enter your choice:
5
City Of Angels
Lord of War
Ghost Rider
1. Search a DVD
2. Check out a DVD
3. Check in a DVD
4. Check whether a DVD is in stock
5. Print all titles of DVD
6. Print all the DvDs
9. Exit
Enter your choice:
6
Movie: City Of Angels, Stars: Nicolas Cage and Meg Ryan, Director: Brad Silberling,
Producer: Charles Roven, Production Company: Regency Enterprises
Movie: Lord of War, Stars: Nicolas Cage and Jared Leto, Director: Andrew Niccol,
Producer: Andrew Niccol, Production Company: Entertainment Manufacturing Company
Movie: Ghost Rider, Stars: Nicolas Cage and Eva Mendes, Director: Mark Steven Johnson,
Producer: Avi Arad, Production Company: Crystal Sky Pictures
1. Search a DVD
2. Check out a DVD
3. Check in a DVD
4. Check whether a DVD is in stock
5. Print all titles of DVD
6. Print all the DvDs
9. Exit
Enter your choice:
1
Enter the movie title:
City Of Angels
Movie: City Of Angels, Stars: Nicolas Cage and Meg Ryan, Director: Brad Silberling,
Producer: Charles Roven, Production Company: Regency Enterprises
1. Search a DVD
2. Check out a DVD
3. Check in a DVD
4. Check whether a DVD is in stock
5. Print all titles of DVD
6. Print all the DvDs
9. Exit
Enter your choice:
9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.