Movie Store – Part 5
In this workbook, you will add interactivity to the application.
Task 1
Inside Main
, make the Store
object a class
variable.
Task 2
Inside Main
, define a function named loadMovies
.
/**
* Name: loadMovies <--------
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function:
* - 1. loads movies from <fileName>.txt.
* - 2. adds all movies to the store object's movie field.
* Hint: You will need to 'split' a String into three.
*/
The function receives the fileName
from which you will load movies.
/**
* Name: loadMovies
* @param fileName (String) <-------
* @throws FileNotFoundException
*
* Inside the function:
* - 1. loads movies from <fileName>.txt.
* - 2. adds all movies to the store object's movie field.
* Hint: You will need to 'split' a String into three.
*/
The function throws a FileNotFoundException
.
/**
* Name: loadMovies
* @param fileName (String)
* @throws FileNotFoundException <--------
*
* Inside the function:
* - 1. loads movies from <fileName>.txt.
* - 2. adds all movies to the store object's movie field.
* Hint: You will need to 'split' a String into three.
*/
Follow the steps to complete the function. Note, you can't use scan.next()
because it picks up the next String
separated by white space. In this case, the separator is two hyphens --
.
You'll need to:
-
Pick up the entire line as one
String
(nextLine
). -
Research how to '
split
' aString
into mutipleStrings
. -
Research how to parse a
Double
from aString
(for therating
).
/**
* Name: loadMovies
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function: <------------
* - 1. loads movies from <fileName>.txt.
* - 2. adds all movies to the store object's movie field.
* Hint: You will need to 'split' a String into three Strings.
*/
From main()
, try
to call loadMovies
. Then, print:
"MOVIES LOADED\n\n"
- the movie object.
In case of a failure, catch the FileNotFoundException
.
Expected Output:
Task 3 -- manageMovies
Inside Main
, add a method called manageMovies
.
/**
* Name: manageMovies <--------
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) purchase or b) rent c) return.
* - case a: ask for the name and sell them the movie.
* - case b: ask for the name and rent them the movie.
* - case c: ask for the name and return the movie.
* - 3. call close() from the Scanner object.
*/
Create a Scanner
object.
/**
* Name: manageMovies
* Inside the function:
* - 1. Starts a new instance of Scanner; <-------
* - 2. In an infinite loop, the user can choose to a) purchase or b) rent c) return.
* - case a: ask for the name and sell them the movie.
* - case b: ask for the name and rent them the movie.
* - case c: ask for the name and return the movie.
* - 3. call close() from the Scanner object.
*/
-
Make a
while
loop that runs infinitely. -
println
:\nWould you like to \n\ta) purchase\n\tb) rent \n\tc) return.
/**
* Name: manageMovies
* Inside the function:
* - 1. Starts a new instance of Scanner;
* - 2. In an infinite loop, the user can choose to a) purchase or b) rent c) return. <----------
* - case a: ask for the name and sell them the movie.
* - case b: ask for the name and rent them the movie.
* - case c: ask for the name and return the movie.
* - 3. call close() from the Scanner object.
*/
After performing one of the actions, println
"\n\nUPDATED MOVIES\n\n"
followed by the updated list of movies. Finally, call manageContacts
from from inside the try
block.
Task 4: The finally
block
finally
gets called after thetry - catch
block.
Add a finally block at the end of the try - catch block
finally {
//gets called last (after try - catch)
}
This is a good place for cleanup code. In this case, feel free to print: "Process Complete".