Hi, I had posted this question before and I did appreciate the response I got. However I still need some little help as:
1.In the addMovieGoers method check that every member of the collection is a human and that they are old enough to watch the movie, if not throw an exception. Note that although the family has getMovieGoers method, you can't be sure it was used to build the collection.
2.Finally add another static method to the cinema class that demonstrates the usage of the cinema class. Include multiple scenarios to show how the exceptions work.-you did not include multiple scenarios
How Do I achieve the two conditions above?
the code is as:
//Cinema class
import java.util.*;
public class Cinema
{
float cost;
String Title;
//Movie class
private static Movie movieNew = new Movie();
private static ArrayList<Human> hm = new ArrayList();
public Cinema()
{
}
public Cinema (String mtitle, float pounds)
{
this.Title=mtitle;
this.cost = pounds;
}
public float getcost (int people)
{
return cost * people;
}
/**
* Method check that the movie title and rating have been set,
* if not throw MoviePropertiesNotSetException.
*/
public void setMovie(Movie movieNew) throws MoviePropertiesNotSetException
{
if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
{
System.out.println("The movie is called " +movieNew.getTitle()+
" and is rated " +movieNew.getRating()+ ".");
}
else
{
throw new MoviePropertiesNotSetException("The Movie properties not set");
}
}
/**
* Method check that the movie title and rating have been set,
* if not throw MoviePropertiesNotSetException.
*/
public void showMovie() throws MoviePropertiesNotSetException
{
if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null))
{
System.out.println("Showing... "+movieNew.getTitle());
}
else
{
throw new MoviePropertiesNotSetException ("The movie properties are not set.");
}
}
/**Checks if Movie properties are set, if not throws MyException
* takes a collection (ArrayList) of Human - Adult and Child
* and checks whether they're old enough to watch a movie using
* the canWatchMovie() method in Adult and Child class.
*/
public void addMovieGoers(ArrayList<Human> hm) throws MoviePropertiesNotSetException,IllegalAgeException
{
if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
{
String mRt = movieNew.getRating();
/**
* Movie rated PG
*/
if(movieNew.getRating().equalsIgnoreCase("pg"))
{
System.out.println("Movie Goers are::");
for(int i = 0; i < hm.size(); i++)
{
Human test = (Human)hm.get(i);
if ( test.canWatchMovie(mRt))
{
System.out.println(test);
}
else
{
throw new IllegalAgeException("The rest are of Illegal Age!");
}
}
}
/**
* Movie rated A
*/
else if(movieNew.getRating().equalsIgnoreCase("a"))
{
System.out.println("Movie goers are: ");
for ( int i = 0; i < hm.size(); i++)
{
Human test = (Human)hm.get(i);
if ( test.canWatchMovie(mRt))
{
System.out.println(test);
}
else
{
throw new IllegalAgeException("The rest are of Illegal Age!");
}
}
}
/**
* Movie rated G
*/
else if(movieNew.getRating().equalsIgnoreCase("g"))
{
System.out.println("Movie goers are: ");
for ( int i = 0; i < hm.size(); i++)
{
Human test = (Human)hm.get(i);
if ( test.canWatchMovie(mRt))
{
System.out.println(test);
}
else
{
System.out.println("The rest are below required age.");
}
}
}
/**
* Throw Exception should the movie properties be missing
*/
else
{
throw new MoviePropertiesNotSetException("The movie properties are not set");
}
}
}
public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException
{
Cinema cn = new Cinema();
try
{
Human a = new Adult("Joe",43);
Human b = new Adult("Sue",39);
Human c = new Adult("Tracy",20);
Human d = new Child("Sammy",17);
Human e = new Child("Julie",12);
Human f = new Child("Mona",4);
hm.add(a);hm.add(b);hm.add(c);hm.add(d);
hm.add(e);hm.add(f);
hm.trimToSize();
//setting properties to the movie
movieNew.setName("Comedy");
movieNew.setRating("G");
movieNew.setTitle("Mr Bin");
cn.setMovie(movieNew);
cn.showMovie();
cn.addMovieGoers(hm);
}
catch(MoviePropertiesNotSetException exp)
{
System.out.println(exp.getMessage());
}
catch(IllegalAgeException msg)
{
System.out.println(msg.getMessage());
}
finally
{
System.out.println("Movie is over! Have a good day.");
movieNew=null;
cn=null;
}
}
}