Hi, I have a Cinema class as shown in the code below. However I'm required to create my own exceptions and pass them to the controlling module (here method with scenarios). After an exception occurs the code should stop execution of current scenario and go to another one. The code should contains plenty of System.out.println statements so that the flow is clear.
Can someone give me a hand on this please, I'm a newbie in Java and I don't know how to go about this creating my own exceptions.

import java.util.*;
import java.io.*;
public class Cinema 
{
    float cost;
    String Title;

    String mrPG="PG";
    String mrA="A";
      
    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 IOException
    { 
        try
        {                      
            if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
            {
                 System.out.println("The movie is called " +movieNew.getTitle()+ 
                        " and is rated " +movieNew.getRating()+ ".");
            }
            else
            {
                 System.out.println("Movie title and rating not set");
            }
        }
        catch(Exception e)
        {
            System.out.println("Movie properties not set.");
        }
    }
     /*
      * this method first checks if the movie properties are set.
      */
    public void showMovie() throws IOException
    {
        try
        {
            if(movieNew.getTitle()!=null)
            {
                System.out.println(movieNew.getTitle()+" Now Showing");                           }
            else
            {
                System.out.println("Show has no Movie title.");
            }            
        }
        catch(Exception e)
        {
            System.out.println("Check the movie properties.");
        }
        finally
        {
            System.out.println("Thanks for coming.");           
        }
   }
   
public void addMovieGoers(Human h)
{
    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 = "";
    try
    {
        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("This movie is rated A");
                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 all can watch"+
                        ": ");
                 for ( int i = 0; i < hm.size(); i++)
                 {
                     System.out.println(i);
                 }
            }
        }
    }
    catch(Exception x)
    {
        System.out.println("Movie properties are missing. Please set the Title"+
                " and rating first.");
    }

 }

    public static void main(String args[]) throws IOException
    { 
        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");

        System.out.println("Setting Movie properties:");
        cn.setMovie(movieNew);

        System.out.println("Movie Goers are: ");
        cn.addMovieGoers(h);
        
    }
}

Recommended Answers

All 3 Replies

Woah!! Woah!! Woah!! First things first, don't catch Exception unless you really need to! Exception will catch *all* exceptions. I'd rather create my own exception class which inherits the base class. Then you can catch that one!

okay, I've created my exception class as:

public class MyException extends Exception
{
    String exception;
    public MyException()
    {
        super();
        exception="Unknown";
    }
    public MyException(String exp)
    {
        super(exp);
        this.exception=exp;
    }
    public String getException()
    {
        return this.exception;
    }
}

and changed my method to look like this:

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");
        }
    }

So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.

Another requirement to my Cinema class is to 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.

the main method is:

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("G");
        movieNew.setTitle("Mr Bin");

        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);
        
    }

Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)

public void showMovie() throws MyException
    { 
        movieNew.setName("Comedy");
        movieNew.setRating("G");
        movieNew.setTitle("Mr Bin");
        if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
        {
             System.out.println(movieNew.getTitle()+" Now Showing");             
        }
        else
        {
            throw new MyException ("The  movie properties are not set.");
        }            
    }

So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.

You also can use try catch to use your exceptions. See following example:

public int SomMethod() {
 try {
  //Your codes
}
 catch (MyException e) {
  //do whatever you wanted to do.
}
}

Another requirement to my Cinema class is to 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 can call a static method by:

public static returnType methodName (argument)

You don't have to create any object in order to call this method. You can call the method any time you want- from static method. So, you will basically call it from your main function (as that is by default static). Here are two examples of calling a static method:

public static void main(String args[]) throws MyException
    { 
        //direct call to a static method:
       yourStaticMethod(arg);
       //Through class
       Cinema.yourStaticMethod(arg); //NOTE: Cinema is the name of the class, not the object.
        
    }

Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)

Your question is not clear to me. You may use another property called showStatus which will contain the information about the show or a property called showTime to contain the time, which will compare time with current time and display if the show is available or not. Is that what you wanted? :-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.