I need to write a JAVA program named MovieRental that has 4 classes ( Movie, Ren
ID: 3817266 • Letter: I
Question
I need to write a JAVA program named MovieRental that has 4 classes ( Movie, Rental, Customer and main) .
Program allows user to choose which movies a customer rented and for how long
Rental charges are calculated depending on how long the movie is rented and the movie type
There are 2 kinds of movies: regular, new release. Regular movies cost is $1per day and New Releases are $2 per day.
The Customer class should have a statement method.
The Movie class should have the title, pricing structure (based on the movie type) .
The Rental class should have at least the number of days rented.
The output should look like this .
Please enter customer name:
Tim
Please select movie type:
(1) New releases
(2) Regular movies
1
1-New Releases
Please select the movie name (1-3):
(1)Ghost in the Shell
(2)Beauty And The Beast
(3)The Fate Of The Furious
1
Please enter number of rental days :
2
Do you want to add another movie? (y/n)
y
Please select movie type:
(1)New releases
(2)Regular movies
2
2 – Regular movies
Please select the movie name (1-3):
(1)Matrix
(2)The Godfather
(3)Iron Man
3
Please enter number of rental days :
1
Do you want to add another movie? (y/n)
n
-----------------------------------------------------------------------------------------------------------------------------------
BILLING INVOICE:
Movie type : New Releases | Movie name: Ghost in the Shell | Movie Rental Cost: $2 | Number of days: 2| Cost: $4
Movie type : Regular movies | Movie name: Iron man | Movie Rental Cost: $1 | Number of days: 1| Cost: $1
TOTAL RENTAL COST: $5
--------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
//Customer.java
/**
* The java program Customer that prompts user to select movie
* type and name of the name, number of days rented then creates
* a movie object and prompt user to continue until user enters
* n to stop and print invoice of the customer.
* */
//Customer.java
import java.util.ArrayList;
import java.util.Scanner;
public class Customer {
public static void main(String[] args) {
//create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
//new movies array
String newmovies[]={"Ghost in the Shell",
"Beauty And The Beast",
"The Fate Of The Furious"};
//regular movies array
String regularmoives[]={"Matrix",
"The Godfather",
"Iron Man"};
double totalcost=0;
ArrayList<Movie> movies=new ArrayList<>();
boolean repeat=true;
System.out.println("Please enter customer name:");
String name=scanner.nextLine();
while(repeat)
{
System.out.println("Please select movie type:");
System.out.println("(1) New releases");
System.out.println("(2) Regular movies");
int movieType=Integer.parseInt(scanner.nextLine());
if(movieType==1)
{
System.out.println("1-New Releases");
System.out.println("Please select the movie name (1-3):");
System.out.println("(1)Ghost in the Shell");
System.out.println("(2)Beauty And The Beast");
System.out.println("(3)The Fate Of The Furious");
}
else if(movieType==2)
{
System.out.println("(2) Regular movies");
System.out.println("Please select the movie name (1-3):");
System.out.println("(1)Matrix");
System.out.println("(2)The Godfather");
System.out.println("(3)Iron Man");
}
int movieChoice=Integer.parseInt(scanner.nextLine());
System.out.println("Please enter number of rental days :");
int rentdays=Integer.parseInt(scanner.nextLine());
if(movieType==1)
//create a movie object and add to movies arraylist
movies.add(new Movie(newmovies[movieChoice-1], movieType, 2, rentdays));
else if(movieType==2)
//create a movie object and add to movies arraylist
movies.add(new Movie(regularmoives[movieChoice-1], movieType, 1, rentdays));
System.out.println("Do you want to add another movie? (y/n)");
String anotherMovie=scanner.nextLine();
if(anotherMovie.equals("n"))
repeat=false;
}
System.out.println("BILLING INVOICE:");
//print to console
for (Movie movie : movies) {
totalcost+=movie.getCost();
System.out.println(movie);
}
//print total cost
System.out.println("TOTAL RENTAL COST: $"+totalcost);
}
}
----------------------------------------------------------------------------------------------------------------------------------------
/**
* Movie that extends Rental class
* and sets title,type,cost and days
* */
//Movie.java
public class Movie extends Rental{
private String title;
private int type;
//constructor
public Movie(String title, int type,double cost,int days) {
super(cost,days);
this.title=title;
this.type=type;
}
//Set movieType based on type value
public String getmovieType(){
String movieType="";
if(type==1)
movieType="New Releases";
else if(type==2)
movieType="Regular movies";
return movieType;
}
//calculate cost
public double getCost()
{
//calling super class getCost
return super.getCost();
}
//Returns the string representaion of Movie object
public String toString() {
String movieDesc="";
movieDesc+="Movie type : "+getmovieType()+
"| Movie name: "+title+super.toString();
return movieDesc;
}
}
----------------------------------------------------------------------------------------------------------------------------------------
//Rental.java
public class Rental {
private double cost;
private int days;
//constructor that takes cost and days
public Rental(double cost,int days) {
this.cost=cost;
this.days=days;
}
//returns the cost of the rent
public double getCost()
{
return cost*days;
}
//Returns the string representation of Rental
public String toString() {
return "| Movie Rental Cost:$"+cost+
"| Number of days: "+days+
"| Cost: $"+getCost();
}
}
----------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Please enter customer name:
Tim
Please select movie type:
(1) New releases
(2) Regular movies
1
1-New Releases
Please select the movie name (1-3):
(1)Ghost in the Shell
(2)Beauty And The Beast
(3)The Fate Of The Furious
1
Please enter number of rental days :
2
Do you want to add another movie? (y/n)
y
Please select movie type:
(1) New releases
(2) Regular movies
2
(2) Regular movies
Please select the movie name (1-3):
(1)Matrix
(2)The Godfather
(3)Iron Man
3
Please enter number of rental days :
1
Do you want to add another movie? (y/n)
n
BILLING INVOICE:
Movie type : New Releases| Movie name: Ghost in the Shell| Movie Rental Cost:$2.0| Number of days: 2| Cost: $4.0
Movie type : Regular movies| Movie name: Iron Man| Movie Rental Cost:$1.0| Number of days: 1| Cost: $1.0
TOTAL RENTAL COST: $5.0