public class ValidMovie implements Comparable<ValidMovie>, java.io.Serializable
{
// Constants to validate the year and rating limits
// Of course, we don't want this data serialized, so make them transient
public static transient final int YEAR_MIN = 1900;
public static transient final int YEAR_MAX = 2005;
public static transient final double RATING_MIN = 0.0;
public static transient final double RATING_MAX = 4.0;
// Class members
private String movieTitle;
private String movieFormat;
private int movieYear;
private double movieRating;
/**
* This function will try to create a ValidMovie object on unvalidated data
* but will also do the validation. throws an error if it fails.
*/
public ValidMovie(String sTitle, String sYear, String sRating, String sFormat) throws MovieException
{
int year = 0;
double rating = 0.0;
// Validate year
try
{
year = Integer.parseInt(sYear);
}
catch (NumberFormatException e)
{
throw new NonNumericYearException(sYear);
}
// Validate rating
try
{
rating = Double.parseDouble(sRating);
}
catch (NumberFormatException e)
{
throw new NonNumericRangeException(sRating);
}
// Data is in the proper format as far as types go
// Call the set methods to do extra validation
setTitle(sTitle);
setYear(year);
setRating(rating);
setFormat(sFormat);
}
/**
* Out of convenience sake, there should be a way to create a new ValidMovie object
* without having to convert alread-valid-integers to Strings just to be converted back
* to integers again (and the same for doubles). This is it:
*/
public ValidMovie(String title, int year, double rating, String format) throws MovieException
{
setTitle(title);
setYear(year);
setRating(rating);
setFormat(format);
}
public void setTitle(String title)
{
movieTitle = title;
}
public void setYear(int year) throws InvalidYearRangeException
{
// Validate input before setting new value
if (year < YEAR_MIN || year > YEAR_MAX)
{
throw new InvalidYearRangeException(String.valueOf(year));
}
movieYear = year;
}
public void setRating(double rating) throws InvalidRatingRangeException
{
// Validate input before setting new value
if (rating < RATING_MIN || rating > RATING_MAX)
{
throw new InvalidRatingRangeException(String.valueOf(rating));
}
movieRating = rating;
}
public void setFormat(String format) throws InvalidFormatException
{
// Validate input before setting new value
if (format.toLowerCase().equals("vhs"))
{
format = "VHS";
}
else if (format.toLowerCase().equals("dvd"))
{
format = "DVD";
}
else if (format.toLowerCase().equals("imax"))
{
format = "IMAX";
}
else if (format.toLowerCase().equals("theater"))
{
format = "Theater";
}
else
{
throw new InvalidFormatException(format);
}
movieFormat = format;
}
public String getTitle()
{
return movieTitle;
}
public int getYear()
{
return movieYear;
}
public double getRating()
{
return movieRating;
}
public String getVerdict()
{
double rating = getRating();
if (rating <= 1) return "Worse than Gigli";
if (rating <= 2) return "Pretty much sucks";
if (rating <= 3) return "Almost worth the 12.95";
else return "Changed my life";
}
public String getFormat()
{
return movieFormat;
}
public int compareTo(ValidMovie c)
{
ValidMovie movie = (ValidMovie)c;
if (this.getRating() > movie.getRating()) return -1;
else if (this.getRating() < movie.getRating()) return 1;
else
{
/* This isn't required by the assignment, but since the
* assignment doesn't specify any desired order for rows with
* equal rating values it would be pretty nifty to second-order
* the movies by year, if they have the same rating value.
*/
if (this.getYear() > movie.getYear()) return -1;
else return 1;
}
}
/**
* Returns the data as found in the movies database file, so we can rewrite it
* as gzipped output
*/
public String toString()
{
return getTitle() + "\t" + getYear() + "\t" + getRating() + "\t" + getFormat();
}
} Powered by
GeSHi Syntax Highlighting software.
Author of all (other) material unless otherwise specified:
Loren Segal. Copyright 2005.