Movie Store – Part 3
In this workbook, you will apply the Big 3 steps to the Store
class
.
The store is identified by the movies that it manages. It can add movies, sell them, and allow the user to rent/return them.
Task 1 – Adding the field
Add the movies
field to the Store
class
. The number of movies can increase or decrease so use an ArrayList
.
Task 2 – The Big 3
-
Constructor.
-
It receives no parameters.
-
It sets the field equal to a new object of the
ArrayList
class.
-
-
Getter:
getMovie
-
It receives one parameter:
int index
. -
It returns an object at the requested
index
.
-
-
Setter:
setMovie
-
It receives two parameters:
int index
,Movie movie
. -
It sets an element equal to a copy of the object being passed in.
-
As you're applying each step, careful not to fall into the reference trap.
Task 3 – addMovie
.
-
This method is
void
-
Receives one parameter:
Movie movie
. -
Adds a movie to the ArrayList.
Task 4 – sellMovie
.
-
This method is
void
. -
Receives one parameter:
String movieName
. -
Removes the movie that matches the name passed in.
Task 5 – rentMovie
.
-
This method is
void
. -
Receives one parameter:
String movieName
. -
Sets
isAvailable
equal tofalse
.
Task 6 – returnMovie
.
-
This method is
void
. -
Receives one parameter:
String movieName
. -
Sets
isAvailable
equal totrue
.
Task 7 – Remove sellMovie
, rentMovie
and returnMovie
You probably noticed that rentMovie
, sellMovie
and returnMovie
have identical code. Instead of three methods, create one:
-
named
action
. -
accepts two parameters:
String name, String action
. -
runs three cases:
sell
,rent
, andreturn
.
Task 8 – toString
.
-
Set a
String
variabletemp
that equals""
. -
Loop through every movie in the
movies
field. -
During each run, add the
toString
of each movie. -
During each run, add two new lines
\n\n
to temp. -
return temp;
Task 9 – Testing the code
-
Inside
main()
, create an object of theStore class
. -
add three movies to the
movies
field. Use the following values:-
The Shawshank Redemption--Blue-Ray--9.2
-
The Godfather--Blue-Ray--9.1
-
The Godfather: Part II--DVD--9.0
-
-
Print the
Store
.
Expected Output:
- call
action
andsell
"The Godfather"
.
Expected Output:
- call
action
andrent
"The Shawshank Redemption"
Expected Output:
- call
action
andreturn
the movie"The Shawshank Redemption"
Expected Output: