Film and this class contains the variables needed to hold the
preceding information. The data members in this class (as well as all of the other
classes you will define) should be declared as private.
The database of films will be stored as an object of class
FilmDB, containing an array of Film objects together
with a count of how many films are currently entered in the database. The
count variable indicates the number of films that have been
entered; initially the array is created to have a certain capacity
for film objects: the capacity is the length of the array of
Film objects. But initially every slot in the array is null, that is,
the empty Film object. At this point, the count is 0.
Each time the user enters a new film, one of these null
objects is then replaced with the user's data and count is
incremented. Each new film is inserted into the array in such a way that the
films remain alphabetically sorted by title. New entries are only inserted as
long as there is still some capacity (some null objects) remaining.
Each Film object contains within it a Director object. Each
object of the Director class has
Analagous to the FilmDB, the DirectorDB class
organizes the Director objects into an array that is alphabetically
sorted according to last name (first names break ties). This array is also
preallocated and contains a corresponding count variable.
By definition, these four classes are deeply entwined: a FilmDB
contains Films, which contain Directors, which in turn
contain their own FilmDBs. While this may sound complicated, it
requires no special work on your part. Simply define each class as you always
have: specifying variable and methods names and types as needed. The entwined
nature of this assignment's classes are summarized in the following diagram:
|
The defintitions of each class should be included together along with the
CS5App class in the CS5App.java file. For this
problem, submit your CS5App.java file under Homework 14, Problem 1.
private String title
private int year
private String rating
private double review
private Director dir public Film(String title, int year, String rating, double review,
Director dir) public String getTitle()
public Director getDirector()
public int getYear()
public String getRating()
public double getReview() public void display()
Title: Unforgiven
Director: Eastwood, Clint
Year: 1992
Rating: R
Review: 8.0
Note that to print the director's name, you'll need to access it
from the Director object's accessor methods! I'd suggest using
getFullName(). public void save()
Unforgiven
Eastwood
Clint
1992
R
8.0
private String fname
private String lname
private FilmDB filmDB public Director(String fname, String lname, int capacity)
this.filmDB = new FilmDB(capacity);
public String getFullName()
Eastwood, Clint
public String getFName()
public String getLName()
public FilmDB getFilmDB() private Film[] films
private int count
public FilmDB(int capacity) public boolean isFull()
public boolean checkForFilm(String fulltitle)
public int getCapacity() public int getCount() public void addFilm(Film f) public void displayAllFilms() public void saveAllFilms() public void displayFilmsByTitle(String titlepiece)
public void displayFilmsByYear(int year)
public void displayFilmsByRating(String rating)
public void displayFilmsByReview(double minreview) private Director[] dirs
private int count
public DirectorDB(int capacity)
public boolean isFull()
public int getCount()
public int getCapacity() public void addDirector(Director dir)
public void displayAllDirectors() public Director findDirectorByName(String lname, String
fname) String arguments for a first
and last name and searches the dirs array for an entry with that
name. Note that this search should not be case-sensitive -- again, I'd
advise using the "full name," i.e., lname+", "+fname, which is
easy to compare to the Strings returned from getFullName. If there is
a match, the corresponding Director object should be returned. If
there is no match, null should be returned -- the general-purpose
empty object. private static FilmDB F;
private static DirectorDB D; public static void printMenu()
0 Display All Directors
1 Display All Films
2 Display Films by Title
3 Display Films by Director
4 Display Films by Year
5 Display Films by Rating
6 Display Films by Review
7 Add a New Film
8 Save database to file
9 Load database from file
42 Quit
public static void saveDB(String fileNamePart)
public static void saveDB(String fileNamePart)
{
String fullFileName = "../" + fileNamePart + ".txt";
H.pl("Saving the database's contents to the file " + fullFileName);
H.outputToFile(fullFileName); // redirect the output to a file
H.pl(F.getCapacity()); // print the capacity (length of the array)
H.pl(F.getCount()); // print the count (# of actual films)
F.saveAllFilms(); // print all film data
H.outputToConsole(); // redirect subsequent output to the console
}
The above code is a working version of saveDB -- be sure to use it!public static void readDB(String fileNamePart)
public static void readDB(String fileNamePart)
{
String fullFileName = "../" + fileNamePart + ".txt";
H.pl("Loading database contents from file " + fullFileName;
H.inputFromFile(fullFileName); // redirect input to come from the file
int capacity = H.ni(); // reads in the capacity, an int
F = new FilmDB(capacity); // trashes the old F and creates a new one
D = new DirectorDB(capacity); // trashes the old D and creates a new one
int count = H.ni(); // reads in the count, an int
for(int i = 0; i < count; i++) // for each subsequent film...
{
readFilmEntry(); // read that film's entry
}
H.inputFromConsole(); // take subsequent input from the console
}
public static void readFilmEntry()
G, PG, PG-13, R, NC-17, UR
and it should also check that the review score is between 0 and 10.0 inclusive.
These checks will not disturb the process of reading from a file -- as long
as the file contains only valid information!
FilmDB. If not, an error is printed and the method should return.
FilmDB (i.e. an exact
match on the title is found), another message is printed and the method
should return.
Director object,
the user then enters the rest of the film's information: year, MPAA rating (G,
PG, PG-13, R, NC-17, UR), and review (0-10). Make sure these are valid entries!
Film object is created
and added to the film database. The film also needs to be added to the
director's personal film database! You can obtain this by calling
getFilmDB() on the director object that you have either newly created or
obtained from the findDirectorByName method.
public static void main(String[] args) main method drives the rest of the
program and runs the user interface. This interface is similar in structure to
those you have built in previous assignments.
The main method should begin with the lines
Then, it should present the user with the above menu of options and
handle the option entered.
F = new Film[100]; // creates a 100-film database by default
D = new Director[100]; // creates a 100-director database by default
The handling of choices 0 through 6 are straightforward. The user is first
prompted for an appropriate search value (options 0 and 1 don't even
need a search value...). The appropriate method is called from either database F
or D to print the desired information. Here are some details:
main should check that the rating or review
that is to be searched for is legitimate (i.e. between 0.0 and 10.0 or one of
the 6 allowed MPAA ratings). DirectorDB for that
name. If a match is found, the contents of the director's FilmDB
is printed; otherwise, a message reporting that the director was not found
should be printed. String
methods will be useful in performing the String-based searches
required in this assignment
toLowerCase: useful when making case-insensitive comparisons.
compareTo: useful when performing alphabetic sorting.
indexOf: useful for determining if one string is a substring
of another. Here are some examples of how each of these methods can be used: String s = "Hamburger";
String t = "Urge";
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
s.indexOf(t) -- returns the location of t within s or -1 if not there
if (s.indexOf(t) >= 0)
H.out.println("s contains t");
else
H.out.println("s does not contain t");
In this case, because "Urge" is capitalized, s.indexOf(t) would be -1
and the latter meddage would be printed out.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
s.toLowerCase() -- returns a string identical to s, but all lower case
if (s.toLowerCase().indexOf(t.toLowerCase()))
H.out.println("s contains t (ignoring case)");
else
H.out.println("s does not contain t (ignoring case)");
In this case, the former message would get printed.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
s.compareTo(t) -- returns 0 if s and t are identical strings
returns a number less than 0 if s is before t alphabetically
returns a number greater than 0 if s is after t alphabetically
if (s.compareTo(t) < 0)
H.out.println("s is before t alphabetically");
else if (s.compareTo(t) > 0)
H.out.println("s is after t alphabetically");
else
H.out.println("s and t are identical strings");
In this case, the first message would be printed out.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
s.compareToIgnoreCase(t) -- returns 0 if s and t are identical strings
returns a number less than 0 if s is before t (ignoring case)
returns a number greater than 0 if s is after t (ignoring case)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Extra Credit Extra credit on this assignment involves
replacing the methods that use for loops with ones that use recursion! We will
look at a couple of these in class, and the same idea can be applied to
the other methods that contain for loops... . Keep in mind
that you don't have to use recursion at all -- this is simply
if you'd like to try.
Welcome the the CS 5 Movie Database.
There are 0 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 9
Enter a file name: twofilms
Loading database contents from file ../twofilms.txt
100
2
Enter the title: Lord of the Rings: Return of
Enter the director's last name: Jackson
Enter the director's first name: Peter
Enter the movie's year: 2003
Enter the movie's MPAA rating: PG-13
Enter a review score: 9.0
Enter the title: Gigli
Enter the director's last name: Brest
Enter the director's first name: Martin
Enter the movie's year: 2003
Enter the movie's MPAA rating: R
Enter a review score: 2.2
There are 2 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 0
Director #0 is Brest, Martin
Director #1 is Jackson, Peter
There are 2 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 7
Enter the title: King Kong
Enter the director's last name: Jackson
Enter the director's first name: Peter
Enter the movie's year: 2005
Enter the movie's MPAA rating: PG-13
Enter a review score: 6.0
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 1
Title: Gigli
Director: Brest, Martin
Year: 2003
Rating: R
Review: 2.2
Title: King Kong
Director: Jackson, Peter
Year: 2005
Rating: PG-13
Review: 6.0
Title: Lord of the Rings: Return of the King
Director: Jackson, Peter
Year: 2003
Rating: PG-13
Review: 9.0
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 2
Enter a titlepiece: king
Title: King Kong
Director: Jackson, Peter
Year: 2005
Rating: PG-13
Review: 6.0
Title: Lord of the Rings: Return of the King
Director: Jackson, Peter
Year: 2003
Rating: PG-13
Review: 9.0
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 3
Enter last name: Brest
Enter first name: Martin
Title: Gigli
Director: Brest, Martin
Year: 2003
Rating: R
Review: 2.2
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 8
Enter a file name: threefilms
Saving the database's contents to the file ../threefilms.txt
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 5
Enter a rating: 8.5
There are 3 films in our database (max: 100)
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to file
9 Read database from file
42 Quit
Which would you like? 42
Until next time, the gallery is closed.