How can I use the Exceptions I have created below in the Cinema class?
//IllegalAgeException class
public class IllegalAgeException extends Exception
{
/**
* Creates a new instance of <code>IllegalAgeException</code> without detail message.
*/
String exception;
public IllegalAgeException()
{
exception="Unknown";
}
/**
* Constructs an instance of <code>IllegalAgeException</code> with the specified detail message.
* @param msg the detail message.
*/
public IllegalAgeException(String msg)
{
super(msg);
this.exception=msg;
}
public String getException()
{
return this.exception;
}
}
//MoviePropertiesNotSetException class
public class MoviePropertiesNotSetException extends Exception
{
/**
* Creates a new instance of <code>MoviePropertiesNotSetException</code> without detail message.
*/
String exception;
public MoviePropertiesNotSetException()
{
exception="Unknown";
}
/**
* Constructs an instance of <code>MoviePropertiesNotSetException</code> with the specified detail message.
* @param msg the detail message.
*/
public MoviePropertiesNotSetException(String msg)
{
super(msg);
this.exception=msg;
}
public String getException()
{
return this.exception;
}
}
//Cinema class
import java.util.*;
public class Cinema
{
float cost;
String Title;
String mrPG="PG";
String mrA="A";
String mrG="G";
Movie movieNew = new Movie();
//MovieRating class
MovieRating mrating = new MovieRating();
private 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 an exception.
*/
public void setMovie(Movie movieNew) throws MyException
{
if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
{
System.out.println("The movie is called " +movieNew.getTitle()+
" and is rated " +movieNew.getRating()+ ".");
}
else
{
throw new MyException("The Movie properties not set");
}
}
/*
* this method first checks if the movie properties are set.
*/
public void showMovie() throws MyException
{
// movieNew.setName("Comedy");
// movieNew.setRating("PG");
// movieNew.setTitle("Mr Bin");
if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
{
System.out.println("Now Showing... "+movieNew.getTitle());
}
else
{
throw new MyException ("The movie properties are not set.");
}
}
/*Checks if Movie properties are set, if not throws MyException
* takes a collection of Human - Adult and Child
*/
public void addMovieGoers(Human h) throws MyException
{
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",6);
hm.add(a);hm.add(b);hm.add(c);hm.add(d);
hm.add(e);hm.add(f);
hm.trimToSize();
String toPrint = "";
if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
{
if(mrPG.equals(movieNew.getRating())) //movies rated PG
{
System.out.println("The following can only watch under parental guidance:");
for(int i = 0; i < hm.size(); i++)
{
Human test = (Human)hm.get(i);
if ( test instanceof Child)
{
if(toPrint.equals(""))
{
System.out.println(test);
}
else
{
System.out.println("Children: " + toPrint);
}
}
}
}
else if(mrA.equals(movieNew.getRating())) //movies rated Adult
{
System.out.println("The following can watch...");
for ( int i = 0; i < hm.size(); i++)
{
Human test = (Human)hm.get(i);
if ( test instanceof Adult)
{
if(toPrint.equals(""))
{
System.out.println(test);
}
else
{
System.out.println("Adults: " + toPrint);
}
}
}
}
else //Movies rated G
{
System.out.println("This is movie rated G. So the following can watch"+
": "+hm+"");
}
}
else
{
throw new MyException("The Movie properties are not set.");
}
}
public static void main(String args[]) throws MyException
{
Cinema cn = new Cinema();
Human h = new Human();
//instantiating and setting properties to the movie
Movie movieNew = new Movie();
movieNew.setName("Comedy");
movieNew.setRating("PG");
movieNew.setTitle("Mr Bin");
try
{
if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
{
cn.showMovie();
}
}
catch(MoviePropertiesNotSetException msg)
{
System.out.println("Nothing");
}
// System.out.println("Setting Movie properties:");
// cn.setMovie(movieNew);
//
// System.out.println("Current Show:");
// cn.showMovie();
//
// System.out.println("Movie Goers are: ");
// cn.addMovieGoers(h);
}
}
NOTE
Creating your own exceptions and passing them to the controlling module (here method with scenarios). After an exception occurs your code should stop execution of current scenario and go to another one.
I need to see several scenarios resulting with different exceptions being thrown before finally executing a final scenario without exceptions being thrown.