Movie Store – Part 1
From Java Bootcamp Resources
-> Module 2
-> 8. Exception Handling
-> Exception Handling Workbooks
, open the folder: movie-store
.
The requirements.
Based on the requirements, there are two types of objects:
We can describe each movie using six fields: name
, format
, rating
, sellingPrice
, rentalPrice
, and isAvailable
.
The store is identified by the movies that it manages. First, it adds movies to its collection. It also sells movies and allows the user to rent/return them.
The tasks in this workbook apply to the Movie
class
.
Task 1 – Fields
Add the necessary fields to the Movie
class
. Protect each field using the private
keyword.
Task 2 – The Big 3
If a class has fields, you need to apply the Big 3:
-
Constructor
-
Getters
-
Setters
Task 2.1 – Constructor
-
The constructor will only receive three parameters:
name
,format
andrating
. -
At first,
isAvailable
is equal to a fixed value oftrue
. -
Use conditional assignment syntax (
?
and:
) to update the selling and rental price (see requirements).
Task 2.2 – Getters and Setters
You can autocomplete the getters and setters using Visual Studio Code Intellisense.
-
Note: getters for the
boolean
type are written asisFieldName
. Here are some examples:-
boolean
empty
-- getter:isEmpty
-
boolean
available
-- getter:isAvailable
-
boolean
ready
-- getter:isReady
-
boolean
isReady
-- getter:isReady
-
- This note was added to your next cheat sheet.
Task 2.3 – setSellingPrice
Make setSellingPrice
private
. The caller should not be allowed to access it. The format
should determine the sellingPrice
.
Task 2.4 – setRentalPrice
Make setRentalPrice
private
. The caller should not be allowed to access it. The format
should determine the rentalPrice
.
Task 3 – Copy Constructor
To avoid the reference trap, we need a way to copy Movie
objects. So, add a copy constructor.
Task 4 – toString
Every class that models an object should have a toString
method.
Add a toString
method to your class, and return
a String
that adheres to this format:
return "\t Name: " + <name> + "\n" +
"\t Format: " + <Format> + "\n" +
"\t Rating: " + <Rating> + "\n" +
"\t Selling Price: " + <Price> + "\n" +
"\t Rental Price: " + <Price> + "\n" +
"\t Availability: " + <in-stock if true and rented if false> + "\n";
Task 5 – Test your code.
- Inside
main()
, create anew
object of theMovie
class and print it.
Example output: