Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BLUEJ QUESION: The Movie class represents an individual movie. It stores the mov

ID: 3590564 • Letter: B

Question

BLUEJ QUESION:

The Movie class represents an individual movie. It stores the movie’s title: title, cost: cost, number in stock: inStock and total times it has been rented: totalRentals. Write the Movie class according to the following requirements.

Add the four (4) fields that are used by the Movie class and create a constructor that takes two (2) parameters. The first parameter is a String that is used to initialize the title of the movie. The second parameter is an integer that is used to initialize the cost of the movie. The number in stock is initialized to a random number between 5 and 15. The total times the movie has been rented is initialized to zero (0). All fields must be declared private.

Write a mutator method that rents a movie. This method takes one parameter of type String that is the title of a movie. If the movie title passed in to this method and title are the same and the movie is in stock, then increment the total times this movie has been rented, decrement the number of this movie in stock and return true. Otherwise, return false.

Write an accessor method that returns the cost of this movie.

Write an accessor method that returns the total number of times this movie has been rented.

Write a method that prints the details of this class. This method should print the following: the title, the cost, the total number in stock and the total number rented. This output should be formatted as example 1 (on last page).

The MovieStore class represents a movie rental store that rents movies. It stores three (3) Movie objects.

Add the three (3) fields that are used by the MovieStore class and create a constructor that initializes the three (3) fields with the values: field one (“Batman”, 3), field two (“Clueless”, 5), and field three (“Serenity”, 4).

Write a mutator method that rents a movie. This method takes one (1) parameter of type String that is the title of the movie that is to be rented.  

If the title passed in to the method is a movie that we have in stock print the following message: “You have rented: title for cost.” (title  Is the name of the movie and cost  is how much it costs to rent the movie)

Otherwise print the following message: “Sorry, we do not have title in stock.” (title is the name of the movie that is trying to be rented)

Write a method that shows the details of the movies in stock and how much we have made in rentals. You will show:  the titles of the movies, the cost to rent of each movie, the number of each movie in stock, the number of times each movie has been rented and the total amount made from renting all movies. This output should be formatted as example 2 (on last page).

Example 1 (output format for 1e.)

---------------------

Title:Batman

Cost:3

In Stock:9

Rented:1

---------------------

Example 2 (output format for 2c.)

Store Inventory Sheet

---------------------

Title:Batman

Cost:3

In Stock:9

Rented:1

---------------------

---------------------

Title:Clueless

Cost:5

In Stock:12

Rented:0

---------------------

---------------------

Title:Serenity

Cost:4

In Stock:10

Rented:0

---------------------

Total revenues for movie rentals were: $3  

Explanation / Answer

/*This class creates a Movie object with title , cost , inStock , totalRentals and has the required setters and getters
* The toString() method displays the details of the movie object in this class as Example 1 (output format for 1e.)
* */
public class Movie {

   /*Private data members for Movie*/
   private String title;
   private int cost;
   private int inStock;
   private int totalRentals;
   /*Constructor for creating Movies*/
   public Movie(String title,int cost) {
       setTitle(title);
       setCost(cost);
       setInStock(1 + (int)(Math.random() * 11) + 4);//Generates number from 5 to 15 randomly
       setTotalRentals(0);
   }
   /*Setters and getters for private data members*/
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public int getCost() {
       return cost;
   }
   public void setCost(int cost) {
       this.cost = cost;
   }
   public int getInStock() {
       return inStock;
   }
   public void setInStock(int inStock) {
       this.inStock = inStock;
   }
   public int getTotalRentals() {
       return totalRentals;
   }
   public void setTotalRentals(int totalRentals) {
       this.totalRentals = totalRentals;
   }
   /*Method to rent a movie if a movie is in stock*/
   public boolean rentMovie(String title) {
       if(title.equalsIgnoreCase(getTitle()) && getInStock()>0) {
           setTotalRentals(getTotalRentals()+1);//Increment the total rentals
           setInStock(getInStock()-1);//Decrement the stocks
           return true;
       }
       return false;
   }
   /*Method to print the movies with the details*/
   @Override
   public String toString() {
       return "--------------------- Title:" + title + " Cost:" + cost + " In Stock:" + inStock + " Rented:" + totalRentals
               +" --------------------- ";
   }
}

********************************************************************************************

/*This class stores 3 Movie objects and has the logic for renting the book and displaying the details of books in stock along with the revenue earned
* display() method displays as : Example 2 (output format for 2c.) Store Inventory Sheet
* */
public class MovieStore {

   //An array to store 3 movie objects
   private Movie moviesInStock[] = new Movie[3];
   private int revenues;//Data member to store Revenues

   public MovieStore() {
       //Creating 3 movie objects
       Movie movie1 = new Movie("Batman", 3);
       Movie movie2 = new Movie("Clueless", 5);
       Movie movie3 = new Movie("Serenity", 4);
       //Storing Movie objects in array
       moviesInStock[0] = movie1;
       moviesInStock[1] = movie2;
       moviesInStock[2] = movie3;
       //Initial revenue is 0
       setRevenues(0);

   }
   /*Method to rent a movie as requested by the user*/
   public void rentMovie(String title) {
       boolean success = false;
       for (int i = 0; i < moviesInStock.length; i++) {
           if (moviesInStock[i].getTitle().equalsIgnoreCase(title)) {//Checking if the movie is in stock
               success=true;
               boolean result = moviesInStock[i].rentMovie(title);
               if (result == true) {
                   System.out.println("You have rented: " + title + " for $" + moviesInStock[i].getCost());
                   setRevenues(getRevenues() + moviesInStock[i].getCost());//Increasing the revenues , adding a new revenue from the movie object
               } else {
                   System.out.println("Sorry, we do not have " + title + "in stock.");
               }
           }
       }
       if(success==false) {
           System.out.println("Sorry, we do not have " + title + "in stock.");
       }
   }
   /*Setter and Getter for revenues*/
   public int getRevenues() {
       return revenues;
   }

   public void setRevenues(int revenues) {
       this.revenues = revenues;
   }
   /*Displaying all the movies with the details along with the revenue */
   public void display() {
       for(Movie movie : moviesInStock) {
           System.out.println(movie);
       }
       System.out.println("Total revenues for movie rentals were: $"+getRevenues());
   }
}

******************************************************************************************

/*This class is the driver class that has the main method. It calls the methods of the MovieStore Class*/
public class MovieStoreDemo {

   public static void main(String[] args) {
       MovieStore movieStore = new MovieStore();
      
       movieStore.rentMovie("clueless");
       movieStore.rentMovie("batman");
       movieStore.rentMovie("hooman");
       movieStore.rentMovie("clueless");
       movieStore.rentMovie("spiderman");
       movieStore.display();
   }
}

******************************************************************************************
NOTE:
Run the MovieStoreDemo class which has the main method to see the output.
If a different class is not required, copy and paste the main method from the MovieStoreDemo class to MovieStore class.
Both will give the same results.
******************************************************************************************